Why Use a Proxy?
Your CDP Paymaster endpoint URL contains your Client API Key (not a Secret API Key). While this isn’t as sensitive as a secret key, exposing it in client-side code allows anyone to use your Paymaster endpoint to sponsor transactions—potentially draining your gas credits or abusing your allowlisted contracts. A proxy solves this by:- Keeping your API key server-side — The Paymaster URL never reaches the browser
- Enabling custom validation — Add rate limiting, user authentication, or business logic
- Providing an audit trail — Log all sponsorship requests for monitoring
When is a proxy optional?If you’re using CDP user wallets with
useCdpPaymaster: true, the SDK handles paymaster communication securely. A custom proxy is only needed if you want additional validation beyond CDP’s built-in contract allowlists.Basic Proxy Implementation
Here’s a minimal Next.js API route that proxies requests to your CDP Paymaster:app/api/paymaster/route.ts
.env
Using the Proxy
With CDP user wallets
Pass your proxy URL touseSendUserOperation:
With Wagmi
Pass your proxy URL in the paymaster capabilities:Adding Custom Validation
The power of a proxy is adding validation logic beyond what CDP’s allowlists provide.Rate Limiting
Prevent abuse by limiting requests per user:app/api/paymaster/route.ts
User Authentication
Only sponsor transactions for authenticated users:app/api/paymaster/route.ts
Custom Business Logic
Add application-specific rules:app/api/paymaster/route.ts
Express.js Example
If you’re not using Next.js, here’s an Express equivalent:server.ts
Testing Your Proxy
Local Development
For local development, you can temporarily use the Paymaster URL directly in your frontend to verify your smart account setup works. Once confirmed, switch to your proxy.Verify the Proxy Works
- Start your backend server
- Send a test request to your proxy endpoint:
- You should receive a JSON-RPC response (either success or an error about invalid params—both confirm the proxy is forwarding correctly)
Production Checklist
Before deploying:- Environment variable is server-side only — No
NEXT_PUBLIC_prefix - Rate limiting configured — Prevent abuse
- Authentication required — Only sponsor for your users
- Error handling — Graceful failures with appropriate status codes
- Logging — Monitor sponsorship requests and failures
- Contract allowlist configured — Set up in CDP Portal as a secondary defense
Next Steps
- Security — Full security recommendations
- Errors — Common error codes and solutions
- Troubleshooting — Debugging failed transactions