Checkout Sessions
A Checkout Session orchestrates the payment flow for one-time payments or subscriptions. When a customer is ready to pay, you create a Session and redirect them to its unique URL. This guide covers the API for managing Checkout Sessions.
For practical examples and implementation walkthroughs, please refer to our Usage Guide for Checkout Sessions.
The Checkout Session Object#
A Checkout Session object contains all the details about a single purchase flow.
Attribute | Type | Description |
|---|---|---|
|
| Unique identifier for the Checkout Session. |
|
| The URL to which you should redirect your customer to complete the payment. This is available only after creation. |
|
| The current status of the session. Can be |
|
| The payment status for the session. Can be |
|
| The mode of the Checkout Session, determining its purpose. Can be |
|
| The ID of the associated Customer, if one exists. |
|
| The ID of the subscription created if the session is in |
|
| A list of items the customer is purchasing. |
|
| The URL to redirect to after a successful payment. |
|
| The URL to redirect to if the customer cancels the payment flow. |
|
| The Unix timestamp at which the session will expire. |
|
| A set of key-value pairs that you can attach to the object for your own reference. |
Create a Checkout Session#
Creates a new session to initiate a payment or subscription flow.
Parameters
Name | Type | Description |
|---|---|---|
|
| The mode of the Checkout Session. Required. Can be |
|
| A list of line items for the session. Each item object must contain a |
|
| The URL the customer will be directed to after a successful purchase. Required. |
|
| The URL the customer will be directed to if they cancel the purchase. Required. |
|
| ID of an existing customer to associate with this session. Optional. |
|
| A list of payment method types to accept (e.g., |
|
| Enables user-redeemable promotion codes. Optional. |
|
| Data used when |
|
| Key-value data to store with the session. Optional. |
Returns
Returns the newly created TCheckoutSession object. The url attribute contains the redirect link for the customer.
Example
async function createCheckoutSession() {
try {
const session = await payment.checkoutSessions.create({
success_url: 'https://example.com/success?session_id={CHECKOUT_SESSION_ID}',
cancel_url: 'https://example.com/cancel',
line_items: [{
price_id: 'price_123456789',
quantity: 2,
}],
mode: 'payment',
metadata: {
order_id: 'order_abc123'
}
});
console.log('Redirect your customer to:', session.url);
} catch (error) {
console.error('Error creating session:', error);
}
}
createCheckoutSession();Example Response
{
"id": "cs_xxxxxxxxxxxxxx",
"object": "checkout.session",
"url": "https://pay.arcblock.io/session/cs_xxxxxxxxxxxxxx",
"status": "open",
"payment_status": "unpaid",
"mode": "payment",
"success_url": "https://example.com/success?session_id={CHECKOUT_SESSION_ID}",
"cancel_url": "https://example.com/cancel",
"customer_id": null,
"line_items": [
{
"price_id": "price_123456789",
"quantity": 2
}
],
"expires_at": 1678886400,
"metadata": {
"order_id": "order_abc123"
}
}Retrieve a Checkout Session#
Fetches the details of an existing Checkout Session by its ID. This is useful for checking the status after a customer has been redirected.
Parameters
Name | Type | Description |
|---|---|---|
|
| The unique identifier of the Checkout Session. Required. |
Returns
Returns the TCheckoutSessionExpanded object, which includes more details than the standard session object.
Example
async function retrieveCheckoutSession(sessionId) {
try {
const session = await payment.checkoutSessions.retrieve(sessionId);
console.log('Session status:', session.status);
console.log('Payment status:', session.payment_status);
} catch (error) {
console.error('Error retrieving session:', error);
}
}
retrieveCheckoutSession('cs_xxxxxxxxxxxxxx');Example Response
{
"id": "cs_xxxxxxxxxxxxxx",
"object": "checkout.session",
"status": "complete",
"payment_status": "paid",
"mode": "payment",
"customer_id": "cus_yyyyyyyyyyyyyy",
"payment_intent_id": "pi_zzzzzzzzzzzzzzz",
"metadata": {
"order_id": "order_abc123"
}
}Update a Checkout Session#
Updates an existing Checkout Session. Currently, only the metadata can be modified.
Parameters
Name | Type | Description |
|---|---|---|
|
| The ID of the Checkout Session to update. Required. |
|
| A set of key-value pairs to store with the session. Any existing metadata will be replaced. Required. |
Returns
Returns the updated TCheckoutSession object.
Example
async function updateCheckoutSession(sessionId) {
try {
const session = await payment.checkoutSessions.update(sessionId, {
metadata: {
order_id: 'order_abc123',
shipped: 'false'
}
});
console.log('Updated metadata:', session.metadata);
} catch (error) {
console.error('Error updating session:', error);
}
}
updateCheckoutSession('cs_xxxxxxxxxxxxxx');Example Response
{
"id": "cs_xxxxxxxxxxxxxx",
"object": "checkout.session",
"status": "open",
"metadata": {
"order_id": "order_abc123",
"shipped": "false"
}
}List all Checkout Sessions#
Returns a paginated list of all Checkout Sessions. You can filter the list based on various criteria.
Parameters
Name | Type | Description |
|---|---|---|
|
| Filter by session status ( |
|
| Filter by payment status ( |
|
| Only return sessions for the given customer ID. Optional. |
|
| Only return the session for the given Payment Intent. Optional. |
|
| Only return the session for the given Subscription. Optional. |
|
| The page number for pagination. Optional. |
|
| The number of items per page. Defaults to 20. Optional. |
Returns
A paginated object containing a list of TCheckoutSessionExpanded objects and a count of total matching records.
Example
async function listCompletedSessions() {
try {
const sessions = await payment.checkoutSessions.list({
status: 'complete',
limit: 10
});
console.log(`Found ${sessions.count} completed sessions.`);
sessions.list.forEach(session => {
console.log(`- ${session.id}`);
});
} catch (error) {
console.error('Error listing sessions:', error);
}
}
listCompletedSessions();Example Response
{
"count": 5,
"list": [
{
"id": "cs_session_abc",
"status": "complete",
"payment_status": "paid"
},
{
"id": "cs_session_def",
"status": "complete",
"payment_status": "paid"
}
]
}Expire a Checkout Session#
Manually expires an open Checkout Session. Once expired, a customer can no longer complete the payment.
Parameters
Name | Type | Description |
|---|---|---|
|
| The ID of the Checkout Session to expire. Required. |
Returns
Returns the TCheckoutSession object with its status set to expired.
Example
async function expireCheckoutSession(sessionId) {
try {
const session = await payment.checkoutSessions.expire(sessionId);
console.log(`Session ${session.id} status is now: ${session.status}`);
} catch (error) {
console.error('Error expiring session:', error);
}
}
expireCheckoutSession('cs_xxxxxxxxxxxxxx');Example Response
{
"id": "cs_xxxxxxxxxxxxxx",
"object": "checkout.session",
"status": "expired",
"payment_status": "unpaid",
"mode": "payment"
}After managing Checkout Sessions, you may want to learn more about handling Subscriptions or managing Payment Intents.