x402 Integration Guide

x402 is an HTTP 402 payment protocol. The irsb-x402 package bridges HTTP payments to on-chain IRSB receipts, providing proof of service delivery for paid API calls.

The irsb-x402 package is published. Receipt signing uses Cloud KMS. Buyer-side payments use EIP-7702 delegated signing via X402Facilitator.

How It Works

Client sends HTTP request with x402 payment | v Server verifies payment, executes request | v irsb-x402 middleware builds V2 receipt - Links payment hash to intent hash - Stores response hash as evidence - Sets privacy level | v Receipt posted to IntentReceiptHub on Sepolia | v Client can verify service delivery on-chain

Installation

bash
npm install irsb-x402 irsb

Server-Side: Express Example

After verifying an x402 payment, build and post a receipt linking the payment to the service response.

typescript
import { buildReceiptV2FromX402, postReceiptV2FromX402 } from 'irsb-x402';
import { IRSBClient } from 'irsb';

const irsbClient = new IRSBClient({
  chainId: 11155111,
  rpcUrl: process.env.RPC_URL,
});

app.post('/api/inference', async (req, res) => {
  // 1. Verify x402 payment (your existing logic)
  const payment = verifyX402Payment(req);

  // 2. Execute the service
  const startTime = Date.now();
  const result = await runInference(req.body);
  const endTime = Date.now();

  // 3. Build IRSB receipt from x402 context
  const receipt = buildReceiptV2FromX402({
    payload: {
      service: 'inference-api',
      payment: payment,
      request: req.body,
      response: result,
      timing: { startTime, endTime },
    },
    ciphertextPointer: resultCID, // IPFS CID of full response
    solverId: process.env.SOLVER_ID,
  });

  // 4. Post receipt on-chain
  await postReceiptV2FromX402(irsbClient, receipt, solverSigner);

  // 5. Return response with receipt reference
  res.json({
    result: result,
    receipt: receipt.receiptId,
  });
});

Client-Side: Verifying Delivery

typescript
import { IRSBClient } from 'irsb';

// After receiving response with receipt ID
const client = new IRSBClient({ chainId: 11155111, rpcUrl });

const receipt = await client.getReceipt(receiptId);
const isValid = await client.verifyReceipt(receiptId);

console.log('Service delivery verified:', isValid);
console.log('Evidence hash:', receipt.evidenceHash);
console.log('Timestamp:', receipt.createdAt);

Receipt Field Mapping

How x402 payment fields map to IRSB receipt fields:

x402 FieldReceipt FieldDerivation
payment hashintentHashkeccak256(payment + request)
service constraintsconstraintsHashkeccak256(SLA terms)
execution pathrouteHashkeccak256(service endpoint)
responseoutcomeHashkeccak256(response body)
full response CIDciphertextPointerIPFS CID of response
timing dataevidenceHashkeccak256(timing + metadata)