Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.monkepay.xyz/llms.txt

Use this file to discover all available pages before exploring further.

Installation

npm install @monkepay/sdk express
npm install -D @types/express

Basic usage

import express from 'express'
import { MonkePayExpress } from '@monkepay/sdk'

const app = express()

const monkePay = MonkePayExpress({
  apiKeyId: process.env.MONKEPAY_API_KEY_ID!,
  apiKeySecret: process.env.MONKEPAY_API_KEY_SECRET!,
  price: '0.001',
})

app.get('/api/data', monkePay(), (req, res) => {
  res.json({ result: 'paid content' })
})

app.listen(3000)

Per-route pricing

Pass overrides when calling the factory:
const monkePay = MonkePayExpress({
  apiKeyId: process.env.MONKEPAY_API_KEY_ID!,
  apiKeySecret: process.env.MONKEPAY_API_KEY_SECRET!,
  price: '0.001',
})

app.get('/api/cheap', monkePay({ price: '0.001' }), (req, res) => {
  res.json({ result: 'cheap' })
})

app.get('/api/expensive', monkePay({ price: '0.10' }), (req, res) => {
  res.json({ result: 'expensive' })
})

app.get('/api/report', monkePay({ price: '1.00', paymentMode: 'one_time' }), (req, res) => {
  res.json({ result: 'one-time report' })
})

Gate multiple routes

Use app.use to gate a path prefix:
app.use('/api', monkePay())

app.get('/api/data', handler)
app.get('/api/other', handler)

Error handler

Register an Express error handler after your routes. If an unexpected SDK error occurs after response headers have already been sent, the SDK calls next(error) to hand off to Express’s error handler:
// Must be registered after all routes
app.use((err: Error, req: express.Request, res: express.Response, next: express.NextFunction) => {
  console.error(err)
  if (!res.headersSent) {
    res.status(500).json({ error: 'Internal server error' })
  }
})

Behind a reverse proxy

If your Express app runs behind a reverse proxy (Railway, Render, Nginx), configure trust proxy so Express reads the correct client IP and protocol:
app.set('trust proxy', 1)
This ensures x402 constructs https:// resource URLs correctly when your app is behind TLS termination. See Reverse Proxies for details.