NEAR API
The NEAR API is a set of libraries that allow you to interact with the NEAR blockchain. You can use it to in frontend and backend applications to:
- Call functions on a deployed contract
- Query information about an account
- Create NEAR accounts
- Send Tokens such as NEAR, FTs, and NFTs
- Create, Add and Delete Account Keys
- Deploy a contract
Our API is available in multiple languages, including:
- JavaScript:
near-api-js
- Rust:
near-api-rs
- Python:
py-near
To allow users to login into your web application using a wallet you will need the wallet-selector
. Read more in our Web Frontend integration article
Installโ
- ๐ JavaScript
- ๐ฆ Rust
- ๐ Python
Include near-api-js
as a dependency in your package.
npm i near-api-js
If you are building a site without using npm
, you can include the library directly in your HTML file through a CDN.
<script src="https://cdn.jsdelivr.net/npm/near-api-js/dist/near-api-js.min.js"></script>
cargo add near-api
pip install py-near
Importโ
- ๐ JavaScript
- ๐ฆ Rust
- ๐ Python
You can use the API library in the browser, or in Node.js runtime.
Loading...
Using the API in Node.js
All these examples are written for the browser, to use these examples in Node.js you should convert the project to an ES module. To do this, add the following to your package.json
:
Loading...
The methods to interact with the NEAR API are available through the prelude
module.
Loading...
You can use the NEAR API by importing the py_near
package, either entirely
import py_near
or only the parts you need, for example:
from py_near.account import Account
from py_near.providers import JsonProvider
Connecting to NEARโ
- ๐ JavaScript
- ๐ฆ Rust
The object returned from connect
is your entry-point for all commands in the API.
To transactions you'll need a KeyStore
.
Loading...
Mainnet/Localnet connection
// Mainnet config example
const connectionConfig = {
networkId: "mainnet",
keyStore: myKeyStore,
nodeUrl: "https://rpc.mainnet.near.org",
};
// Localnet config example
const connectionConfig = {
networkId: "local",
nodeUrl: "http://localhost:3030",
};
To interact with the blockchain you'll need to create a NetworkConfig
object.
Preset connections mainnet
and testnet
are available that come with standard configurations for each network.
Loading...
You can also create your own custom connection.
Loading...
Key Handlers: Stores & Signersโ
- ๐ JavaScript
- ๐ฆ Rust
- ๐ Python
To sign transactions you'll need to a KeyStore
with valid keypairs.
- Browser
- Credentials Path
- File
- Private Key
- Seed Phrase
BrowserLocalStorageKeyStore
can only be used in the browser, it uses the browser's local storage to store the keys.
// Creates keyStore using private key in local storage
const { keyStores } = nearAPI;
const myKeyStore = new keyStores.BrowserLocalStorageKeyStore();
UnencryptedFileSystemKeyStore
can be used is used to load keys from the legacy credentials directory used by the NEAR CLI.
Loading...
Keystores can be created by loading a private key from a json file.
Loading...
Keystores can be created by using a private key string.
Private keys have the format "ed25519:5Fg2...".
Loading...
Keystores can be created by using a seed phrase. To parse the seed phrase into a private key, the near-seed-phrase
library is needed.
npm i near-seed-phrase
Seed phrases have the format "shoe three gate ..." and are usually 12 words long.
Loading...
To sign transactions you'll need to create a Signer
that holds a valid keypair.
- Keystore
- Credentials Path
- File
- Private Key
- Seed Phrase
Signers can be created using the Keystore that is also used as the standard for saving keys with the NEAR CLI.
Loading...
Signers can be created using the credentials directory which is the legacy option for saving keys with the NEAR CLI.
Loading...
Signers can be created by loading a public and private key from a file.
Loading...
Signers can be created by using a private key string.
Private keys have the format "ed25519:5Fg2...".
Loading...
Signers can be created by using a seed phrase.
Seed phrases have the format "shoe three gate ..." and are usually 12 words long.
Loading...
TODO: not exactly the same in Python, it's more and account + RPC URL, or a JSON RPC provider
RPC Failoverโ
RPC providers can experience intermittent downtime, connectivity issues, or rate limits that cause client transactions to fail. This can be prevented by using the FailoverRpcProvider
that supports multiple RPC providers.
- ๐ JavaScript
- ๐ Python
Loading...
You can pass multiple RPC providers to JsonRpcProvider
from py_near.providers import JsonProvider
provider = JsonProvider(["https://test.rpc.fastnear.com", "https://rpc.testnet.pagoda.co"])
Accountโ
Instantiate Accountโ
This will return an Account object for you to interact with.
- ๐ JavaScript
- ๐ฆ Rust
- ๐ Python
Loading...
Loading...
You can instantiate any account with the following code:
from py_near.account import Account
account = Account(account_id="example-account.testnet", rpc_addr="https://rpc.testnet.pagoda.co")
await account.startup()
If you want to use it to submit transactions later, you need to also pass the private_key
param:
account = Account(account_id="example-account.testnet", private_key="ed25519:...", rpc_addr="https://rpc.testnet.pagoda.co")
Get Balanceโ
Gets the available and staked balance of an account in yoctoNEAR.
- ๐ JavaScript
- ๐ฆ Rust
- ๐ Python
Loading...
Loading...
from py_near.account import Account
account = Account(account_id="example-account.testnet", rpc_addr="https://rpc.testnet.pagoda.co")
await account.startup()
account_balance = account.get_balance()
Get Stateโ
Get basic account information, such as its code hash and storage usage.
- ๐ JavaScript
- ๐ฆ Rust
- ๐ Python
Loading...
Loading...
from py_near.account import Account
account = Account(account_id="example-account.testnet", rpc_addr="https://rpc.testnet.pagoda.co")
await account.startup()
account_state = account.fetch_state()