Quickstart, local
You run a server that charges 0.002 fUSD for POST /answer and a client that pays it, in one file. An in-memory chain
stands in for QoreChain: it holds both key registries and an escrow, and it checks every signature with real
ML-DSA-87. Sealing uses real ML-KEM-1024. Nothing touches a network. CI runs this exact file on every push.
-
Make a project and install the packages:
Terminal window mkdir pq402-quickstart && cd pq402-quickstartpnpm init && pnpm pkg set type=modulepnpm add @pq402/client @pq402/server @pq402/scheme-qorechain @pq402/core @x402/core@2.31.0pnpm add -D tsxTerminal window mkdir pq402-quickstart && cd pq402-quickstartnpm init -y && npm pkg set type=modulenpm install @pq402/client @pq402/server @pq402/scheme-qorechain @pq402/core @x402/core@2.31.0npm install -D tsxTerminal window mkdir pq402-quickstart && cd pq402-quickstartyarn init -y && npm pkg set type=moduleyarn add @pq402/client @pq402/server @pq402/scheme-qorechain @pq402/core @x402/core@2.31.0yarn add -D tsx -
Save this as
quickstart.ts:quickstart.ts /*** The local quickstart: a paid, sealed endpoint and a client that pays it, in* one process, against an in-memory chain. No network, no keys to fund.** npx tsx quickstart.ts*/import { createServer } from "node:http";import type { AddressInfo } from "node:net";import { createPq402Fetch } from "@pq402/client";import { generateSealKeys, mldsaKeyPair, type Hex } from "@pq402/core";import { MemoryChain, PqErc20Facilitator, qorechainTestnet } from "@pq402/scheme-qorechain";import { PaymentGate, nodeListener, type Handler } from "@pq402/server";import { x402Facilitator } from "@x402/core/facilitator";// 1. Two accounts, each with an ML-DSA-87 signing key and an ML-KEM-1024 sealing key.const server = {address: "0x00000000000000000000000000000000000000a1" as Hex,mldsa: mldsaKeyPair(),seal: generateSealKeys(),};const client = {address: "0x00000000000000000000000000000000000000b2" as Hex,mldsa: mldsaKeyPair(),seal: generateSealKeys(),};const escrow: Hex = "0x00000000000000000000000000000000000000e5";// 2. An in-memory chain stands in for QoreChain: both key registries and the escrow.const chain = new MemoryChain(escrow);for (const account of [server, client]) {chain.registerKey(account.address, account.mldsa.publicKey);chain.registerKemKey(account.address, account.seal.mlkemPublicKey);}chain.deposit(client.address, 1_000_000n); // 1 fUSD (6 decimals) in the escrow// 3. The server: one paid route, 0.002 fUSD a call, request body sealed.const networks = [qorechainTestnet.network];const facilitator = new x402Facilitator().register(qorechainTestnet.network,new PqErc20Facilitator({ networks, reader: chain, settler: chain.settler(server.address) }),);const gate = new PaymentGate({network: qorechainTestnet,escrow,facilitator,kemKeys: chain,gateway: { address: server.address, mldsaSecretKey: server.mldsa.secretKey, sealKeys: server.seal },routes: {"POST /answer": { price: "0.002 fUSD", payTo: server.address, requestSealing: "required" },},});const answer: Handler = async (req) => ({status: 200,contentType: "text/plain",body: new TextEncoder().encode(`You asked: ${new TextDecoder().decode(req.body)} The answer is 42.`),});const http = createServer(nodeListener(gate, answer));await new Promise<void>((resolve) => http.listen(0, "127.0.0.1", resolve));const url = `http://127.0.0.1:${(http.address() as AddressInfo).port}/answer`;// 4. Without paying, the route answers 402 with a price.const unpaid = await fetch(url, { method: "POST" });console.log(`without payment: ${unpaid.status} ${unpaid.statusText}`);// 5. The client: a fetch that pays, seals the question and opens the answer.const pay = createPq402Fetch({payer: client.address,mldsaSecretKey: client.mldsa.secretKey,sealKeys: client.seal,keys: chain,chainId: qorechainTestnet.chainId,maxAmount: 5_000n, // never pay more than 0.005 fUSD for one call});const res = await pay(url, {method: "POST",headers: { "content-type": "text/plain" },body: "What is 6 x 7?",});console.log(`with payment: ${res.status} ${await res.text()}`);console.log(`paid ${res.payment?.requirements.amount} units with ${res.payment?.requirements.scheme}`);console.log(`request sealed: ${res.payment?.requestSealed}`);console.log(`receipt signed by ${res.payment?.receipt?.gateway}`);console.log(`escrow balance left: ${await chain.escrowBalance(escrow, client.address)}`);http.close(); -
Run it:
Terminal window npx tsx quickstart.tsYou see:
without payment: 402 Payment Requiredwith payment: 200 You asked: What is 6 x 7? The answer is 42.paid 2000 units with pq-erc20request sealed: truereceipt signed by 0x00000000000000000000000000000000000000a1escrow balance left: 998000
What happened
Section titled “What happened”- A plain
fetchgot402 Payment Required, with the price in thePAYMENT-REQUIREDheader. createPq402Fetchsent a body-less probe first, so the question never crossed the wire in plain text. It read the price, sealed the question to the server’s ML-KEM-1024 key, signed a payment of 2,000 units (0.002 fUSD) with the client’s ML-DSA-87 key, and sent both.PaymentGatechecked the signature against the client’s registered key, opened the question, ran your handler, sealed the answer to the client’s ML-KEM-1024 key, settled the payment and signed a receipt.- The client checked the receipt against the server’s registered key, then opened the answer.
The escrow went from 1,000,000 to 998,000 units: one call at 2,000.
- Run the same flow on the real chain: testnet quickstart.
- Put the gate in front of your own app: charge for a route.
- See what each step puts on the wire: wire format.