← Protocols
NEAR Protocol
01Description

NEAR is a sharded L1 with human-readable account IDs (alice.near), built-in account abstraction (function-call access keys), and a WASM smart-contract runtime. `near-api-js` is the canonical JS/TS SDK for chain reads, signing, and contract calls; `@near-wallet-selector/*` standardizes wallet connections.

02Best for
  • 01NEAR dapps and chain abstraction
  • 02named account onboarding (.near, .testnet)
  • 03function-call access keys / session keys
  • 04WASM smart contract calls (Rust / JS)
  • 05fast, cheap transactions for consumer apps
03Install
  • pnpm add near-api-js @near-wallet-selector/core @near-wallet-selector/my-near-wallet @near-wallet-selector/meteor-wallet
04Environment variables
VariableScopeDescription
NEXT_PUBLIC_NEAR_NETWORKClient`mainnet` or `testnet` — selects RPC and wallet URLs.
NEXT_PUBLIC_NEAR_NODE_URLClientNEAR JSON-RPC endpoint, e.g. https://rpc.mainnet.near.org or https://rpc.testnet.near.org.
NEXT_PUBLIC_NEAR_CONTRACT_IDClientAccount ID of the contract your dapp interacts with (e.g. myapp.near).
05Prompt snippet
Use `near-api-js` for chain access and `@near-wallet-selector` for connection UX. Initialize a connection with `connect({ networkId, nodeUrl, keyStore: new keyStores.BrowserLocalStorageKeyStore() })`, then `await near.account(accountId)` to read state, `account.viewFunction({ contractId, methodName, args })` for free reads, and `account.functionCall({ contractId, methodName, args, gas, attachedDeposit })` for state-changing calls. Wallet Selector wraps multiple wallets (MyNearWallet, Meteor, HERE, Ledger) behind one API: `selector.wallet().then(w => w.signAndSendTransaction({...}))`. Add a function-call access key (`account.addKey(publicKey, contractId, methodNames, allowance)`) to enable session-keyed dapps with no extra prompts.
06Gotchas
  • NEAR uses named accounts (`alice.near`) plus implicit accounts (64-hex) — both are valid `accountId`s but only named accounts can have sub-accounts and reserved storage; implicit accounts cannot create them.
  • Storage is paid via `storage_staking` — every byte of contract state requires the contract account to hold ~1 NEAR per 100KB locked; transactions that grow state without enough balance fail with `LackBalanceForState`.
  • Function-call access keys have a per-key allowance in NEAR; once exhausted the key is silently rejected and must be re-added.
  • Argument and return serialization is Borsh for Rust contracts but JSON for most JS contracts — pick the right `argsSerializer`/parsing or you'll get cryptic `Failed to deserialize input` errors.
  • Gas is denominated in TGas (10^12) and capped at 300 TGas per receipt; cross-contract calls split the budget across receipts and finality is asynchronous (one block per hop, ~600ms each).
  • Mainnet uses `near` TLD and testnet uses `.testnet`; mixing networkId and account suffix (e.g. `alice.testnet` on mainnet config) leads to silent `AccountDoesNotExist` errors.
07Alternatives