Rootscan Docs
  • 👋Getting Started
    • Creating Portal Account
    • Creating API & RPC Keys
    • Managing Subscriptions
  • 🔗API
    • API Endpoints
  • 🧊RPC
    • Usage Example
    • Network Endpoints
    • RPC Documentation
Powered by GitBook
On this page

Was this helpful?

  1. RPC

Usage Example

To make RPC calls, you need to generate an RPC API Key and include it in the request header as the x-api-key parameter.

  • Create an API instance

import "@therootnetwork/api-types";
import { ApiPromise, HttpProvider } from '@polkadot/api';

// Set node endpoint (ideally, replace with .env variable)
const endpoint = 'https://rootscan.io/archive'

// Set RPC API Key into Header
const headers = { 
    x-api-key: process.env['RPC_API_KEY'] || '' 
};

// Initialize the provider instance
const provider = new HttpProvider(endpoint, headers);

// Create an API instance
const api = await ApiPromise.create({ 
    ...getApiOptions(),
    provider,
});
import "@therootnetwork/api-types";
import { ApiPromise, WsProvider } from '@polkadot/api';

// Set node endpoint (ideally replace with .env variable)
const endpoint = 'wss://rootscan.io/archive/ws'

// Set RPC API Key into Header
const headers = { 
    x-api-key: process.env['RPC_API_KEY'] || '' 
};

// Automatically reconnect after 1 sec
const autoConnectMs = 1000;

// Initialize the provider instance
const provider = new WsProvider(endpoint, autoConnectMs, headers);

// Create an API instance
const api = await ApiPromise.create({ 
    ...getApiOptions(),
    provider,
});
  • Use api instance to interact with the node

import "@therootnetwork/api-types";

// Query and display account data
const data = await api.query.system.account("0xE04CC55ebEE1cBCE552f250e85c57B70B2E2625b");
const unsubscribe = await api.rpc.chain.subscribeNewHeads((header) => {
  console.log("New block: " + header.number.toNumber());
});

api.on('disconnected', () => {
  unsubscribe(); // Unsubscribe to avoid memory leaks
});

Alternatively, we support passing the API key as a query parameter instead of a using it in the header. Here is an example of such a query: https://rootscan.io/archive?apikey={API_KEY}

PreviousManaging SubscriptionsNextNetwork Endpoints

Last updated 7 months ago

Was this helpful?

🧊