End-to-End Guide: Headless checkout flow with the REST Management API

This tutorial walks you through creating a cart, checkout, an order, and processing a payment using the Carts and Checkout REST Management APIs to build a headless storefront.

This article assumes you are familiar with the following:

  • Creating a channel and a site.
  • Retrieving product data using REST Management API calls

Prerequisites

  • A store-level or app-level API account with the following permissions:
UI NamePermissionParameter
Cartsmodifystore_cart
Checkoutsmodifystore_checkouts
Ordersmodifystore_v2_orders
Productsread-onlystore_v2_products_read_only

For more information, see OAuth Scopes.

Creating a cart

1

Step 1: Generate the cart ID

To generate a cart ID, send a request to the Create a cart endpoint. The response contains an id, which is the cart_id or cartId in subsequent calls.

Example request: Create a cart
1 POST https://api.bigcommerce.com/stores/{{store_hash}}/v3/carts
2 Accept: application/json
3 Content-Type: application/json
4 X-Auth-Token: {{ACCESS_TOKEN}}
5 {
6 "customer_id": 0,
7 "line_items": [
8 {
9 "quantity": 2,
10 "product_id": 120,
11 "list_price": 15,
12 "name": "mug"
13 }
14 ],
15 "channel_id": 1,
16 "currency": {
17 "code": "USD"
18 },
19 "locale": "en-US"
20 }
2

Step 2: Generate the redirect URLs

After you generate the cart ID, generate URLs to redirect customers to the BigCommerce-hosted cart and checkout pages.

To generate the redirect URLs, send a request to the Create a cart redirect URL endpoint. You use the cart ID generated in Step 1. The response contains the cart_url and the checkout_url.

Example request: Create cart redirect URLs
1 POST https://api.bigcommerce.com/stores/{{store_hash}}/v3/carts/{{cartId}}/redirect_urls
2 Accept: application/json
3 Content-Type: application/json
4 X-Auth-Token: {{ACCESS_TOKEN}}
5 {}

Creating a checkout

After you create a cart, transform it into a checkout by adding a billing address. You must have a checkoutId, which is the same as the cart_id you generated in Step 1 of creating a cart. To add a billing address, send a request to the Add Checkout Billing Address endpoint.

Example request: Add Checkout Billing Address
1 POST https://api.bigcommerce.com/stores/{{store_hash}}/v3/checkouts/{{checkoutId}}/billing-address
2 Accept: application/json
3 Content-Type: application/json
4 X-Auth-Token: {{ACCESS_TOKEN}}
5 {
6 "first_name": "John",
7 "last_name": "Doe",
8 "email": "john.doe@example.com",
9 "company": "BigCommerce",
10 "address1": "555 Main Street",
11 "address2": "",
12 "city": "Austin",
13 "state_or_province": "TX",
14 "state_or_province_code": "TX",
15 "country_code": "US",
16 "postal_code": "78701",
17 "phone": "555-555-5555"
18 }

After you transform your headless cart into a proper checkout with a billing address, you can add a consignment with a shipping address, line items, and a shipping option. You can do so using the following two-step process:

1

Step 1: Add a new consignment to the checkout

Send a request to the Add Consignment to Checkout endpoint. Append the include=consignments.available_shipping_options query parameter to your request to return available shipping options. Use one of the available shipping options to update the consignment in step 2.

Example request: Add Consignment to checkout
1POST https://api.bigcommerce.com/stores/{{store_hash}}/v3/checkouts/{{checkoutId}}/consignments?include=consignments.available_shipping_options
2Accept: application/json
3Content-Type: application/json
4X-Auth-Token: {{ACCESS_TOKEN}}
5[
6 {
7 "shipping_address": {
8 "first_name": "John",
9 "last_name": "Doe",
10 "email": "john.doe@example.com",
11 "company": "BigCommerce",
12 "address1": "555 Main Street",
13 "address2": "",
14 "city": "Austin",
15 "state_or_province": "Texas",
16 "state_or_province_code": "TX",
17 "country_code": "US",
18 "postal_code": "78701",
19 "phone": "555-555-5555"
20 },
21 "line_items": [
22 {
23 "item_id": "ca9ef0d1-1da9-48e5-a505-7051eb575432",
24 "quantity": 2
25 }
26 ]
27 }
28]

The response contains an array of available_shipping_options. In the next step, use one of the available shipping options to update the consignment.

available shipping options
1 "available_shipping_options": [
2 {
3 "id": "6ded13392879983ee32a3563f5fa6a7b",
4 "type": "shipping_pickupinstore",
5 "description": "Pickup In Store",
6 "image_url": "",
7 "cost": 0,
8 "transit_time": "",
9 "additional_description": ""
10 },
11 {
12 "id": "26fb2db4ad77b0f039328d22d2869617",
13 "type": "shipping_flatrate",
14 "description": "Flat Rate",
15 "image_url": "",
16 "cost": 5,
17 "transit_time": "",
18 "additional_description": ""
19 },
20 {
21 "id": "508540c73074d5ffa2cc3dced0adc552",
22 "type": "shipping_byweight",
23 "description": "Ship by Weight",
24 "image_url": "",
25 "cost": 8,
26 "transit_time": "",
27 "additional_description": ""
28 }
29 ]
2

Step 2: Update the consignment with a shipping option

After you create the consignment, update it with one of the available shipping options the previous request returned. Send a request to the Update Checkout Consignment endpoint.

Example request: Update checkout consignment
1 PUT https://api.bigcommerce.com/stores/{{store_hash}}/v3/checkouts/{{checkoutId}}/consignments/{{consignmentId}}
2 Accept: application/json
3 Content-Type: application/json
4 X-Auth-Token: {{ACCESS_TOKEN}}
5 {
6 "shipping_option_id": "6ded13392879983ee32a3563f5fa6a7b"
7 }

Creating an order

After you add a billing address and a consignment to your checkout, you can create an order by sending a request to the Create an Order endpoint. The initial order status is incomplete.

Example request: Create an order
1 POST https://api.bigcommerce.com/stores/{{store_hash}}/v3/checkouts/{{checkoutId}}/orders
2 Accept: application/json
3 Content-Type: application/json
4 X-Auth-Token: {{ACCESS_TOKEN}}

Resources

API Reference: REST Management