NEAR API
We offer a collection of language-specific libraries that allow developers to interact with the NEAR blockchain from both frontend and backend applications. The different APIs allow you to perform a variety of actions on the NEAR blockchain, including but not limited to:
- Create and manage NEAR accounts
- Call functions on smart contracts
- Transfer tokens, including native NEAR, Fungible Tokens, Non-Fungible Tokens
- Sign transactions/meta-transactions/messages and broadcasting them to the network
- Deploy smart contracts
Available APIs
We have APIs available for Javascript, Rust, and Python. Add them to your project using the following commands:
- 🌐 near-api-js
- 🌐 near-kit
- 🦀 near-api-rs
- 🐍 py-near
npm i near-api-js
npm i near-kit
cargo add near-api
pip install py-near
If you are building a web app and need to add Wallet Login on it you will instead need a Wallet Connector
Account
Get Balance
Gets the available and staked balance of an account in yoctoNEAR.
- 🌐 near-api-js
- 🌐 near-kit
- 🦀 near-api-rs
- 🐍 py-near
Loading...
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.
- 🌐 near-api-js
- 🌐 near-kit
- 🦀 near-api-rs
- 🐍 py-near
Loading...
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()
Create Named Account
To create a named account like user.testnet, you need to call the create_account function on near (or testnet), passing as parameters the new account ID, and a public key to add as FullAccess key
- 🌐 near-api-js
- 🌐 near-kit
- 🦀 near-api-rs
- 🐍 py-near
Loading...
Loading...
Loading...
Creating an account from a seed phrase
You can also create an account via a randomly generated seed phrase.
Loading...
await account.function_call("testnet", "create_account", {"new_account_id": "example-account.testnet", "new_public_key": "ed25519:..."}, "30000000000000", 1 * NEAR)
Create Sub-Account
Accounts on NEAR can create sub-accounts under their own namespace, which is useful for organizing accounts by purpose — for example, project.user.testnet.
- 🌐 near-api-js
- 🌐 near-kit
- 🦀 near-api-rs
- 🐍 py-near
Loading...
Loading...
Loading...
Create a sub-account and fund it with your main account:
from py_near.account import Account
from py_near.dapps.core import NEAR
account = Account(account_id="example-account.testnet", private_key="ed25519:...", rpc_addr="https://rpc.testnet.pagoda.co")
await account.startup()
res = account.create_account(account_id="sub.example-account.testnet", public_key="...", initial_balance=1 * NEAR))
Parent accounts have no control over their sub-accounts, they are completely independent.
Delete Account
Accounts on NEAR can delete themselves, transferring any remaining balance to a specified beneficiary account.
- 🌐 near-api-js
- 🌐 near-kit
- 🦀 near-api-rs
Loading...
Loading...
Loading...
Deleting an account DOES NOT affect its sub-accounts - they will remain active.
Fungible (FTs) or Non-Fungible tokens (NFTs) held by the account ARE NOT automatically transferred. These tokens are still associated with the account, even after the account is deleted. Make sure to transfer those assets manually before deletion, or you're risking losing them permanently! Once the account is gone, those assets are effectively stuck unless the same account is recreated by anyone (not necessarily you).
If the beneficiary account doesn't exist, all NEAR tokens sent to it will be burned. Double-check the account ID before proceeding.
Transactions
Send Tokens
Accounts can transfer different types of tokens to other accounts, including the native NEAR token and NEP-141 fungible tokens.
- 🌐 near-api-js
- 🌐 near-kit
- 🦀 near-api-rs
- 🐍 py-near
Loading...
Loading...
Loading...
from py_near.account import Account
from py_near.dapps.core import NEAR
account = Account(account_id="example-account.testnet", private_key="ed25519:...", rpc_addr="https://rpc.testnet.pagoda.co")
await account.startup()
await account.send_money("receiver-account.testnet", 1 * NEAR))
Call Function
A smart contract exposes its methods, and making a function call that modifies state requires a Signer/KeyPair. You can optionally attach a NEAR deposit to the call.
- 🌐 near-api-js
- 🌐 near-kit
- 🦀 near-api-rs
- 🐍 py-near
- function call
- typed contract
Loading...
When using Typescript, you can type the return of callFunction<T>
Loading...
- function call
- typed contract
Loading...
When using Typescript, you can type the return of Near.view<T> and Near.call<T>
Loading...
Loading...
Loading...
await account.function_call("usn.near", "ft_transfer", {"receiver_id": "bob.near", "amount": "1000000000000000000000000"})
Batch Actions
You can send multiple actions in a batch to a single receiver. If one action fails then the entire batch of actions will be reverted.
- 🌐 near-api-js
- 🌐 near-kit
- 🦀 near-api-rs
Loading...
Loading...
Loading...
Simultaneous Transactions
The only way to have true simultaneous transactions is to use multiple access keys on a same account. Each access key maintains its own nonce, allowing transactions signed with different keys to be processed in parallel:
- 🌐 near-api-js
- 🌐 near-kit
- 🦀 near-api-rs
- 🐍 py-near
Loading...
Loading...
Loading...
import asyncio
from py_near.account import Account
account = Account(account_id="example-account.testnet", private_key="ed25519:...", rpc_addr="https://rpc.testnet.pagoda.co")
await account.startup()
# Prepare the transactions
tx1 = account.function_call("guestbook.near-examples.testnet", "add_message", { "text": "Hello, world!" })
tx2 = account.function_call("counter.near-examples.testnet", "increment", {})
# Send the transactions simultaneously
const transactionsResults = await asyncio.gather(tx1, tx2)
Simultaneous execution means there’s no guarantee of order or success. Any transaction may fail independently. If your use case requires strict ordering, then you should stick to sending transactions sequentially from a single key.
Deploy a Contract
On NEAR, a smart contract is deployed as a WASM file. Every account has the potential to become a contract — you simply need to deploy code to it.
- 🌐 near-api-js
- 🌐 near-kit
- 🦀 near-api-rs
- 🐍 py-near
Loading...
Loading...
Note that the signer here needs to be a signer for the same account_id as the one used to construct the Contract object.
Loading...
import asyncio
from py_near.account import Account
account = Account(account_id="example-account.testnet", private_key="ed25519:...", rpc_addr="https://rpc.testnet.pagoda.co")
await account.startup()
with open("contract.wasm", "rb") as f:
contract_code = f.read()
await account.deploy_contract(contract_code)
Deploy a Global Contract
Global contracts allow smart contracts to be deployed once and reused by any account without incurring high storage costs.
There are two ways to reference a global contract:
- By account: The contract code is tied to another account.
- By hash: You reference the contract by its immutable code hash.
- 🌐 near-api-js
- 🌐 near-kit
- 🦀 near-api-rs
- by account
- by hash
Loading...
Loading...
- by account
- by hash
Loading...
Loading...
Once you've created an Account instance, you can deploy your regular contract as a global contract.
- By Account
- By Hash
Let’s look at an example of deploying a global contract by account.
To do this, use the deploy_global_contract_code function and use the method as_account_id, along with the contract’s code bytes.
let global_account_id: AccountId = "nft-contract.testnet".parse().unwrap();
let code = std::fs::read("path/to/your/contract.wasm").unwrap();
let signer = Signer::new(Signer::from_secret_key(private_key)).unwrap();
let result: FinalExecutionOutcomeView = Contract::deploy_global_contract_code(code)
.as_account_id(global_account_id)
.with_signer(signer)
.send_to_testnet()
.await.unwrap();
See full example on GitHub
Let’s look at an example of deploying a global contract by hash.
To do this, use the deploy_global_contract_code function and use the method as_hash, along with the contract’s code bytes.
let account_id: AccountId = "my-account.testnet".parse().unwrap();
let code = std::fs::read("path/to/your/contract.wasm").unwrap();
let signer = Signer::new(Signer::from_secret_key(private_key)).unwrap();
let result: FinalExecutionOutcomeView = Contract::deploy_global_contract_code(code)
.as_hash()
.with_signer(account_id, signer)
.send_to_testnet()
.await.unwrap();
See full example on GitHub
Use a Global Contract
Once a global contract has been deployed, let’s see how you can reference and use it from another account.
- 🌐 near-api-js
- 🌐 near-kit
- 🦀 near-api-rs
- by account
- by hash
Loading...
Loading...
- by account
- by hash
Loading...
Loading...
- By Account
- By Hash
To reference a global contract by account, you need to call the use_global_account_id function and pass the source accountId where the contract was originally deployed.
let global_account_id: AccountId = "nft-contract.testnet".parse().unwrap();
let my_account_id: AccountId = "my-contract.testnet".parse().unwrap();
let my_signer = Signer::new(Signer::from_secret_key(private_key)).unwrap();
let result: FinalExecutionOutcomeView = Contract::deploy(my_account_id)
.use_global_account_id(global_account_id)
.without_init_call()
.with_signer(my_signer)
.send_to_testnet()
.await.unwrap();
See full example on GitHub
To reference a global contract by hash, you need to call the use_global_hash function and pass the source hash of the original contract.
let global_hash: types::CryptoHash = "DxfRbrjT3QPmoANMDYTR6iXPGJr7xRUyDnQhcAWjcoFF".parse().unwrap();
let account_id: AccountId = "my-contract.testnet".parse().unwrap();
let signer = Signer::new(Signer::from_secret_key(private_key)).unwrap();
let result: FinalExecutionOutcomeView = Contract::deploy(account_id)
.use_global_hash(global_hash)
.without_init_call()
.with_signer(signer)
.send_to_testnet()
.await.unwrap();
See full example on GitHub
View Function
View functions are read-only methods on a smart contract that do not modify state. You can call them without using an account or signing a transaction.
- 🌐 near-api-js
- 🌐 near-kit
- 🦀 near-api-rs
- 🐍 py-near
Loading...
When using Typescript, you can type the return of callFunction<T>
Loading...
When using Typescript, you can type the return of Near.view<T>
Loading...
view_call_result = await account.view_function("guestbook.near-examples.testnet", "total_messages", {})
# If args are required, they can be passed in like this in the 3rd argument:
# {
# "from_index": "0",
# "limit": "10"
# }
print(view_call_result)
Keys
Get All Access Keys
- 🌐 near-api-js
- 🦀 near-api-rs
- 🐍 py-near
Loading...
Loading...
keys = await account.get_access_key_list()
Add Full Access Key
A Full Access key grants complete control over the account.
Anyone with this key can transfer funds, sign transactions, interact with contracts, or even delete the account entirely.
- 🌐 near-api-js
- 🌐 near-kit
- 🦀 near-api-rs
- 🐍 py-near
Loading...
Loading...
Loading...
keys = await account.add_full_access_public_key("5X9WvUbRV3aSd9Py1LK7HAndqoktZtcgYdRjMt86SxMj")
Add Function Call Key
A Function Call access key is designed specifically to sign transactions that include only functionCall actions to a specific contract.
You can further restrict this key by:
- Limiting which method names can be called
- Capping the amount of
NEARthe key can spend on transaction fees
- 🌐 near-api-js
- 🌐 near-kit
- 🦀 near-api-rs
- 🐍 py-near
Loading...
Loading...
Loading...
await account.add_public_key(
"5X9WvUbRV3aSd9Py1LK7HAndqoktZtcgYdRjMt86SxMj",
"example-contract.testnet", # Contract this key is allowed to call
["example_method"], # Methods this key is allowed to call (optional)
0.25 * NEAR # Gas allowance key can use to call methods (optional)
)
For security reasons, Function Call access keys **can only be used with function calls that attach zero NEAR tokens. Any attempt to include a deposit will result in a failed transaction.
Delete Access Key
Accounts on NEAR can delete their own keys.
- 🌐 near-api-js
- 🌐 near-kit
- 🦀 near-api-rs
- 🐍 py-near
Loading...
Loading...
Loading...
await account.delete_public_key("5X9WvUbRV3aSd9Py1LK7HAndqoktZtcgYdRjMt86SxMj")
Be very careful when deleting keys, remove all keys from an account and you will lose access to the account permanently
Validate Message Signatures
Users can sign messages using the wallet-selector signMessage method, which returns a signature. This signature can be verified using the following code:
- 🌐 near-api-js
- 🌐 near-kit
Loading...
Loading...
Additional resources
- 🌐 near-api-js
- 🌐 near-kit
- 🦀 near-api-rs
- 🐍 py-near