|
| 1 | +# @graphprotocol/client-x402 |
| 2 | + |
| 3 | +Query The Graph's gateway with automatic payment handling using the [x402 protocol](https://github.com/coinbase/x402), no API key required! |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install @graphprotocol/client-x402 |
| 9 | +``` |
| 10 | + |
| 11 | +## Environments |
| 12 | + |
| 13 | +| Environment | Gateway endpoint | Payment network | |
| 14 | +| ------------ | ----------------------------------------------- | --------------- | |
| 15 | +| `production` | `https://gateway.thegraph.com/api/x402` | `base` | |
| 16 | +| `testnet` | `https://testnet.gateway.thegraph.com/api/x402` | `base-sepolia` | |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +Three ways to use this package, from simplest to most complete: |
| 21 | + |
| 22 | +### 1. CLI |
| 23 | + |
| 24 | +For quick queries, testing, or shell scripts. No code required. |
| 25 | + |
| 26 | +```bash |
| 27 | +export X402_PRIVATE_KEY=0xabc123... |
| 28 | + |
| 29 | +graphclient-x402 "{ pairs(first: 5) { id } }" \ |
| 30 | + --endpoint https://gateway.thegraph.com/api/x402/subgraphs/id/<SUBGRAPH_ID> \ |
| 31 | + --chain base |
| 32 | +``` |
| 33 | + |
| 34 | +### 2. Programmatic |
| 35 | + |
| 36 | +For scripts, bots, or simple integrations. No build step, no types. |
| 37 | + |
| 38 | +```typescript |
| 39 | +import { createGraphQuery } from '@graphprotocol/client-x402' |
| 40 | + |
| 41 | +const query = createGraphQuery({ |
| 42 | + endpoint: 'https://gateway.thegraph.com/api/x402/subgraphs/id/<SUBGRAPH_ID>', |
| 43 | + chain: 'base', |
| 44 | +}) |
| 45 | + |
| 46 | +const result = await query('{ pairs(first: 5) { id } }') |
| 47 | +console.log(result.data) |
| 48 | +``` |
| 49 | + |
| 50 | +### 3. Typed SDK |
| 51 | + |
| 52 | +For applications that want full type safety. Uses the `@graphprotocol/client-cli` build workflow to generate a typed SDK from your schema. |
| 53 | + |
| 54 | +**Install:** |
| 55 | + |
| 56 | +```bash |
| 57 | +npm install @graphprotocol/client-cli @graphprotocol/client-x402 |
| 58 | +``` |
| 59 | + |
| 60 | +**Configure `.graphclientrc.yml`:** |
| 61 | + |
| 62 | +```yaml |
| 63 | +customFetch: '@graphprotocol/client-x402' |
| 64 | + |
| 65 | +sources: |
| 66 | + - name: uniswap |
| 67 | + handler: |
| 68 | + graphql: |
| 69 | + endpoint: https://gateway.thegraph.com/api/x402/subgraphs/id/<SUBGRAPH_ID> |
| 70 | + |
| 71 | +documents: |
| 72 | + - ./src/queries/*.graphql |
| 73 | +``` |
| 74 | +
|
| 75 | +**Define your queries in `src/queries/pairs.graphql`:** |
| 76 | + |
| 77 | +```graphql |
| 78 | +query GetPairs($first: Int!) { |
| 79 | + pairs(first: $first) { |
| 80 | + id |
| 81 | + token0 { |
| 82 | + symbol |
| 83 | + } |
| 84 | + token1 { |
| 85 | + symbol |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +**Build:** |
| 92 | + |
| 93 | +```bash |
| 94 | +export X402_PRIVATE_KEY=0xabc123... |
| 95 | +export X402_CHAIN=base |
| 96 | +graphclient build |
| 97 | +rm .graphclient/package.json # Required: mesh generates broken ESM exports |
| 98 | +``` |
| 99 | + |
| 100 | +**Use with full type safety:** |
| 101 | + |
| 102 | +```typescript |
| 103 | +import { execute, GetPairsDocument } from './.graphclient' |
| 104 | +
|
| 105 | +const result = await execute(GetPairsDocument, { first: 5 }) |
| 106 | +// ^ fully typed based on your schema and query |
| 107 | +``` |
| 108 | + |
| 109 | +## Configuration |
| 110 | + |
| 111 | +All configuration is via environment variables: |
| 112 | + |
| 113 | +```bash |
| 114 | +export X402_PRIVATE_KEY=0x... # Required: private key for signing payments |
| 115 | +export X402_CHAIN=base # Optional: "base" (default) or "base-sepolia" |
| 116 | +``` |
| 117 | + |
| 118 | +### Private Key |
| 119 | + |
| 120 | +Required for signing payment permits. Can also be provided via: |
| 121 | + |
| 122 | +- `privateKey` option in `createGraphQuery()` (Mode 2) |
| 123 | +- `config.x402PrivateKey` in `execute()` context (Mode 3) |
| 124 | + |
| 125 | +### Chain |
| 126 | + |
| 127 | +The payment chain can also be specified via: |
| 128 | + |
| 129 | +- `chain` option in `createGraphQuery()` (Mode 2) |
| 130 | +- `--chain` flag in CLI (Mode 1) |
0 commit comments