What It Demonstrates
- Mounting the Key0 payment gateway on a Hono app with
key0App - Defining multiple pricing plans with different prices and access durations
- Issuing JWTs with plan-specific TTLs via
AccessTokenIssuer - Protecting downstream routes with
honoValidateAccessTokenmiddleware
Architecture
Prerequisites
- Bun v1.0+
- A wallet address to receive USDC payments
Key Files
The entire example is a single file:Code Walkthrough
1. Imports and configuration
@key0ai/key0/hono subpath export provides Hono-specific helpers.
2. Payment adapter and token issuer
X402Adapter handles on-chain verification of USDC transfers on Base. AccessTokenIssuer signs and verifies JWTs using the shared secret.
3. Payment gateway
key0App returns a Hono sub-app that mounts the A2A agent card at /.well-known/agent.json and the x402 payment endpoint at POST /x402/access. The plans array defines two tiers — a single photo for $0.10 and a full album for $1.00.
4. Token issuance
fetchResourceCredentials runs after payment verification succeeds, minting a JWT with a plan-appropriate lifetime — 1 hour for single photos, 24 hours for full albums.
5. Payment lifecycle hook
The optionalonPaymentReceived callback fires after a payment is verified and a credential is issued. Use it for logging, analytics, or notifying downstream systems:
6. Protecting routes with JWT middleware
/api sub-router is gated by honoValidateAccessToken. Any request without a valid Bearer token receives a 401. The middleware is independent of the payment flow — it only verifies JWTs signed by the same secret.
7. Server export
serve() call needed.
Running the Example
Expected Output
Verify the agent card
Complete a payment flow
Run an agent against this seller to execute a full discovery, payment, and resource access cycle. When a payment completes, you will see:Hono vs Express
The Key0 integration surface is nearly identical across frameworks. The main differences:| Hono | Express | |
|---|---|---|
| Gateway mount | key0App() returns a Hono sub-app | key0Router() returns an Express router |
| Token middleware | honoValidateAccessToken() | validateAccessToken() |
| Import path | @key0ai/key0/hono | @key0ai/key0/express |
| Server | export default { fetch } | app.listen(port) |
Source code
examples/hono-seller/server.ts
