Read the Latest TON Masterchain Sequence Number with JSON-RPC
Use Node.js to call a TON JSON-RPC endpoint, validate the response, and distinguish a live chain read from indexed historical queries.
To read the latest TON masterchain sequence number, send a getMasterchainInfo JSON-RPC request to a compatible endpoint and inspect result.last.seqno. The Node.js example below checks both HTTP and JSON-RPC errors before printing the value.
Get a compatible TON JSON-RPC endpoint
Use a TON JSON-RPC endpoint that supports getMasterchainInfo. Check your provider's documentation for the exact URL, network, and authentication requirements. The OnFinality TON network page is a relevant starting point when choosing access.
Set the endpoint as an environment variable. If your provider requires an API key in a header, adapt the request headers in the example to its documented format. Treat endpoint URLs containing credentials as secrets.
- Use a mainnet or testnet endpoint intentionally; the returned sequence number belongs to the network behind that endpoint.
- Do not assume every TON HTTP API uses the same URL path or authentication scheme.
export TON_RPC_URL='YOUR_TON_JSON_RPC_ENDPOINT'Replace the placeholder with your provider's actual endpoint. Avoid committing the URL if it contains credentials.
Request the latest masterchain information
Save the following as ton-masterchain.js and run it with Node.js 18 or later. It sends a JSON-RPC 2.0 request, applies a 10-second timeout, and validates the field before printing it.
async function main() {
const url = process.env.TON_RPC_URL;
if (!url || url === 'YOUR_TON_JSON_RPC_ENDPOINT') {
throw new Error('Set TON_RPC_URL to a TON JSON-RPC endpoint.');
}
const response = await fetch(url, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'getMasterchainInfo',
params: {}
}),
signal: AbortSignal.timeout(10000)
});
if (!response.ok) {
throw new Error(`RPC HTTP request failed with status ${response.status}.`);
}
let body;
try {
body = await response.json();
} catch {
throw new Error('RPC response was not valid JSON.');
}
if (body.error) {
throw new Error(`RPC error ${body.error.code}: ${body.error.message}`);
}
const seqno = body.result?.last?.seqno;
if (!Number.isSafeInteger(seqno) || seqno < 0) {
throw new Error('RPC response did not contain a valid result.last.seqno.');
}
console.log(`Latest masterchain seqno: ${seqno}`);
}
main().catch((error) => {
console.error(error.name === 'TimeoutError' ? 'RPC request timed out.' : error.message);
process.exitCode = 1;
});Run node ton-masterchain.js. A successful response prints Latest masterchain seqno: followed by the non-negative integer returned by the endpoint. The value changes as the chain advances; it is not a fixed test result.
An HTTP error may indicate an incorrect URL, missing credentials, or provider-side rejection. A JSON-RPC error may indicate an unsupported method or invalid parameters. Check the provider's documentation and avoid posting credential-bearing URLs when asking for help.
Know when an RPC read is not enough
This request reads current chain information from an RPC endpoint. It does not build a searchable history or provide application-specific aggregations.
If your application needs queries such as historical activity filtered by account or a derived dataset, evaluate an indexing pipeline that ingests the relevant chain data and stores it in a queryable form. Confirm the chain and data support of any indexing tool before designing around it.
- For repeated reads, handle rate limits and transient failures according to your provider's guidance; use bounded retries rather than an indefinite loop.
- To assess freshness for your use case, record response times and sequence numbers over time and compare them with an independent trusted source for the same network.
Next step: explore RPC request details
Use the OnFinality RPC Assistant as a starting point for exploring RPC requests, then verify method names, parameters, and authentication against the documentation for your chosen TON endpoint.