Accept Crypto Subscription Payment
Example of how to create a recurring subscription using Copperx APIs
Overview
This guide provides an example of how to create a recurring subscription using Copperx APIs. In this example, we will set up a subscription payment for 10 USDC per month.
Prerequisites
Before starting, ensure you have the following:
Development Environment: We will use the Polygon Mumbai Testnet and Goerli Testnet. https://dashboard.copperx.dev
API Key: Generate your API key from the Copperx Dashboard. If you don't have one, then generate it from here.
Test ERC20 Faucets: You can request test tokens from Goerli Faucet and Polygon Faucet. For additional test tokens, contact our team. As you are using test environment, there is no real money involved. So feel free to play around.
Copperx Account: Ensure you have an account to access the dashboard.
Steps to Create a Subscription
1. Create a Plan in Copperx Dashboard
To start a subscription, you first need to create a plan in the Copperx Dashboard.
Go to the Copperx Dashboard.
Log in with your Copperx account.
Click on Billing in the top menu and select Plans.
Click the Create Plan button in the top-right corner.
Fill in the plan details:
Title: Plan title
Description: Plan description
Recurring Interval: Set to monthly
Price: Set to 10 USDC
Click the Create Plan button.
Congratulations! 🎉 You have successfully created your first plan on Copperx.
Congratulations 🎉, you have successfully created your first plan on Copperx.
2. Create a Checkout Session
To create a subscription, you need to create a Checkout Session using the priceId
of the newly created plan.
Find the plan for which you want to create a Checkout Session.
Click
•••
> Details to find more details.On details page, you will find a field called Plan Id.
Press Copy icon to copy
Price Id
Now let's go ahead and create subscription of our plan.
To create a checkout session, run below command in your terminal:
Make sure you update YOUR_API_KEY
and PRICE_ID
in the code.
You will receive the responsive as below:
Here,
id
= Unique Identifier for Checkout Session
amountTotal
= Amount to be collected from user (in 8 decimal)
currency
= Payment Currency
url
= Hosted Checkout page for the Session. You need to redirect user to this URL.
To know more about the full Checkout Session object, click here.
Once you create a Checkout Session, you need to redirect user to the give URL in url
parameter of the response.
Once user makes the payment, user will be redirected to the URL provided in successUrl
.
If user cancels the payment, then user will be redirected to the URL provided in cancelUrl
.
Learn More
To learn more about handling subscription webhooks and the subscription lifecycle, refer to the following documentation pages:
If you are facing issues while using our API, feel free to contact us on support@copperx.io.
Last updated