Skip to main content

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


The canonical SDK config is identifyToken (PRISM_IDENTIFY_TOKEN env var) — formerly apiKey / PRISM_API_KEY. SDKs accept the legacy names as fallback during the migration window; new code should use the canonical names.

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:
Key Points:
  • 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
See Stablecoin Settlement for details.

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:
Include the 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 configuration
  • routes: Record<string, RoutePaymentConfig> - Protected routes
Returns: express.RequestHandler

PrismMiddlewareConfig


RoutePaymentConfig


Request Augmentation


Examples

AI Agent API


Content Paywall


Troubleshooting

Check:
  1. Payment signature is valid (use correct private key)
  2. Payment hasn’t expired (validBefore timestamp)
  3. Nonce hasn’t been used before (replay protection)
  4. Network matches (eth-sepolia vs eth-mainnet)
  5. Amount matches exactly (don’t modify amount field)
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.
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
Install type definitions:
Import types:
Last modified on May 21, 2026