alpha release
alpha release

NEAR

api to interact with the NEAR blockchain

Run Code in Dev Console

JavaScript Compilation Targets

Optimize your app for your environment.

  • ESM ECMAScript Module
  • CJS CommonJS
  • IIFE / UMD Universal Module Definition

Without Node.js-specific features (such as Buffer.from) the library can run in the browser’s developer console.

dev console usage
+ + i

Berry Club demo

Wait, what's Berry Club?

"Berry Club is one of the oldest and certainly most engaging smart contracts on the NEAR blockchain…

It's a crazy mix between a chat room and graffiti wall."

try it:

This view call fetches lines populating the pixel board. No keys are required for view calls.

(paste code in dev console)

Load Board (View / query, no key needed)

await near.view({
  contractId: "berryclub.ek.near",
  methodName: "get_lines",
  args: {
    lines: [
      0,1,2,3,4,5,6,7,8,9,
      10,11,12,13,14,15,16,17,18,19,
      20,21,22,23,24,25,26,27,28,29,
      30,31,32,33,34,35,36,37,38,39,
      40,41,42,43,44,45,46,47,48,49
    ]
  },
});

Minimal example of signing and broadcasting a smart contract call to the draw method.

You must be “signed in” to draw, which means using one of your full‐access keys to create a session key.

(open dev console to track progress)

Draw Pixel (Sign and broadcast with session key)

await near.sendTx({
  receiverId: "berryclub.ek.near",
  actions: [
    near.actions.functionCall({
      methodName: "draw",
      args: {
        pixels: [{
          x: 10,
          y: 20,
          color: 65280, // decimal for green
        }],
      },
      gas: $$`100 Tgas`, // default 225 teragas
      deposit: "0", // default 0 yoctoNEAR
    }),
  ],
});

Fetching this account’s avocado_balance from the BerryClub contract.

Total Supply: 2417099.3688 🥑
Your Balance: 1583.7318 🥑
Get Berry Club Account (View / query, no key needed)

await near.view({
  contractId: "berryclub.ek.near",
  methodName: "get_account",
  args: { account_id: near.accountId() },
});

Beyond session keys

Some actions (like transferring funds) require your full‐access key. That means this request will route to your chosen web3 wallet, instead of signing and broadcasting the transaction from the session key.

Learn more about access keys: NEAR documentation

(privileged interactions get sent to wallet)

Buy Tokens (Cannot use session key)

await near.sendTx({
  receiverId: "berryclub.ek.near",
  actions: [
    near.actions.functionCall({
      methodName: "buy_tokens",
      args: {},
      gas: $$`100 Tgas`,
      deposit: $$`0.1 NEAR`,
    }),
  ],
});