Onchain SQL over a month (long ranges read by day)
POST /api/onchain-sql-rangeRun the same read-only SQL as onchain-sql (the same tables, dialect and rules) over long ranges in one call. Send POST /api/onchain-sql-range with the required field sql and pay $0.33 per call over x402 with USDC on an EVM chain or MPP. It returns a JSON object with rows, rowCount, maxRows, returned, matched and 3 more.
It takes x402 exact on EVM chains and runs as a job (Prefer: respond-async). A plain row select on solana.instructions whose whole block_time range runs past the query service's time window is read one UTC day at a time, up to 31 days, and the rows are merged in block_time order, up to 1,000 rows. When the call's time, the row cap or a day the query service cannot answer ends the walk early, the answer is served partial and marked in fields (complete, truncated, windowed with the days read, coveredFrom and coveredTo) with the range left to read. A walk that read at least one day is a served answer and is charged at this route's price, even when it is partial or holds no rows; a walk that read no day answers 422 and is not charged (this route takes x402 exact on EVM chains only). Any query that is not read by day gets the same answer as on onchain-sql, at this route's price; send it to onchain-sql for the lower price. Takes up to 3 minutes to answer: send the paid call with "Prefer: respond-async" to get a job link at once and collect the answer from it (on x402 the payment settles only on a delivered result), or with "Prefer: wait=180" and keep the connection open for at least 210 seconds, because a client that disconnects first loses the answer. A paid call with neither header is answered 400 before any payment is taken.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
sql | string | yes | Read-only SELECT (ClickHouse dialect, max 60,000 chars) over the onchain-sql tables (base.*, base_sepolia.*, solana.instructions; a query that filters, groups or aggregates solana.instructions needs a block_time range). A plain row select on solana.instructions (one table, no aggregate, GROUP BY, DISTINCT, JOIN or subquery, ORDER BY block_time if any) over more than a day and up to 31 days whose whole range runs past the query service's time window is read one UTC day at a time and merged in order. Returns up to 1,000 rows; a walk that ends early is marked partial in fields. A walk that read at least one day is a served answer and is charged at this route's price, even when it is partial or holds no rows; a walk that read no day answers 422 and is not charged (this route takes x402 exact on EVM chains only). |
cacheSeconds | number | no | Accept cached results up to this old (max 900). |
Example request
curl -i -X POST https://agent402.tools/api/onchain-sql-range \
-H "Content-Type: application/json" \
-d '{"sql":"SELECT block_slot, block_time, executing_account FROM solana.instructions WHERE block_time > now() - INTERVAL 30 DAY AND executing_account = '\''TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'\'' ORDER BY block_time LIMIT 100"}'Without payment this returns HTTP 402 Payment Required with the exact price for onchain-sql-range; any x402 v2 or MPP client pays it and retries.
Watch a call get paid
POST /api/onchain-sql-range$0.33 USDCPAYMENT-SIGNATUREon 11 chains200 OKAgent402-ReceiptEd25519Example response
{
"rows": [
{
"block_slot": 377412001,
"block_time": "2026-10-24T00:00:01Z",
"executing_account": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
}
],
"rowCount": 1,
"maxRows": 1000,
"returned": 1,
"matched": 1,
"complete": true,
"truncated": false,
"windowed": {
"by": "day",
"days": 31,
"daysRead": 31,
"order": "asc",
"from": "2026-10-23T12:00:00.000Z",
"to": "2026-11-22T12:00:00.000Z"
}
}| Field | Type | Always present | In the example |
|---|---|---|---|
rows | array of objects | yes | 1 item in the example |
rowCount | number | yes | 1 |
maxRows | number | yes | 1000 |
returned | number | yes | 1 |
matched | number | yes | 1 |
complete | boolean | yes | true |
truncated | boolean | yes | false |
windowed | object | yes | 6 fields: by, days, daysRead, order, from, to |
From an MCP client
catalog.call {
"slug": "onchain-sql-range",
"params": {
"sql": "SELECT block_slot, block_time, executing_account FROM solana.instructions WHERE block_time > now() - INTERVAL 30 DAY AND executing_account = 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA' ORDER BY block_time LIMIT 100"
}
}The hosted connector at https://agent402.tools/mcp needs a payment for onchain-sql-range; the stdio package pays it from a wallet or from an AGENT402_CREDITS_KEY issued earlier. Local install: npx -y agent402-mcp@0.15.0.
Errors and behavior
sqlis required. An input the tool rejects returns an HTTP 4xx whose body carrieserror,tool,expected,requiredandexample, so the caller can correct it.- A paid call that ends in any status of 400 or above is not charged over x402, MPP or a prepaid credits key: settlement runs after the tool and only on a successful response. Some calls are paid before they run; a failed one can be retried free with the same payment, or is refunded.
- Wallet-only: this tool reaches the network or stored state, so it has no proof-of-work tier. A prepaid card-credits key issued earlier (
Authorization: Bearer a402_...) also pays it. - Long-running: payment settles after the work finishes, so x402 is offered on EVM chains only and MPP on Base or Celo; Tempo is not offered.
- Takes up to 180 seconds. Send the paid call with
Prefer: respond-asyncto get202and a job link at once, then pollGET /api/jobs/{id}for its stage, progress and ETA until it has the answer; payment still settles only when the answer is ready, except a Tempo transfer sent before the call, which is repaid after on-chain proof if the run fails. To wait on the connection instead, sendPrefer: wait=180and keep it open that long. A paid call with neither header is answered400before any payment is taken. - A
GETorHEADto /api/onchain-sql-range returns the same 402 quote, so the price can be read without a body. - An
Idempotency-Keyheader makes a retried paid call replay the first 200 instead of charging again (an answer larger than 1 MB is not replayed).
Paid call (JavaScript agent)
import { wrapFetchWithPayment } from "@x402/fetch";
import { x402Client } from "@x402/core/client";
import { registerExactEvmScheme } from "@x402/evm/exact/client";
import { privateKeyToAccount } from "viem/accounts";
const client = new x402Client();
client.setSpendControls?.({ maxAmountPerPayment: "$0.33" }); // refuse any quote above this tool's price
registerExactEvmScheme(client, { signer: privateKeyToAccount(KEY) });
const payFetch = wrapFetchWithPayment(fetch, client);
const res = await payFetch("https://agent402.tools/api/onchain-sql-range", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
"sql": "SELECT block_slot, block_time, executing_account FROM solana.instructions WHERE block_time > now() - INTERVAL 30 DAY AND executing_account = 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA' ORDER BY block_time LIMIT 100"
}),
});Related tools
Onchain SQL over a week (read by day)
Run the same read-only SQL as onchain-sql (the same tables, dialect and rules) over a week in one call, on every payment.
Onchain SQL (query Base with SQL)
Run read-only SQL against indexed, DECODED blockchain data - base.events (decoded logs with parameters).
Onchain SQL schema
The table + column schema for the onchain-sql tool - every queryable table (base.events, base.transactions, base.blocks.
Onramp link (fund a wallet with a card)
Generate a single-use hosted onramp URL that lets a human fund any wallet with a card or Apple Pay - the fastest way.
Testnet faucet (try x402 free)
Fund any address with testnet money from a testnet faucet - USDC (1) or ETH (0.0001) on Base Sepolia, USDC (1) or SOL.
Wallet token balances (indexed)
All token balances for any address in one call, from an indexed chain-data API - ERC-20 + native on EVM, SPL on Solana.