Skip to main content

Package Overview

@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.

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 } from "@key0ai/key0/express";
ExportDescription
key0RouterExpress router that mounts challenge and proof endpoints.
validateAccessTokenExpress middleware that validates Bearer JWTs on protected routes.

Hono

import { key0App, honoValidateAccessToken } from "@key0ai/key0/hono";
ExportDescription
key0AppHono app that mounts challenge and proof endpoints.
honoValidateAccessTokenHono middleware that validates Bearer JWTs on protected routes.

Fastify

import { key0Plugin, fastifyValidateAccessToken } from "@key0ai/key0/fastify";
ExportDescription
key0PluginFastify plugin that registers challenge and proof routes.
fastifyValidateAccessTokenFastify hook that validates Bearer JWTs on protected routes.

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.