Skip to main content
A complete Express server that charges 0.01perweatherqueryand0.01 per weather query and 0.005 per joke — settled on-chain every time a route is called. No long-lived token is issued; the route handler runs normally after payment. Source: examples/ppr-embedded

What it demonstrates

  • Defining top-level routes for pay-per-call pricing
  • Using key0.payPerRequest(routeId, opts?) middleware to gate individual routes
  • Accessing req.key0Payment (containing txHash, planId, amount, challengeId) in route handlers
  • Discovery via GET /discover and agent card showing per-request skills with route metadata
  • Automatic PENDING → PAID → DELIVERED state transitions with refund safety (pass a store to enable)

Architecture

Key differences from subscription mode

Running locally

Prerequisites

  • Bun v1.3+ or Node 18+
  • Redis running locally
  • A wallet address on Base Sepolia
  • A gas wallet private key (ETH-funded) for self-contained settlement

Setup

Server starts on http://localhost:3000.

Environment variables

Code walkthrough

1. Define top-level pay-per-call routes

2. Mount key0Router and gate routes

3. Discovery response

Agents call GET /discover and see pay-per-call routes directly:
The agent card at /.well-known/agent.json includes these as route skills.

Testing with curl

Extending the example

  • Add more routes with different plans
  • Use req.key0Payment.challengeId for your own audit records
  • Pass store to key0.payPerRequest() to enable refund safety on per-request routes (when settlement succeeds but the handler crashes)
  • Combine with subscription plans on the same server — agents can use both flows

Next Steps

PPR Standalone Example

Key0 as a gateway that proxies to a separate backend service.

Express Integration

Full reference for key0Router and payPerRequest options.

SellerConfig Reference

Routes, plans, and ProxyToConfig type definitions.

Building a Seller

End-to-end guide from install to production.