Overview
The Key0 SDK is organized into five distinct layers insidesrc/. Each layer has a single responsibility and depends only on the layers below it. Types sit at the bottom (no runtime logic), core business logic builds on top, and integrations sit at the top — wiring everything into your framework of choice.
Directory Tree
Core Layers
The SDK is composed of five layers. Dependencies flow downward only.1. Types
All interfaces and message types live insrc/types/. This layer contains zero runtime logic — it defines the contracts that every other layer depends on. Key interfaces include IPaymentAdapter, IChallengeStore, ISeenTxStore, and IAuditStore.
2. Core
Business logic that is independent of any HTTP framework or blockchain client. TheChallengeEngine owns the full payment lifecycle state machine. AccessTokenIssuer handles JWT creation and verification. The storage/ subdirectory provides Redis and Postgres implementations of the store interfaces.
3. Adapter
On-chain interaction throughX402Adapter, which implements IPaymentAdapter. The adapter verifies ERC-20 Transfer events on Base (mainnet chain ID 8453, testnet chain ID 84532) using viem. It also provides sendUsdc for processing refunds.
4. Integrations
Framework-specific adapters that mount the challenge and proof endpoints. Each integration exports a router/plugin and avalidateAccessToken middleware for protecting downstream routes. The settlement.ts module handles on-chain settlement through either the Coinbase facilitator or a gas wallet.
5. Helpers
Auth strategies for outbound requests from client agents (noAuth, sharedSecretAuth, signedJwtAuth, oauthClientCredentialsAuth) and createRemoteTokenIssuer for delegating token issuance to a remote Key0 instance.
Entry Points
The SDK exposes framework-specific subpath imports so you only pull in the dependencies you need.| Import path | Exports |
|---|---|
@key0ai/key0 | Everything — types, core, adapter, helpers, factory |
@key0ai/key0/express | key0Router, validateAccessToken |
@key0ai/key0/hono | key0App, honoValidateAccessToken |
@key0ai/key0/fastify | key0Plugin, fastifyValidateAccessToken |
@key0ai/key0/mcp | createMcpServer, mountMcpRoutes |
src/index.ts) re-exports from all layers. Framework subpaths are defined in the exports field of package.json and resolve to the corresponding file under src/integrations/.
Key Files
| File | Purpose |
|---|---|
src/core/challenge-engine.ts | State machine governing the full challenge lifecycle (PENDING, PAID, DELIVERED, EXPIRED, CANCELLED, REFUNDED) |
src/core/access-token.ts | JWT issuance and verification with HS256/RS256 support and fallback secrets for zero-downtime rotation |
src/adapter/verify-transfer.ts | On-chain verification of ERC-20 Transfer events — runs 6 checks (tx status, log presence, token address, recipient, sender, amount) |
src/types/config.ts | SellerConfig definition — the single configuration object that drives the entire SDK |
src/types/storage.ts | Store interfaces (IChallengeStore, ISeenTxStore, IAuditStore) that all storage backends implement |
src/factory.ts | createKey0() — the top-level factory that wires adapter, engine, stores, and integration together |
src/executor.ts | Key0Executor — implements the A2A AgentExecutor interface for native agent-to-agent payment flows |
src/integrations/settlement.ts | Settlement logic for both facilitator-based (EIP-3009) and gas wallet (direct transfer) strategies |
src/integrations/x402-http-middleware.ts | x402 HTTP middleware that intercepts requests, issues challenges, and processes payment headers |
src/utils/gas-wallet-lock.ts | Distributed locking for gas wallet settlement — Redis-based across replicas, in-process queue for single instances |

