Pure Rust implementation of Nginx module for x402 HTTP micropayment protocol.
Nginx-X402 is an early-stage Rust project in the AI payments / x402 ecosystem, focused on nginx, rust, x402, x402-payment. It currently has 4 GitHub stars and 2 forks, and sits alongside related tools like Vault-0, dna-x402, ngx-x402.
Pure Rust implementation of Nginx module for x402 HTTP micropayment protocol.
Linux:
sudo apt-get install -y libclang-dev
export NGX_CONFIGURE_ARGS="--without-http_rewrite_module"
cargo build --release
macOS:
xcode-select --install
# Set libclang path for bindgen (optional, usually auto-detected)
export LIBCLANG_PATH="$(xcode-select -p)/Toolchains/XcodeDefault.xctoolchain/usr/lib"
export SDKROOT=$(xcrun --show-sdk-path)
cargo build --release
The build script automatically detects your system nginx version and downloads matching source. Optionally set NGINX_SOURCE_DIR to use a specific nginx source.
# Download latest release
ARCH=$(dpkg --print-architecture)
LATEST_URL=$(curl -s https://api.github.com/repos/polyjuicelab/Nginx-X402/releases/latest | grep "browser_download_url.*${ARCH}\.deb" | cut -d '"' -f 4 | head -1)
wget "$LATEST_URL" -O nginx-x402.deb
# Install
sudo dpkg -i nginx-x402.deb
sudo apt-get install -f
sudo ln -s /etc/nginx/modules-available/x402.conf /etc/nginx/modules-enabled/x402.conf
sudo systemctl reload nginx
IMPORTANT: Module Loading
You have two options to load the module. Choose ONE method only:
Option 1: Using modules-enabled (Recommended for Debian/Ubuntu)
sudo ln -s /etc/nginx/modules-available/x402.conf /etc/nginx/modules-enabled/x402.conf
This automatically loads the module via /etc/nginx/modules-enabled/x402.conf.
Option 2: Manual load_module in nginx.conf
Add this line to your /etc/nginx/nginx.conf (at the top level, before http block):
load_module /usr/lib/nginx/modules/libnginx_x402.so;
⚠️ DO NOT use both methods - this will cause "module already loaded" errors.
After loading the module, add x402 configuration to your nginx.conf:
http {
server {
# Basic USDC payment (default)
location /protected {
x402 on;
x402_amount 0.0001;
x402_pay_to 0xYourWalletAddress;
x402_facilitator_url https://x402.org/facilitator;
x402_network base-sepolia;
}
# Custom ERC-20 token with 18 decimals
location /api/custom-token {
x402 on;
x402_amount 0.001;
x402_pay_to 0xYourWalletAddress;
x402_facilitator_url https://x402.org/facilitator;
x402_network_id 84532; # Base Sepolia chainId
x402_asset 0xYourCustomTokenAddress;
x402_asset_decimals 18; # Standard ERC-20
}
}
}
Basic Configuration:
x402 on|off - Enable/disable payment verificationx402_amount <amount> - Payment amount (e.g., "0.0001")x402_pay_to <address> - Recipient wallet addressx402_facilitator_url <url> - Facilitator service URLx402_description <text> - Payment descriptionNetwork Configuration:
x402_network <network> - Network identifier (e.g., "base", "base-sepolia")x402_network_id <chainId> - Network chainId (e.g., 8453 for Base Mainnet, 84532 for Base Sepolia). Takes precedence over x402_network if both are specified.Token Configuration:
x402_asset <address> - Custom token/contract address (optional, defaults to USDC for the network)x402_asset_decimals <decimals> - Token decimals (default: 6 for USDC, typically 18 for ERC-20 tokens). Required when using custom tokens to ensure correct amount calculation.Advanced Configuration:
x402_resource <path> - Resource path or full URL (default: automatically builds full URL from request: scheme://host/path)x402_timeout <seconds> - Facilitator API timeout (1-300, default: 10)x402_ttl <seconds> - Time-to-live for payment authorization validity (1-3600, default: 60). Controls the maximum time window for payment authorization timestamps.x402_facilitator_fallback <mode> - Fallback on error: error (500) or pass (default: error)x402_metrics on|off - Enable Prometheus metrics endpointNote: If x402_resource is not configured, the module automatically builds a full URL from the request (scheme://host/path). This ensures compatibility with facilitator APIs that require full URLs instead of relative paths. If you need a relative path or custom URL, explicitly set x402_resource.
Note: When using custom tokens, always specify x402_asset_decimals to match your token's decimal precision. Most ERC-20 tokens use 18 decimals, while USDC uses 6 decimals.
Add a /metrics location:
location /metrics {
x402_metrics on;
access_log off;
}
Available metrics:
x402_requests_total - Total requests processedx402_payment_verifications_total - Verification attemptsx402_payment_verifications_success_total - Successful verificationsx402_payment_verifications_failed_total - Failed verificationsx402_responses_402_total - 402 responses sentx402_facilitator_errors_total - Facilitator errorsx402_verification_duration_seconds - Verification latency histogramx402_payment_amount - Payment amount histogramscrape_configs:
- job_name: 'nginx-x402'
static_configs:
- targets: ['your-nginx-server:80']
metrics_path: '/metrics'
Unit Tests:
cargo test
All unit tests run without requiring nginx source or a running nginx instance.
Integration Tests:
cargo test --test docker_integration_test --features integration-test -- --ignored
Integration tests require Docker and will automatically build and run nginx in a container.
NGINX_SOURCE_DIR - Path to nginx source (optional, auto-detected if not set)NGINX_BINARY_PATH - Path to system nginx binary (optional, for signature extraction)NGX_CONFIGURE_ARGS - Nginx configure arguments (recommended: --without-http_rewrite_module)LIBCLANG_PATH - Path to libclang (required on macOS)SDKROOT - macOS SDK path (required on macOS)AGPL-3.0
Vault-0: Agent Security, Monitor & x402 Wallet for OpenClaw. Encrypted secret vault, real-time agent monitor, policy enforcement, and native x402 payments — built with Rust + Tauri.
DNA — Payment rails for AI agents. x402 micropayment protocol on Solana. Netting, transfer, stream settlement. Receipt anchoring on-chain.
Nginx module for the x402 HTTP micropayment protocol.
Agent behavior that compiles
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.
x402 payments in Rust: verify, settle, and monitor payments over HTTP 402 flows
Context-aware agentic LLM gateway & router that optimize your agentic workflows with every runs, works with any harnesses, any models, any workflows.
Production-ready x402 facilitator server.
Rust SDK for the x402 payment protocol.