Skip to main content
A lightweight seller agent built with Hono that monetizes a photo gallery API. AI agents discover the seller via its A2A agent card, pay USDC on Base, and receive a JWT to access protected photo endpoints.

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 honoValidateAccessToken middleware

Architecture

Prerequisites

  • Bun v1.0+
  • A wallet address to receive USDC payments
This example omits store and seenTxStore from the key0App options for brevity. In practice, createKey0() (used internally) throws if these are missing — RedisChallengeStore and RedisSeenTxStore (or their Postgres equivalents) are required for the server to start. See the Storage guide for setup instructions.

Key Files

The entire example is a single file:

Code Walkthrough

1. Imports and configuration

All configuration comes from environment variables with safe defaults for local development. The @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 optional onPaymentReceived 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

The /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

Bun picks up the default export automatically. No explicit serve() call needed.

Running the Example

1

Install dependencies

2

Configure environment

Edit .env and set your wallet address:
.env
3

Start the server

Expected Output

Verify the agent card

This returns the auto-generated A2A agent card describing the seller’s capabilities, pricing plans, and payment endpoint URL.

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: For full integration documentation, see the Hono integration guide.

Source code

examples/hono-seller/server.ts