EOSJS Library

22.1.0 · active · verified Tue Apr 21

EOSJS is the official JavaScript library for interacting with EOSIO blockchain APIs. It provides a comprehensive set of tools for sending transactions, querying blockchain state, managing accounts, and performing cryptographic operations like signing and verifying. The current stable version is 22.1.0, which introduces support for read-only transactions within smart contracts via HTTP-RPC and action return values. Historically, the library has undergone significant internal changes, such as the switch from `eosjs-ecc` to the `elliptic` cryptography library in v21.0.2, while striving to maintain a stable public API. Releases often include security, stability, and miscellaneous fixes, with release candidates preceding major version bumps. It is a critical component for building applications that interact with EOSIO-based blockchains, offering robust TypeScript support and keeping pace with new blockchain features.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize `eosjs` with `JsonRpc` and `Api`, perform a read-only query using `get_table_rows`, and provides a commented-out example of pushing a transaction.

import { JsonRpc } from 'eosjs';
import { Api, RpcInterfaces } from 'eosjs';
import { JsSignatureProvider } from 'eosjs/dist/eosjs-jssig'; // Example signature provider
import { TextEncoder, TextDecoder } from 'util'; // For Node.js environments

// Configuration
const defaultPrivateKey = process.env.EOS_PRIVATE_KEY ?? ''; // Replace with an actual private key for signing, DO NOT expose in client-side code.
const rpcEndpoint = 'https://eos.greymass.com'; // Example public EOS mainnet endpoint

// 1. Setup RPC client
const rpc = new JsonRpc(rpcEndpoint, { fetch }); // Using global fetch or node-fetch in Node.js

// 2. Setup SignatureProvider (required even for read-only if you intend to send transactions later)
// For demonstration, using a JS-based signature provider. In production, consider more secure options.
const signatureProvider = new JsSignatureProvider([defaultPrivateKey]);

// 3. Setup API client
const api = new Api({
    rpc,
    signatureProvider,
    textDecoder: new TextDecoder(), // Required for Node.js
    textEncoder: new TextEncoder(), // Required for Node.js
});

async function runExample() {
    try {
        console.log(`Querying a public contract table on ${rpcEndpoint}...`);

        // Example: Get 'stat' table from 'eosio.token' contract for 'EOS' symbol
        const tableRows = await rpc.get_table_rows({
            json: true,
            code: 'eosio.token',
            scope: 'EOS',
            table: 'stat',
            lower_bound: null,
            upper_bound: null,
            limit: 1,
        });

        console.log('Table Rows (eosio.token, EOS, stat):', JSON.stringify(tableRows, null, 2));

        // Example of pushing a transaction (requires a valid private key and account for 'youraccount')
        // This part is commented out as it requires a real key and funded account to execute.
        /*
        if (defaultPrivateKey && defaultPrivateKey !== '') {
            console.log('Attempting to push a dummy transaction...');
            const transactionResult = await api.transact({
                actions: [{
                    account: 'eosio.token',
                    name: 'transfer',
                    authorization: [{
                        actor: 'youraccount', // Replace with your EOS account name
                        permission: 'active',
                    }],
                    data: {
                        from: 'youraccount',
                        to: 'teamgreymass', // Example recipient
                        quantity: '0.0001 EOS', // Use a small test amount
                        memo: 'Test transfer from eosjs quickstart',
                    },
                }]
            }, {
                blocksBehind: 3,
                expireSeconds: 30,
            });
            console.log('Transaction Result:', JSON.stringify(transactionResult, null, 2));
        }
        */

    } catch (error) {
        console.error('Error:', error);
        if (error instanceof RpcInterfaces.RpcError) {
            console.error('RPC Error details:', JSON.stringify(error.json, null, 2));
        }
    }
}

runExample();

view raw JSON →