Skip to main content

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:

  1. Create and manage NEAR accounts
  2. Call functions on smart contracts
  3. Transfer tokens, including native NEAR, Fungible Tokens, Non-Fungible Tokens
  4. Sign transactions/meta-transactions/messages and broadcasting them to the network
  5. Deploy smart contracts

Available APIs

We have APIs available for Javascript, Rust, and Python. Add them to your project using the following commands:

npm i near-api-js
Wallet Integration

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.


Get State

Get basic account information, such as its code hash and storage usage.


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


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.

info

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.

info

Deleting an account DOES NOT affect its sub-accounts - they will remain active.

The Beneficiary Only Receives NEAR Tokens

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).

Make Sure the Beneficiary Account Exists

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.


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.


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.


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:

Keep in mind

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.


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.

Use a Global Contract

Once a global contract has been deployed, let’s see how you can reference and use it from another account.


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.

Typed Result

When using Typescript, you can type the return of callFunction<T>


Keys

Get All Access Keys


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.


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 NEAR the key can spend on transaction fees
tip

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.

danger

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:


Additional resources