{"library":"near-api-js","title":"NEAR JavaScript API","description":"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.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install near-api-js"],"cli":null},"imports":["import { Account } from 'near-api-js'","import { JsonRpcProvider } from 'near-api-js'","import { nearToYocto } from 'near-api-js'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { Account, JsonRpcProvider, teraToGas, KeyPairString, nearToYocto } from \"near-api-js\";\n\n// Configure your connection to a NEAR testnet RPC endpoint\nconst provider = new JsonRpcProvider({\n  url: \"https://test.rpc.fastnear.com\",\n});\n\n// For read-only calls (e.g., viewing contract state), you can use the provider directly\nconst messages = await provider.callFunction({\n  contractId: 'guestbook.near-examples.testnet',\n  method: \"get_messages\",\n  args: {},\n});\n\nconsole.log(\"Guestbook messages:\", messages);\n\n// To modify blockchain state, you need an account with a signer (private key)\nconst accountId: string = 'example.testnet'; // Replace with an actual account ID\n// WARNING: Never hardcode private keys in production. Use environment variables or secure key management.\nconst privateKey = process.env.NEAR_PRIVATE_KEY ?? 'ed25519:YOUR_PRIVATE_KEY_HERE' as KeyPairString;\n\nif (privateKey === 'ed25519:YOUR_PRIVATE_KEY_HERE') {\n  console.warn(\"WARNING: Please set the NEAR_PRIVATE_KEY environment variable or replace the placeholder in quickstart. The example will not modify state without a valid key.\");\n} else {\n  const account = new Account(accountId, provider, privateKey);\n\n  // Call a mutable contract method to add a message\n  const result = await account.callFunction({\n    contractId: 'guestbook.near-examples.testnet',\n    methodName: \"add_message\",\n    args: { text: `Hello from near-api-js at ${new Date().toISOString()}!` },\n    gas: teraToGas('30'), // Attach 30 TeraGas for execution\n    deposit: nearToYocto('0.1'), // Attach 0.1 NEAR as deposit\n  });\n\n  console.log(\"Transaction result:\", result);\n}\n","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}