Overview
The@key0ai/key0/fastify subpath exports two pieces:
| Export | Type | Purpose |
|---|---|---|
key0Plugin | Fastify plugin | Registers agent-card and x402 payment routes on your Fastify server |
fastifyValidateAccessToken | onRequest hook | Validates Bearer JWTs and attaches the decoded token |
Installation
Routes
When you registerkey0Plugin, the following routes are added to your Fastify instance:
| Method | Path | Description |
|---|---|---|
| GET | /.well-known/agent.json | Returns the auto-generated A2A agent card |
| GET | /discover | Plan catalog (all plans and pricing) |
| POST | /x402/access | Unified x402 HTTP payment endpoint |
Setup
Configure SellerConfig and storage
Create your
SellerConfig, payment adapter, challenge store, and seen-tx store. These are framework-agnostic and shared across all integrations.Register the Key0 plugin
Pass the config objects as plugin options to After registration,
fastify.register.GET /.well-known/agent.json returns your agent card.Protect routes with access-token validation
Use The hook reads the
fastifyValidateAccessToken as an onRequest hook on any route that requires a paid access token.Authorization: Bearer <jwt> header, verifies the signature, and attaches the decoded payload to request.key0Token. If verification fails, it short-circuits with a 401 or 500 JSON response.Full working example
Accessing the decoded token
ThefastifyValidateAccessToken hook attaches the decoded JWT payload to request.key0Token. Because Fastify’s FastifyRequest type does not include this property by default, you need a type assertion to access it:
sub (request ID), jti (challenge ID), resourceId, planId, and txHash.
Pay-Per-Request Routes
key0Plugin registers the plugin and exposes a payPerRequest(routeId, opts?) factory on the plugin instance. Use it as a Fastify preHandler hook to gate routes behind micro-payments — no JWT issued, payment settles inline.
PAYMENT-SIGNATURE header the hook returns 402 with x402 payment requirements. After settlement, request.key0Payment contains the PaymentInfo object.
For standalone gateway mode, add proxyTo to SellerConfig — see the Express integration for the full proxy configuration reference.
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.
x402 HTTP Flow
Full protocol reference for the payment flow.

