Authentication Methods
| Setting | Options | Purpose |
|---|---|---|
AUTH_MODE | none / jwt | JWT authentication for external frontends |
X402_ENABLED | true / false | x402 USDC micropayments on Base |
B402_ENABLED | true / false | b402 USDT micropayments on BNB Chain |
Authentication methods are independent and can be combined. Payment authentication (x402/b402) takes priority over JWT.
JWT Authentication
For external frontends connecting to the API. Your backend authenticates users and signs JWTs with a shared secret.Quick Start
How JWT Auth Works
JWT Payload Requirements
The JWT payload must include the following claims:User ID as a valid UUID. This identifies the user in the database.
Expiration timestamp (Unix epoch time).Maximum expiration is controlled by
MAX_JWT_EXPIRATION (default: 3600 seconds).Issued at timestamp (Unix epoch time).
User email (optional).
Organization ID for multi-tenant deployments (optional).
Code Examples
Important Notes
Same user = same UUID - Use consistent UUIDs per user to maintain conversation history.
x402 Payment Protocol
Pay-per-request access using USDC micropayments on Base network.Features
Gasless Transfers
EIP-3009 for fee-free USDC transfers
Embedded Wallets
Email-based wallet creation via CDP
HTTP 402 Flow
Standard “Payment Required” protocol
Base Network
Supports testnet (Base Sepolia) and mainnet
Configuration
Get Coinbase CDP Credentials
Get credentials from Coinbase Developer Portal:
How x402 Works
x402 Environment Variables
Enable x402 payment protocol.
Environment:
testnet (Base Sepolia) or mainnet (Base).Your wallet address (0x…) to receive payments.
x402 facilitator service URL.
Network:
base-sepolia (testnet) or base (mainnet).Payment asset (USDC).
USDC contract address (testnet default shown).
Payment timeout in seconds.
Pricing
Default: $0.01 per request. Configure insrc/x402/pricing.ts:
src/x402/pricing.ts
b402 Payment Protocol
Pay-per-request access using USDT micropayments on BNB Chain.Configuration
b402 works identically to x402 but uses USDT on BNB Chain instead of USDC on Base.
Authentication Priority
When multiple auth methods are available, the system uses this priority:- x402/b402 payment proof - Cryptographic wallet signature (highest trust)
- JWT token - Verified signature
- Anonymous - Only if
AUTH_MODE=none
Combining JWT + Payment Auth
You can enable both systems simultaneously:- JWT-authenticated users to access
/api/chatand/api/deep-research/*(no payment) - Anyone to pay-per-request via
/api/x402/chatand/api/x402/deep-research/*
API Endpoints
| Endpoint | Auth Required | Payment Required |
|---|---|---|
/api/chat | JWT (if AUTH_MODE=jwt) | No |
/api/x402/chat | No | Yes (x402) |
/api/b402/chat | No | Yes (b402) |
/api/deep-research/start | JWT (if AUTH_MODE=jwt) | No |
/api/deep-research/status | JWT (if AUTH_MODE=jwt) | No |
/api/x402/deep-research/start | No | Yes (x402) |
/api/x402/deep-research/status | No | Yes (x402) |
/api/x402/config | No | No |
/api/health | No | No |
Database Schema
Payment Tracking - x402_payments
Implementation Files
| File | Purpose |
|---|---|
src/middleware/authResolver.ts | Unified auth middleware |
src/services/jwt.ts | JWT verification |
src/middleware/x402.ts | Payment enforcement |
src/x402/service.ts | Payment verification |
src/types/auth.ts | Auth types |
Troubleshooting
JWT Authentication Failed
JWT Authentication Failed
Symptom: 401 UnauthorizedSolutions:
- Verify
BIOAGENTS_SECRETmatches on both sides - Check JWT expiration (max 1 hour by default)
- Ensure
subclaim is a valid UUID - Verify
AUTH_MODE=jwtis set on server
Payment Verification Failed
Payment Verification Failed
Symptom:
x402_payment_invalid errorSolutions:- Check USDC/USDT balance in wallet
- Verify network configuration (testnet vs mainnet)
- Ensure facilitator URL is correct
- Check payment signature is valid
x402/b402 Routes Not Available
x402/b402 Routes Not Available
Symptom: 404 on
/api/x402/* or /api/b402/* routesSolutions:- Verify
X402_ENABLED=trueorB402_ENABLED=trueis set - Restart server after changing
.env - Check server logs for initialization errors
Invalid UUID Format
Invalid UUID Format
Symptom: Database error about invalid UUIDSolutions:
- Ensure
subclaim is a valid UUID format - Use
crypto.randomUUID()to generate UUIDs - Don’t use email addresses or usernames as
sub
Resources
x402 Protocol
Learn about the x402 payment protocol
Coinbase CDP
Get Coinbase Developer Platform credentials
Base Network
Learn about Base network
EIP-3009
Transfer With Authorization standard
Next Steps
Environment Variables
View all configuration options
Storage
Configure S3 storage for file uploads