Query The Graph's gateway with automatic payment handling using the x402 protocol, no API key required!
npm install @graphprotocol/client-x402| Environment | Gateway endpoint | Payment network |
|---|---|---|
production |
https://gateway.thegraph.com/api/x402 |
base |
testnet |
https://testnet.gateway.thegraph.com/api/x402 |
base-sepolia |
Three ways to use this package, from simplest to most complete:
For quick queries, testing, or shell scripts. No code required.
export X402_PRIVATE_KEY=0xabc123...
graphclient-x402 "{ pairs(first: 5) { id } }" \
--endpoint https://gateway.thegraph.com/api/x402/subgraphs/id/<SUBGRAPH_ID> \
--chain baseFor scripts, bots, or simple integrations. No build step, no types.
import { createGraphQuery } from '@graphprotocol/client-x402'
const query = createGraphQuery({
endpoint: 'https://gateway.thegraph.com/api/x402/subgraphs/id/<SUBGRAPH_ID>',
chain: 'base',
})
const result = await query('{ pairs(first: 5) { id } }')
console.log(result.data)For applications that want full type safety. Uses the @graphprotocol/client-cli build workflow to generate a typed SDK from your schema.
Install:
npm install @graphprotocol/client-cli @graphprotocol/client-x402Configure .graphclientrc.yml:
customFetch: '@graphprotocol/client-x402'
sources:
- name: uniswap
handler:
graphql:
endpoint: https://gateway.thegraph.com/api/x402/subgraphs/id/<SUBGRAPH_ID>
documents:
- ./src/queries/*.graphqlDefine your queries in src/queries/pairs.graphql:
query GetPairs($first: Int!) {
pairs(first: $first) {
id
token0 {
symbol
}
token1 {
symbol
}
}
}Build:
export X402_PRIVATE_KEY=0xabc123...
export X402_CHAIN=base
graphclient build
rm .graphclient/package.json # Required: mesh generates broken ESM exportsUse with full type safety:
import { execute, GetPairsDocument } from './.graphclient'
const result = await execute(GetPairsDocument, { first: 5 })
// ^ fully typed based on your schema and queryAll configuration is via environment variables:
export X402_PRIVATE_KEY=0x... # Required: private key for signing payments
export X402_CHAIN=base # Optional: "base" (default) or "base-sepolia"Required for signing payment permits. Can also be provided via:
privateKeyoption increateGraphQuery()(Mode 2)config.x402PrivateKeyinexecute()context (Mode 3)
The payment chain can also be specified via:
chainoption increateGraphQuery()(Mode 2)--chainflag in CLI (Mode 1)