Overview
The @1stdigital/prism-express package provides Express.js middleware for payment-protecting your API routes using the x402 protocol. It’s the reference implementation for the Prism SDK and offers the most straightforward integration.Simple Setup
Single middleware function, 5 lines of code
Route Matching
Exact paths, wildcards, and patterns
Payment Info
Access payer address in req.payer
Installation
- npm
- yarn
- pnpm
Quick Start
Configuration
Middleware Configuration
Route Configuration
Route Protection Patterns
Exact Path Matching
Wildcard Matching
Multiple Route Groups
Dynamic Pricing (per-route middleware)
Accessing Payment Information
The middleware adds payment information to the request object:Settlement Validation
The Express middleware uses res.end() interception to validate settlement before sending data:- Works with
res.json(),res.send(),res.sendFile(), etc. - Settlement happens before data reaches the client
- Original response is replaced with 402 error if settlement fails
Error Handling
Payment Errors
When a request lacks valid payment, the middleware returns 402 Payment Required:Gateway Errors
If the Prism Gateway is unreachable or returns an error:traceId when contacting support!
Settlement Errors
If payment verification succeeds but settlement fails:Custom Error Handling
You can add Express error handlers after the Prism middleware:Testing
Unit Testing with Mocks
Integration Testing
Production Deployment
Environment Variables
Production Configuration
Monitoring & Logging
Rate Limiting
Combine with rate limiting to prevent abuse:TypeScript Support
Full TypeScript support with type definitions:API Reference
prismPaymentMiddleware(config, routes)
Creates Express middleware for payment protection.
Parameters:
config: PrismMiddlewareConfig- SDK configurationroutes: Record<string, RoutePaymentConfig>- Protected routes
express.RequestHandler
PrismMiddlewareConfig
RoutePaymentConfig
Request Augmentation
Examples
AI Agent API
Content Paywall
Troubleshooting
402 errors even with valid payment
402 errors even with valid payment
Check:
- Payment signature is valid (use correct private key)
- Payment hasn’t expired (
validBeforetimestamp) - Nonce hasn’t been used before (replay protection)
- Network matches (
eth-sepoliavseth-mainnet) - Amount matches exactly (don’t modify
amountfield)
Settlement fails but payment was valid
Settlement fails but payment was valid
Possible causes: 1. Insufficient balance in sender’s wallet 2. Token
allowance not set for USDC contract 3. Network congestion (transaction
timeout) 4. Gateway blockchain RPC node down Solution: Client should retry
after fixing the issue.
Middleware not intercepting routes
Middleware not intercepting routes
Check order:
javascript // ❌ Wrong order app.get('/api/premium', handler); // Handler registered first app.use(prismPaymentMiddleware(config, routes)); // Middleware too late // ✅ Correct order app.use(prismPaymentMiddleware(config, routes)); // Middleware first app.get('/api/premium', handler); // Handler after Type errors in TypeScript
Type errors in TypeScript
Install type definitions:Import types: