What it demonstrates
- Using
validateKey0Token()as a lightweight standalone validator — no middleware or framework integration required - Dual token validation: Key0 JWTs (native mode) with automatic fallback to custom API keys (remote mode)
- Internal service-to-service endpoints that the Key0 standalone service calls during payment
- Shared-secret authentication for internal endpoint security
Architecture
Internal endpoints reference
These endpoints are called by the Key0 standalone service, not by agents directly. They must be secured with a shared secret via theX-Internal-Auth header.
/internal/issue-token is only called when Key0 is configured with tokenMode: "remote". In native mode, Key0 issues JWTs directly and this endpoint is never hit.Code walkthrough
1. Imports and configuration
server.ts
KEY0_ACCESS_TOKEN_SECRET— shared with the Key0 service so your backend can verify the JWTs it issues.INTERNAL_AUTH_SECRET— a separate secret for authenticating service-to-service calls from Key0 to your internal endpoints.
2. Internal auth middleware
server.ts
3. Custom token issuance (remote mode)
server.ts
AccessGrant.
The response must include
token (the credential string) and tokenType (typically "Bearer"). expiresAt is optional but recommended so agents know when to refresh.4. Payment notification webhook
server.ts
AccessGrant including the transaction hash and block explorer URL.
5. Dual token validation
server.ts
- Native mode —
validateKey0Tokenverifies the JWT signature and expiry. If valid, the decodedAccessTokenPayloadis attached to the request. - Remote mode — If JWT validation fails, the middleware checks the
Authorizationheader against the in-memory API key store. Expired keys are rejected.
Authorization: Bearer <token> and the middleware resolves it.
Environment variables
Running the example
1
Clone and install
2
Configure environment
Create a
.env file with both secrets:.env
3
Start the Key0 standalone service
The backend integration requires the Key0 standalone service running alongside it. See the Standalone quickstart for setup instructions. Make sure its
INTERNAL_AUTH_SECRET and KEY0_ACCESS_TOKEN_SECRET match the values in your backend .env.4
Start the backend
Expected output
Verify the health check
Test token validation
Once you have a token from the Key0 service (via a direct A2A call), use it to access protected endpoints:Related
Standalone quickstart
Set up the Key0 standalone service that calls your backend’s internal endpoints.
Middleware reference
Full API reference for
validateKey0Token, validateAccessToken, and framework-specific middleware.Source code
View
examples/backend-integration on GitHub
