Read Klaytn staking information with JSON-RPC

Use the Klaytn klay_getStakingInfo method to retrieve staking data from a Klaytn RPC endpoint and handle common request failures.

To retrieve Klaytn staking information, send a JSON-RPC request for klay_getStakingInfo to a Klaytn RPC endpoint. This guide shows the request, checks the response for errors, and explains what to do if you need indexed or searchable data.

Set up a Klaytn RPC endpoint

You need an RPC endpoint configured for the Klaytn network and a Node.js 18 or newer installation. The Klaytn staking RPC page is a starting point for finding the relevant network destination; use the endpoint URL provided to you there and keep it private if it contains credentials.

An RPC call reads data exposed by the node. It does not create an indexed dataset or provide a database for arbitrary historical queries.

  • Set the endpoint in the KLATYN_RPC_URL environment variable before running the example.
  • Do not paste an endpoint containing a secret into shared code or logs.

Request staking information

Klaytn's klay_getStakingInfo JSON-RPC method takes an empty parameter list. The script below sends the request, rejects HTTP and JSON-RPC errors, and verifies that the response includes a result before printing it.

JavaScript · Node.js
const endpoint = process.env.KLAYTN_RPC_URL;

if (!endpoint) {
  console.error("Set KLATYN_RPC_URL to your Klaytn JSON-RPC endpoint.");
  process.exit(1);
}

try {
  const response = await fetch(endpoint, {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify({
      jsonrpc: "2.0",
      method: "klay_getStakingInfo",
      params: [],
      id: 1
    })
  });

  if (!response.ok) {
    throw new Error(`HTTP ${response.status} ${response.statusText}`);
  }

  const payload = await response.json();
  if (payload.error) {
    throw new Error(`JSON-RPC error ${payload.error.code}: ${payload.error.message}`);
  }
  if (!("result" in payload)) {
    throw new Error("Response did not contain a JSON-RPC result.");
  }

  console.log(JSON.stringify(payload.result, null, 2));
} catch (error) {
  console.error(`Could not retrieve Klaytn staking information: ${error.message}`);
  process.exitCode = 1;
}

Run it with KLATYN_RPC_URL='your-endpoint' node staking-info.mjs. The expected result is a JSON object returned by the RPC method; its fields and values depend on the network state when the request is handled.

If the request times out or returns an HTTP error, check that the URL is correct, reachable, and configured for Klaytn. A JSON-RPC error indicates the server received the request but reported a method or request problem; inspect the returned code and message.

Choose RPC reads or indexed queries

This request reads the staking information exposed by one RPC method. RPC is suitable when your application needs a direct node response. Indexing is a separate workflow: it processes chain data into a queryable store, which can support application-specific filtering or repeated historical queries when configured for those needs.

For the next step, see SubQuery Learn for indexing guides, or use the RPC Assistant to explore RPC endpoints.