near-api-js is the official JavaScript/TypeScript library for interacting with the NEAR Protocol blockchain via its RPC API. Currently at v7.2.0, this package consolidates all core NEAR functionality into a single library, simplifying development for backend services, CLIs, and scripts. It provides comprehensive tools for account management, transaction building, key management, smart contract interaction, and direct RPC calls. The library offers full TypeScript support and is designed to work seamlessly in both browser and Node.js environments. Key differentiators include its 'batteries-included' approach, user-friendly helpers for unit conversions (e.g., NEAR to yoctoNEAR, TeraGas to Gas), and advanced features like parallel transaction sending using multiple keys for improved performance and nonce management. While it can be used in browsers, the library is primarily recommended for backend applications, with official web login solutions suggested for frontend use cases.
npm install near-api-jsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates both read-only RPC calls using a provider and state-changing contract calls requiring an `Account` and `KeyPairString` for signing, including unit conversions for gas and deposit.
Ensure `parseNearAmount` is called with valid numeric strings or numbers. Implement proper error handling for invalid input instead of checking for `null`.
Refer to the `MIGRATION.md` guide in the repository. Replace imports from individual `@near-js/*` packages with direct imports from `near-api-js` (e.g., `import { Account } from '@near-js/accounts'` becomes `import { Account } from 'near-api-js'`).For frontend applications with user interaction, integrate with `Wallet Selector` and follow the official web login documentation for authentication and transaction signing. Use `near-api-js` for its utility functions or read-only public data queries.
Store private keys in environment variables, use a secrets management service, or integrate with a secure KeyStore (`near-api-js` supports various keystores for Node.js and browser). Never commit private keys to version control.
Ensure `new JsonRpcProvider({ url: 'YOUR_RPC_URL' })` is correctly configured with a valid and accessible NEAR RPC endpoint (e.g., `https://test.rpc.fastnear.com`). Check network connectivity and firewall rules.If using Node.js, ensure your project is configured for ES Modules (e.g., `"type": "module"` in `package.json`) and use `import` statements. For older CommonJS environments, ensure you are on a compatible `near-api-js` version or use dynamic `import()` if supported.
Verify that the `accountId` is spelled correctly and exists on the target network (e.g., `testnet`, `mainnet`). You can use a NEAR explorer to confirm account existence.
Ensure the private key string is correctly formatted, typically starting with `ed25519:` followed by the base58-encoded private key. Double-check for typos or truncation.
No dependency data recorded yet.