Skip to main content
Defined in the Base Account SDK
Node.js Only: This function uses CDP (Coinbase Developer Platform) server wallets and is only available in Node.js environments. For browser/client-side applications, use prepareCharge instead.
The charge function executes subscription charges automatically from your backend. It uses a CDP smart wallet as the subscription owner, handling all transaction details including wallet management, transaction signing, and optional gas sponsorship. No manual transaction management required.
Treat subscription IDs as capability handles, not proof of ownership. Store the ID on your backend against the authenticated user when they subscribe. Do not charge an arbitrary id supplied by the browser alone. Pass expectedPayer (the subscriber’s wallet from your session/database) so the SDK rejects subscriptions that do not belong to that user. charge() also requires the permission spender to match your CDP smart wallet.

How It Works

When you call charge(), the function:
  1. Initializes a CDP client with your credentials
  2. Retrieves the existing smart wallet (subscription owner)
  3. Prepares the charge transaction calls (and verifies the permission spender matches that wallet)
  4. Executes the charge using the smart wallet
  5. Optionally uses a paymaster for gas sponsorship
  6. Returns the transaction hash

Parameters

string
required
The subscription ID (permission hash) returned from subscribe(). Prefer a server-stored ID bound to the authenticated user.Pattern: ^0x[0-9a-fA-F]{64}$
string | 'max-remaining-charge'
required
Amount to charge as a string (e.g., “10.50”) or 'max-remaining-charge' to charge the full remaining amount in the current period.
boolean
Whether to use Base Sepolia testnet. Must match the network used in subscribe(). Default: false
string
CDP API key ID. Falls back to CDP_API_KEY_ID environment variable.
string
CDP API key secret. Falls back to CDP_API_KEY_SECRET environment variable.
string
CDP wallet secret. Falls back to CDP_WALLET_SECRET environment variable.
string
Optional custom wallet name for the CDP smart wallet. Default: “subscription owner”
Use Default: Most applications should omit this parameter and use the default. Only specify if you used a custom name in getOrCreateSubscriptionOwnerWallet().
string
Paymaster URL for transaction sponsorship (gasless transactions). Falls back to PAYMASTER_URL environment variable.
string
Optional recipient address to receive the charged USDC. If not provided, USDC stays in the subscription owner wallet.Pattern: ^0x[0-9a-fA-F]{40}$
string
Optional subscriber wallet address. When set, the subscription’s payer must match this address or charge() throws. Use the authenticated user’s wallet from your session or database.Pattern: ^0x[0-9a-fA-F]{40}$
string
Optional. Must match your CDP smart wallet if provided. charge() already binds the permission spender to the executing CDP wallet automatically.Pattern: ^0x[0-9a-fA-F]{40}$

Returns

ChargeResult
Charge execution result.

Setup

Before using charge(), you need CDP credentials. Get them from the CDP Portal. Set as environment variables:
Or pass directly as parameters (see examples below).

Error Handling

Always wrap charge() calls in try-catch blocks:

Common Errors

Solution: Ensure CDP_API_KEY_ID, CDP_API_KEY_SECRET, and CDP_WALLET_SECRET are set as environment variables or passed as parameters.
Solution: Check that the subscription ID is correct and the subscription hasn’t been cancelled. Use getSubscriptionStatus to verify.
Solution: The subscription has been fully charged for the current period. Wait until the next period starts.
Solution: The CDP wallet hasn’t been created yet. First call getOrCreateSubscriptionOwnerWallet to set up the wallet.
Solution: Confirm you are charging with the CDP wallet that was used as subscriptionOwner at subscribe time, and that expectedPayer matches the subscriber wallet stored for that user.

Usage Pattern

Typical implementation in a backend service:
Scheduled Charging Service

Setup Wallet

Create CDP wallet before charging

Check Status

Verify subscription before charging

Custom Charge

Advanced manual execution