Skip to main content
@key0ai/key0 is a single package that exposes everything you need to add payment-gated endpoints to your API. Import from the main entry point for core functionality, or use framework-specific subpath imports.

Install

npm install @key0ai/key0

Main Import

Everything below is available from the top-level import:
import { createKey0, ChallengeEngine, X402Adapter /* ... */ } from "@key0ai/key0";

Core Classes

ExportDescription
ChallengeEngineState machine that drives the full challenge lifecycle.
AccessTokenIssuerIssues and verifies JWTs (HS256 or RS256) with secret rotation.
X402AdapterVerifies ERC-20 Transfer events on Base mainnet or Base Sepolia.

Storage

ExportDescription
RedisChallengeStoreRedis-backed challenge store with Lua atomics.
RedisSeenTxStoreRedis-backed seen-transaction store (SET NX).
RedisAuditStoreRedis-backed append-only audit log.
PostgresChallengeStorePostgres-backed challenge store.
PostgresSeenTxStorePostgres-backed seen-transaction store.
PostgresAuditStorePostgres-backed append-only audit log.

Factory

ExportDescription
createKey0Wires stores, adapter, and engine together. Returns { requestHandler, agentCard, engine, executor }.

Executor

ExportDescription
Key0ExecutorImplements the A2A AgentExecutor interface for protocol flow.

Middleware

ExportDescription
validateTokenGeneric token-validation middleware.
validateKey0TokenKey0-specific token-validation middleware.

Auth Helpers

ExportDescription
noAuthSends no auth headers (local dev / trusted networks).
sharedSecretAuthAttaches a shared-secret header for service-to-service auth.
signedJwtAuthSigns outbound requests with a JWT.
oauthClientCredentialsAuthFetches and attaches an OAuth2 client-credentials token.

Remote Helpers

ExportDescription
createRemoteTokenIssuerWraps a remote Key0 endpoint for token issuance.

Pay-Per-Request Helpers

ExportDescription
resolveConfigFetchResourceReturns the configured fetchResource callback (from fetchResource or proxyTo) or undefined if embedded mode. Used internally to detect the deployment mode at runtime.

Error Types

ExportDescription
Key0ErrorBase error class for all Key0 errors.

Constants

ExportDescription
CHAIN_CONFIGSNetwork configuration map (mainnet chain 8453, testnet chain 84532).
USDC_DECIMALSUSDC token decimal precision (6).

Subpath Imports

Framework-specific adapters are available as subpath imports. Each exposes a router/plugin and a validateAccessToken middleware for protecting routes.

Express

import { key0Router, validateAccessToken, key0PayPerRequest } from "@key0ai/key0/express";
ExportDescription
key0RouterExpress router that mounts challenge and proof endpoints. Returns the router with an attached payPerRequest(planId, opts?) factory method.
validateAccessTokenExpress middleware that validates Bearer JWTs on protected routes.
key0PayPerRequestStandalone per-request middleware factory for routes not managed by key0Router.

Hono

import { key0App, honoValidateAccessToken, honoPayPerRequest } from "@key0ai/key0/hono";
ExportDescription
key0AppHono app that mounts challenge and proof endpoints. Returns the app with an attached payPerRequest(planId, opts?) factory method.
honoValidateAccessTokenHono middleware that validates Bearer JWTs on protected routes.
honoPayPerRequestStandalone per-request middleware factory for routes not managed by key0App.

Fastify

import { key0Plugin, fastifyValidateAccessToken, fastifyPayPerRequest } from "@key0ai/key0/fastify";
ExportDescription
key0PluginFastify plugin that registers challenge and proof routes. Attaches payPerRequest(planId, opts?) via the key0 Fastify decorator.
fastifyValidateAccessTokenFastify hook that validates Bearer JWTs on protected routes.
fastifyPayPerRequestStandalone per-request preHandler factory for routes not managed by key0Plugin.

MCP

import { createMcpServer, mountMcpRoutes } from "@key0ai/key0/mcp";
ExportDescription
createMcpServerCreates an MCP server exposing discover_plans and request_access tools.
mountMcpRoutesMounts /.well-known/mcp.json discovery and POST /mcp Streamable HTTP endpoints on an Express app.