Onchain SQL (query Base with SQL)
POST /api/onchain-sqlRun read-only SQL against Coinbase's indexed, DECODED blockchain data - base.events (decoded logs with parameters), base.transactions, base.blocks, base.decoded_user_operations, base.transaction_attributions (builder codes), plus solana.instructions and hyperevm.events. Send POST /api/onchain-sql with the required field sql and pay $0.020 per call over x402 or MPP (there is no free tier). It returns a JSON object with rows and rowCount.
ClickHouse-dialect SELECTs, server-side grammar validation, up to 50k rows / 30s / 12 joins. Ask Base anything - token flows, event analytics, gas studies - in one call, no indexer to run.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
sql | string | yes | Read-only SELECT (ClickHouse dialect, max 10,000 chars). Tables: base.events, base.transactions, base.blocks, base.encoded_logs, base.decoded_user_operations, base.transaction_attributions, base_sepolia.*, solana.instructions, hyperevm.events |
cacheSeconds | number | no | Accept cached results up to this old (max 900). Cheaper + faster for repeated analytics. |
Example request
curl -i -X POST https://agent402.tools/api/onchain-sql \
-H "Content-Type: application/json" \
-d '{"sql":"SELECT COUNT(*) AS txs FROM base.transactions WHERE block_number > 0 LIMIT 1"}'
Without payment this returns HTTP 402 Payment Required with the exact price for onchain-sql; any x402 v2 or MPP client pays it and retries.
Example response
{
"rows": [
{
"txs": 123456
}
],
"rowCount": 1
}
| Field | Type | Always present | In the example |
|---|---|---|---|
rows | array of objects | yes | 1 item in the example |
rowCount | number | yes | 1 |
From an MCP client
catalog.call {
"slug": "onchain-sql",
"params": {
"sql": "SELECT COUNT(*) AS txs FROM base.transactions WHERE block_number > 0 LIMIT 1"
}
}
The hosted connector at https://agent402.tools/mcp needs a payment for onchain-sql; the stdio package pays it from a wallet or from AGENT402_CREDITS_KEY. Local install: npx -y agent402-mcp.
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: settlement is cancelled when the tool fails.
- Wallet-only: this tool reaches the network or stored state, so it has no proof-of-work tier. A prepaid card-credits key (
Authorization: Bearer a402_...) also pays it. - A
GETorHEADto /api/onchain-sql 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.
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?.(false); // keep your own spending ceiling in code
registerExactEvmScheme(client, { signer: privateKeyToAccount(KEY) });
const payFetch = wrapFetchWithPayment(fetch, client);
const res = await payFetch("https://agent402.tools/api/onchain-sql", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
"sql": "SELECT COUNT(*) AS txs FROM base.transactions WHERE block_number > 0 LIMIT 1"
}),
});
Part of these workflows
Onchain SQL (query Base with SQL) is one step in this skill pack, each sold as a single call:
- Onchain analyst (SQL over Base) - Ask Base anything in one paid call: your SQL runs against Coinbase's indexed, DECODED chain data (events with parsed parameters, transactions, blocks, user-ops) with the live schema alongside - no indexer, no RPC archaeology.
Related tools
Onchain SQL schema
GET /api/onchain-sql-schemaThe table + column schema for the onchain-sql tool - every queryable table (base.events, base.transactions, base.blocks,…
Wallet token balances (indexed)
GET /api/wallet-balancesAll token balances for any address in one call, from Coinbase's indexed data API - ERC-20 + native on EVM, SPL on Solana…
Onramp link (fund a wallet with a card)
POST /api/onramp-linkGenerate a single-use Coinbase Onramp URL that lets a human fund any wallet with a card or Apple Pay - the fastest way t…
Testnet faucet (try x402 free)
POST /api/testnet-fundFund any address with testnet money via the Coinbase faucet - USDC (1) or ETH (0.0001) on Base Sepolia, USDC (1) or SOL …
Wallet token transfers (filtered)
POST /api/asset-transfersRecent token transfers for a wallet on Base, Ethereum and other EVM chains, newest first. Filtered transfer log for an E…
Attest a settled call on Base
POST /api/attestWrite an on-chain attestation (Ethereum Attestation Service on Base) that binds a call you paid for to the bytes you rec…