Dev.to AI 🤖 Ai 👁 0 📖 3 min read

Pay for a Telegraph ask from escrow, then prove the charge in the ledger (testnet, Node.js)

An agent that buys data needs two things: a payment path that works, and a way to check afterwards that it was charged what it should have been. Telegraph gives you both on testnet: you deposit once into an escrow, pay e

An agent that buys data needs two things: a payment path that works, and a way to check afterwards that it was charged what it should have been. Telegraph gives you both on testnet: you deposit once into an escrow, pay each ask with a signature, and every charge later appears in a public epoch ledger block. This tutorial walks through that loop in about 15 minutes (plus a wait for the next ledger root, see step 3). No real money.

Why escrow and not only x402 exact? On the devnode I hit settlement failed and facilitator timeouts on the per-request rails (#214, #215), and failed asks on those rails can still settle (#228). On the escrow rail a failed ask was not charged in my runs. The catch is that the signature has to be exactly right, so I packaged that part: telegraph-escrow (MIT, one dependency: viem). It was extracted from Escalation Ledger, my entry for mission 05 (Conflict Intelligence).

What you need

  • Node.js 20+
  • A throwaway wallet with about 0.0005 Sepolia ETH (any Sepolia faucet) for the deposit gas
  • Never a key that holds real funds

1. Install and set a throwaway key

git clone https://github.com/a2taka-sourse/telegraph-escrow && cd telegraph-escrow && npm install
echo "EVM_PRIVATE_KEY=0x<a throwaway testnet key>" > .env     # .env is already in .gitignore
npm test                                                         # 7 offline checks, no network

2. Look before you pay (free)

Discovery costs nothing, and an unpaid ask returns 402 Payment Required, which is the price list.

curl https://devnode.telegraphprotocol.com/engine/v1/intents/NEWS_SEARCH_GENERAL
curl -X POST https://devnode.telegraphprotocol.com/engine/v1/ask -H "Content-Type: application/json" \
  -d '{"intent":"NEWS_SEARCH_GENERAL","params":{"query":"Telegraph protocol testnet"}}'

The 402 lists several ways to pay, including escrow on Ethereum Sepolia at $0.01.

3. Fund the escrow

node bin/cli.mjs fund 5        # mints 5 test USDC, approves, deposits to the escrow Diamond on Ethereum Sepolia
node bin/cli.mjs balance       # wait until deposit_not_yet_in_root is false

A new deposit can show as available while asks are still refused, until the next ledger root is published (about 1.5 hours in my case, #232). Do this step first and come back later.

4. Ask, with a ceiling

node bin/cli.mjs ask '{"intent":"NEWS_SEARCH_GENERAL","params":{"query":"Telegraph protocol testnet"}}' 30000

30000 is max_budget_usdc in micro-USDC: the most you authorise. The node may route to several ranked miners and bills one row per answer used. My real run on 2026-11-24 authorised 30,000 and was charged 10,000:

{ "ok": true, "status": 200, "authorized_uusdc": 30000, "charged_uusdc": 10000,
  "verify": [{ "epoch_id": 48, "receipt_hash": "0x2c71…d992" }] }

The client also refuses to sign anything above maxPerCallUusdc (default 50,000 = $0.05): your first guard rail. In code:

import { ask, verifyDelivery } from "./src/index.mjs";
const r = await ask({ intent: "NEWS_SEARCH_GENERAL", params: { query: "..." }, max_budget_usdc: 30000 },
                    { privateKey: process.env.EVM_PRIVATE_KEY, maxPerCallUusdc: 50000 });
if (!r.ok) console.log("not charged:", r.error);   // never throws on network or payment failure

5. What the signature must look like

This is the part that costs hours by hand; the package does it for you.

  1. Ask once without payment; the 402's escrow option has an extra.message template.
  2. Fill {from} with your address in lowercase, a fresh nonce, and a validity window under 2 minutes.
  3. Sign the filled text with personal_sign (EIP-191), not EIP-712.
  4. Send X-PAYMENT: base64({ x402Version: 2, scheme: "escrow", network, payload: { signature, authorization } }).

6. Verify the charge in the ledger

node bin/cli.mjs verify <epoch_id> <receipt_hash>     # "pending" until the epoch block is recorded

This reads /v1/blocks/{epoch}/deliveries/{receipt_hash} and checks that the recorded row matches your purchase (payer, intent, amount, miner). Limits: the Merkle leaf encoding is not published, so the proof is returned but not recomputed locally (#231). Also check the answer itself: answers from the generic LLM fallback and empty verdicts can still be charged (#233, #234).

Try it

Disclosure: this tutorial is part of my entry to Telegraph Hackathon Season II, which has cash prizes.

📰 Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.