Blockchain micropayment middleware for Tyk API Gateway implementing the X402 Payment Protocol
x402-tyk-middleware is an early-stage TypeScript project in the AI payments / x402 ecosystem. It currently has 0 GitHub stars and 0 forks.
Blockchain micropayment middleware for Tyk API Gateway implementing the X402 Payment Protocol
This middleware adds native blockchain payment support to Tyk API Gateway using the HTTP 402 Payment Required standard. It enables pay-per-use pricing models with automatic payment verification and on-chain settlement.
Copy middleware file to Tyk:
cp middleware/x402_payment.js /opt/tyk-gateway/middleware/
Configure API definition:
{
"name": "My Protected API",
"api_id": "protected-api",
"listen_path": "/api/",
"target_url": "http://upstream-service:8000",
"custom_middleware": {
"driver": "otto",
"pre": [{
"name": "x402_payment",
"path": "/opt/tyk-gateway/middleware/x402_payment.js",
"require_session": false,
"raw_body_only": true
}],
"post": [{
"name": "x402_payment",
"path": "/opt/tyk-gateway/middleware/x402_payment.js",
"require_session": false
}]
},
"config_data": {
"x402": {
"network": "solana-devnet",
"payTo": "YOUR_WALLET_ADDRESS",
"asset": "TOKEN_MINT_ADDRESS",
"maxAmountRequired": "100",
"feePayer": "FEE_PAYER_ADDRESS"
}
}
}
Set environment variables:
export FACILITATOR_URL="https://facilitator.emfi.dev"
export FACILITATOR_TIMEOUT="5000"
Reload Tyk Gateway:
docker compose restart tyk-gateway
# or
systemctl restart tyk-gateway
# Step 1: Request without payment (get requirements)
curl -i http://localhost:8080/api/data
# Response: 402 Payment Required
# {
# "error": "Payment Required",
# "paymentRequirements": {
# "network": "solana-devnet",
# "recipient": "YOUR_WALLET_ADDRESS",
# ...
# }
# }
# Step 2: Request with payment (using X402 client SDK)
curl -i http://localhost:8080/api/data \
-H "X-Payment: {\"network\":\"solana-devnet\",\"transaction\":\"...\"}"
# Response: 200 OK with content
┌─────────┐ ┌──────────────┐ ┌──────────┐
│ Client │ │ Tyk Gateway │ │ Upstream │
│ │ │ + Middleware │ │ │
└────┬────┘ └──────┬───────┘ └────┬─────┘
│ │ │
│ 1. GET /api/data │ │
├───────────────────────────>│ │
│ │ │
│ 2. 402 Payment Required │ │
│<───────────────────────────┤ │
│ (payment requirements) │ │
│ │ │
│ 3. GET /api/data │ │
│ X-Payment: {...} │ │
├───────────────────────────>│ │
│ │ │
│ │ 4. Verify with │
│ │ Facilitator │
│ │ ┌──────────────┐ │
│ ├─>│ Facilitator │ │
│ │<─┤ │ │
│ │ └──────────────┘ │
│ │ │
│ │ 5. Proxy request │
│ ├──────────────────────────>│
│ │ │
│ │ 6. Response │
│ │<──────────────────────────┤
│ │ │
│ 7. 200 OK + Content │ │
│<───────────────────────────┤ │
│ │ │
│ │ 8. Settle (async) │
│ │ ┌──────────────┐ │
│ ├─>│ Blockchain │ │
│ │ └──────────────┘ │
│ │ │
X-Payment header from requestX-Payment-Valid header){
"config_data": {
"x402": {
"network": "solana-devnet", // Blockchain network
"payTo": "8xKzG4...", // Your wallet address
"asset": "4zMMC9...", // Token mint address
"maxAmountRequired": "100", // Cost in token base units
"feePayer": "4ALzei...", // Optional fee payer
"scheme": "exact", // Optional: exact|max|dynamic
"description": "Access to API" // Optional description
}
}
}
| Variable | Required | Default | Description |
|---|---|---|---|
FACILITATOR_URL |
Yes | https://facilitator.emfi.dev |
X402 Facilitator service URL |
FACILITATOR_TIMEOUT |
No | 5000 |
Timeout for facilitator calls (ms) |
Each protected route can have custom payment requirements:
| Field | Type | Description | Example |
|---|---|---|---|
network |
string | Blockchain network | "solana-devnet" |
scheme |
string | Payment scheme | "exact" |
payTo |
string | Recipient address | "4ALzeix..." |
feePayer |
string | Fee payer address | "4ALzeix..." |
asset |
string | Token mint address | "4zMMC9s..." |
maxAmountRequired |
string | Payment amount | "100" |
description |
string | Human-readable description | "Premium API access" |
Each API definition can have different pricing:
// API 1: Bitcoin endpoint - 100 tokens
{
"api_id": "bitcoin-api",
"listen_path": "/market/crypto/bitcoin",
"config_data": {
"x402": {
"maxAmountRequired": "100"
}
}
}
// API 2: Stocks endpoint - 200 tokens
{
"api_id": "stocks-api",
"listen_path": "/market/stocks",
"config_data": {
"x402": {
"maxAmountRequired": "200"
}
}
}
See the examples/ directory for complete API definitions:
bitcoin-price-api.json - Basic setupmulti-endpoint.json - Multiple endpointsproduction.json - Production configurationClients need to include the X402 payment proof in the request header:
const response = await axios.get('/protected/resource', {
headers: {
'X-Payment-x402': JSON.stringify({
network: 'solana-devnet',
transaction: 'BASE64_ENCODED_TRANSACTION',
signature: 'SIGNATURE',
payer: 'PAYER_ADDRESS',
// ... other payment fields
})
}
});
For automatic payment handling, use the X402 Client SDK.
| Code | Status | Description |
|---|---|---|
200 |
Success | Payment valid, content delivered |
402 |
Payment Required | No payment or invalid payment |
500 |
Internal Server Error | Middleware error (check logs) |
When payment is required:
{
"error": "Payment Required",
"message": "This resource requires a valid X402 payment",
"x402Version": 1,
"paymentRequirements": {
"scheme": "exact",
"network": "solana-devnet",
"description": "Access to /market/crypto/bitcoin",
"payTo": "4ALzeix...",
"asset": "4zMMC9s...",
"maxAmountRequired": "100",
"maxTimeoutSeconds": 60
},
"instructions": {
"step1": "Sign a transaction with your wallet",
"step2": "Include the signed payment object in the X-Payment-x402 header",
"headerExample": "X-Payment-x402: { x402PaymentObject }"
}
}
x402-tyk-middleware/
├── middleware/
│ ├── x402_payment.js # Main middleware
├── examples/
│ ├── bitcoin-price-api.json # Example API definitions
│ ├── stocks-api.json
│ └── posts-api.json
├── tests/
│ ├── integration/ # Integration tests
│ ├── unit/ # Unit tests
│ └── README.md
└── README.md
# Install dependencies
cd tests
npm install
# Run all tests
npm test
# Run specific test suite
npm run test:unit
npm run test:integration
npm run test:blockchain
# With coverage
npm run test:coverage
See tests/README.md for detailed testing documentation.
# Start Tyk Gateway with Docker
docker compose -f docker-compose.gateway.yml up -d
# Mount middleware directory
docker compose -f docker-compose.gateway.yml \
-v $(pwd)/middleware:/opt/tyk-gateway/middleware \
up -d
# View logs
docker compose logs -f tyk-gateway
# Test endpoint
curl -i http://localhost:8080/market/crypto/bitcoin
# docker-compose.yml
services:
tyk-gateway:
image: tykio/tyk-gateway:v5.2
volumes:
- ./middleware:/opt/tyk-gateway/middleware
- ./apps:/opt/tyk-gateway/apps
environment:
- TYK_GW_LOGLEVEL=info
- FACILITATOR_URL=https://facilitator.emfi.dev
ports:
- "8080:8080"
apiVersion: v1
kind: ConfigMap
metadata:
name: x402-middleware
data:
x402_payment.js: |
# Middleware content here
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: tyk-gateway
spec:
template:
spec:
containers:
- name: tyk-gateway
image: tykio/tyk-gateway:v5.2
volumeMounts:
- name: middleware
mountPath: /opt/tyk-gateway/middleware
volumes:
- name: middleware
configMap:
name: x402-middleware
See docs/DEPLOYMENT.md for detailed deployment instructions.
The middleware logs all operations with the [x402] prefix:
[x402] No payment header found for /market/crypto/bitcoin
[x402] Payment proof parsed successfully
[x402] Verifying payment with facilitator: https://facilitator.emfi.dev
[x402] Payment verified successfully for /market/crypto/bitcoin
[x402] Settling payment for transaction: ABC123...
[x402] Settlement successful: {"signature": "DEF456..."}
Monitor these logs to track:
# Check Tyk Gateway
curl http://localhost:8080/hello
# Check Facilitator connectivity
curl https://facilitator.emfi.dev/health
Symptoms: Even with valid payment header, receiving 402
Causes:
Solution:
# Check facilitator
curl https://facilitator.emfi.dev/health
# Check logs
docker compose logs tyk-gateway | grep x402
# Verify network in API config matches payment
Symptoms: Payment accepted but settlement fails
Causes:
Solution:
Symptoms: Slow response times
Causes:
Solution:
# Reduce timeout
export FACILITATOR_TIMEOUT="3000"
# Use load balancer for facilitator
# Implement caching layer
Enable verbose logging:
# Set log level
export TYK_GW_LOGLEVEL=debug
# Restart gateway
docker compose restart tyk-gateway
# Watch logs
docker compose logs -f tyk-gateway | grep x402
Tested on: 2 CPU cores, 4GB RAM
| Metric | Value |
|---|---|
| Verification latency | ~150ms |
| Settlement latency | ~2s (async) |
| Throughput | 500 req/s |
| Memory overhead | ~50MB |
Contributions welcome! Please:
MIT License - see LICENSE for details
Built with:
Made with ❤️ for micropayment-powered APIs
An open SDK for agentic payments. Let AI agents make payments, hold funds, and move money across chains with policy enforcement and human approval built in.
Golang SDK for A2A Protocol
Client SDK for the Vybe x402 API. Pay-per-call USDC over HTTP and prepaid-credit WebSocket streaming for Vybe's Solana analytics API. Built for AI agents.
Rust SDK for the Machine Payments Protocol
Building blocks for Agentic payments (x402, MPP, AP2) for TypeScript, Rust, Go, Python, Ruby, PHP, Lua, Kotlin and Swift.
Opinionated React Native crypto x AI chat app boilerplate with embedded wallet support, conversational AI, and dynamic UI component injection