Pay with USDC
Buy or sell any tokenized asset with USDC, in an API call
Introduction
This guide will show you how to quickly buy or sell any tokenized asset (NFT) with USDC, using Crossmint’s headless APIs.
Prerequisites
From Crossmint
- Create a developer account in the Staging Console.
- Create a new collection or import yours into the console, and have your
collectionId
ready.- Make sure you follow the maximum prices for collections set in staging outlined here
- Create a server-side API key with the
orders.create
,orders.update
, andorders.read
scopes enabled. More info on creating API keys here.
This Quickstart assumes that you'll be using the API Playground or cURL requests to make the API calls. This approach requires the use of a server-side API key.
If you would rather follow the quickstart while building a client-side app that makes requests to Crossmint directly from the browser, you must use a client-side API key. See the Server or Client Guide for more information.
To integrate in production/mainnet, you'll also need to complete account and collection verification. More information in the production launch guide.
External prerequisites
Wallet
Crypto wallet supporting USDC - Metamask, Phantom, Coinbase Wallet, etc.
Testnet Balance
A small balance of USDC testnet currency in your wallet on the Base-sepolia network.
Get testnet USDC here:
Make sure to use the official Crossmint USDC faucet provided above. Other USDC faucets may use different token addresses that won’t be compatible with this quickstart.
Adding USDC to MetaMask:
- Import the token to MetaMask:
- Open MetaMask and click “Import tokens”
- Paste the USDC contract address of
0x14196F08a4Fa0B66B7331bC40dd6bCd8A1dEeA9F
- Click Import
- Refresh your wallet
Create an order and pay with USDC
Setup your Python environment
Clone the quickstart repo:
Navigate to the project directory and create a virtual environment:
Install the dependencies:
Set keys in .env
Set the following keys in the .env
file:
CROSSMINT_API_KEY
,COLLECTION_ID
are found from the Developer Console. See PrerequisitesPAYER_ADDRESS
is your wallet address that contains USDC.EMAIL_ADDRESS
is where you’ll receive your NFT in a new Crossmint wallet that you can access later.
Create order with USDC payment
Run the application:
This will:
- Create an order that is waiting for a USDC payment on the Base-sepolia network
- Return the serializedTransaction, which will be used in the next step
- Poll the status of the order until it is complete
Receive Payment
- Copy the
serializedTransaction
returned in the previous step and paste it below - Connect to your USDC wallet that was specified in the
.env
file - Send the transaction
Status of order
The code will poll for the status of the order until it is marked as complete. You should see it changing from “payment” to “delivery” and finally to “completed”.
You will also see the final order details, including the transaction hash, in your terminal.
Access your NFT
Once the order is complete, you will receive an email with a link to the purchased item, which can be viewed in the Crossmint website.
You can also access your NFT by logging in to your Crossmint wallet at crossmint.com with the email address you specified in the .env
file.
Understanding the code
Creating an order
The order creation process involves sending a POST request to Crossmint’s API with the necessary payment and recipient details. Let’s break down the key components:
Key parameters explained
-
recipient: Specifies where to deliver the tokenized asset
email
: The email address where the tokenized asset will be delivered in a Crossmint wallet. Instead of an email, awalletAddress
field can be used to deliver the asset to a specific wallet address.
-
payment: Defines how the payment will be made
method
: The blockchain network (base-sepolia
for testnet)currency
: The payment currency (usdc
)payerAddress
: The wallet address that will send the USDC payment
-
lineItems: Specifies what is being purchased
collectionLocator
: Identifies the collection
API request
The API request:
- Sends a POST request to Crossmint’s order endpoint
- Includes your API key for authentication
- Sends the order data as JSON in the request body
Response handling
The response includes:
clientSecret
: A unique token used for authenticating subsequent requests for this orderorder
: The order details including the order ID and status
For more details on creating orders via the headless API, see the Create Order API Reference.
Polling an order
The order status polling process involves periodically checking the order’s status until it’s completed. Let’s examine how this works:
Get order function explained
- Takes
order_id
andclient_secret
as parameters - Makes a GET request to fetch the current state of the order
- Uses both API key and client secret for authentication
- Returns the order details as JSON
Poll order function explained
- Continuously checks the order status every 3 seconds.
- Prints the current phase of the order
- Possible order phases include:
payment
: Waiting for or processing paymentdelivery
: NFT is being deliveredcompleted
: Order is successfully completed
- Returns the final order details when completed
The 3-second polling interval helps prevent rate limiting while still providing timely updates. Adjust this value based on your needs.
For more details on polling for order status, see the Get Order API Reference.
Always implement proper error handling, retry mechanisms, and asynchronous polling in production environments to handle network issues or API interruptions.