Skip to main content
This guide shows how to build a backend proxy for your CDP Paymaster endpoint. A proxy keeps your API key secure while allowing you to add custom validation logic for sponsored transactions.

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:
  1. Keeping your API key server-side — The Paymaster URL never reaches the browser
  2. Enabling custom validation — Add rate limiting, user authentication, or business logic
  3. Providing an audit trail — Log all sponsorship requests for monitoring
For a deeper dive into security considerations, see the Security page.
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
Store your Paymaster URL in an environment variable (server-side only):
.env

Using the Proxy

With CDP user wallets

Pass your proxy URL to useSendUserOperation:

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
For production, store rate limits and usage counts in a persistent store like Redis rather than in-memory.

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

  1. Start your backend server
  2. Send a test request to your proxy endpoint:
  1. 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