Skip to main content

Installation

Quick start

1

Create the Express app and dependencies

2

Mount the Key0 router

3

Protect your routes

key0Router(opts)

Creates an Express Router that serves agent discovery and the unified x402 payment endpoint.

Options

Routes served

The router mounts the following routes on your Express app: When mcp: true is set in config, four additional routes are mounted:

The /x402/access endpoint

This endpoint handles the full x402 payment flow over plain HTTP. It responds differently depending on the request body and headers:
The requestId field is optional. If you omit it, the server generates one automatically. Include it when you need client-side idempotency.

Pay-Per-Request Routes

key0Router returns the router and a payPerRequest(planId, opts?) factory. Use it to gate individual routes behind micro-payments — no JWT issued, payment settles inline.

Embedded mode (route inside your app)

Without a PAYMENT-SIGNATURE header the middleware returns 402 with x402 payment requirements. After settlement, req.key0Payment contains the PaymentInfo object.

Standalone mode (proxy via /x402/access)

Add proxyTo to SellerConfig. No separate route registrations needed — Key0 auto-mounts the configured routes.
Client request (HTTP):
Server returns ResourceResponse (the backend’s response body, not a JWT):

payPerRequest options

validateAccessToken(config)

Express middleware that verifies the Bearer JWT on incoming requests and attaches the decoded payload to req.key0Token.
The middleware returns a 401 or 403 JSON error if the token is missing, expired, or invalid.

Enabling MCP mode

Set mcp: true in your SellerConfig to expose MCP (Model Context Protocol) routes alongside the standard x402 endpoint:
This adds /.well-known/mcp.json for discovery and POST /mcp for Streamable HTTP transport, exposing discover_plans and request_access as MCP tools.
MCP mode requires the @modelcontextprotocol/sdk peer dependency. Install it separately if you enable this option.
See the MCP protocol guide for details on how agents interact with these endpoints.

Full example

A complete working example is available at examples/express-seller. To run it locally:

Next Steps

SellerConfig Reference

Complete reference for every configuration option.

Paying for Access

How clients discover, pay, and call your protected endpoints.

Hono Integration

Key0 as a Hono sub-app.

MCP Protocol

Expose Key0 plans as MCP tools for Claude Code.