Base Subscription Integration Guide

Follow the steps to explore Frontend and Backend subscription controls

This demo has credentials already provided. Your implementation will require the following environment variables:

CDP_API_KEY_ID=********-****-****-****-************

CDP_API_KEY_SECRET=*************************************************************

CDP_WALLET_SECRET=*********************************************************************************************************************

PAYMASTER_URL=https://api.developer.coinbase.com/rpc/v1/base/************************

Frontend

Client-Side Operations

Backend

Server-Side Operations

0

Create or Get Backend Wallet

Backend creates wallet with base.subscription.getOrCreateSubscriptionOwnerWallet

Example:

Code Editor

Intended for testing purposes only. Never paste code from untrusted sources.

Quick Tips

  • This function creates a new wallet or retrieves an existing one
  • The wallet is stored securely and associated with your CDP API credentials
  • This is the wallet that will receive subscription payments
  • Save the wallet address for use in the subscribe function
  • Optional: use walletName parameter to create a custom-named wallet

Output

Click "Execute Code" to run your code

1

Pass Wallet to Frontend

Send wallet address from backend to frontend

Waiting for wallet address...

2

Request Subscription from User

User subscribes with base.subscription.subscribe

Example:

Code Editor

Intended for testing purposes only. Never paste code from untrusted sources.

Quick Tips

  • Get testnet USDC at https://faucet.circle.com/ - select "Base Sepolia"
  • subscriptionOwner is your application's address that will control the subscription
  • recurringCharge is the amount of USDC to charge per period (e.g., "10.50" = $10.50)
  • Optional: use overridePeriodInSecondsForTestnet (testnet only) for fast testing (e.g., 10 = 10 second period)
  • Optional: set requireBalance: false to skip balance check

Output

Click "Execute Code" to run your code

3

Check Subscription Status

User checks status with base.subscription.getStatus

Code Editor

Intended for testing purposes only. Never paste code from untrusted sources.

Quick Tips

  • Use the subscription ID returned from the subscribe function
  • remainingChargeInPeriod shows how much can still be charged this period
  • Make sure to use the same testnet setting as the original subscription

Output

Click "Execute Code" to run your code

4

Pass Subscription ID to Backend

Send subscription ID from frontend to backend

Waiting for subscription ID...

5

Charge Subscription

Backend charges with base.subscription.charge

Example:

Code Editor

Intended for testing purposes only. Never paste code from untrusted sources.

Quick Tips

  • The charge amount must be less than or equal to the recurringCharge
  • You can only charge up to the remainingChargeInPeriod
  • Charges can be made multiple times within a period up to the total recurring charge
  • Use "max-remaining-charge" to charge the maximum remaining amount
  • Optional: specify a recipient address to receive the USDC

Output

Click "Execute Code" to run your code

6

Verify Subscription Status

Backend verifies with base.subscription.getStatus

Code Editor

Intended for testing purposes only. Never paste code from untrusted sources.

Quick Tips

  • Same function as frontend, but typically called from your backend
  • Use this to verify subscription status before granting access
  • Check isSubscribed and remainingChargeInPeriod fields

Output

Click "Execute Code" to run your code

7

Revoke Subscription

Backend cancels with base.subscription.revoke

Code Editor

Intended for testing purposes only. Never paste code from untrusted sources.

Quick Tips

  • Once revoked, no more charges can be made
  • The subscription payer can also revoke from their wallet

Output

Click "Execute Code" to run your code

šŸ’”

Automatic Recurring Charges (Cron Pattern)

Example pattern for automatically collecting charges each period

Code Editor

Intended for testing purposes only. Never paste code from untrusted sources.

Quick Tips

  • This demo executes automatically and shows real-time output for each charging cycle
  • By default, runs for 3 cycles - set maxCycles to null for continuous operation
  • Each cycle checks subscription status, waits for the next period, then charges
  • In production, use a proper scheduler like node-cron, bull queue, or cloud-based schedulers
  • Store subscription IDs in a database and iterate through active subscriptions
  • Use "max-remaining-charge" to collect the full recurring amount each period

Output

Click "Execute Code" to run your code