Cart API - Hands-on Lab
Cart API
Server-to-Server Cart API allows for a cart to be created using BigCommerce cart logic. The existing BigCommerce front-end cart can be bypassed.In this activity, you will use the Server-to-Server Cart API to view the details of a cart using an API REST client. You will also use the Cart API to create, modify, and delete a cart as well as create redirect URLs.
In this lab, you will:
- Create a Cart
- Add cart line items
- GET a cart
- Update cart line items
- Delete cart line items
- Update customer ID
- Create a redirect URL
- Delete a cart
Prerequisites
- BigCommerce store (sandbox or live)
- API credentials for the store
- REST client (Postman)
Create a cart with a simple product (a product without option selections)
- Open Postman
- Click the No Environment dropdown in the upper-right
- Select the environment you created in the previous lab
- Copy andpaste the request below into Postman
If you created your environment correctly in the previous lab, {{store_hash}} will use your store’s store hash automatically.
- Copy andpastethe code below into the body section of Postman
- Replace
{product_id}with the ID of the product to be added to cart - Select POST next to the request URL
- Click the Send button
- Observe response
- Copy and save the
cart_idfor use in the next steps
Create a cart with a complex product, a custom item, and a gift certificate
- Copy and paste the request below into Postman
If you created your environment correctly in the previous lab, {{store_hash}} will use your store’s store hash automatically.
- Replace
{cart_Id}with the cart_id created in the previous steps - Copy and paste the code below into the Body section of Postman
- Replace
{product_id}with the ID of the product to be added to cart - Replace
{variant_id},{option_id}, and{option_value}with the appropriate IDs for the product being added to cart - Select POST next to the request url
- Click the Send button
- Observe response
GET a Cart
- Copy and paste the request below into Postman
If you created your environment correctly in the previous lab, {{store_hash}} will use your store’s store hash automatically.
- Replace
{cart_id}with the cart ID copied in the previous steps - Select GET next to the request URL
- Click the Send button
- Observe response and copy one of the line item IDs for use in the next steps
Update Cart Line Items
Currently only updating the item price and quantity are supported.
If a variant needs to be changed or updated, the product will need to be removed and re-added to the cart with the correct variants using the Add Cart Line Items endpoint.
- Copy andpaste the request below into Postman
If you created your environment correctly in the previous lab, {{store_hash}} will use your store’s store hash automatically.
- Replace
{cart_id}with the Cart ID created in the previous steps - Replace
{item_id}with the Item ID copied in the previous steps - Copy and** paste** the code below into the Body section of Postman
- Replace
{product_id}with the product ID to be updated - Select PUT next to the request URL
- Click the Send button
- Observe response
DELETE Cart Line Items
Removing the last line_item in the Cart deletes the Cart.
- Copy and paste the request below into Postman
If you created your environment correctly in the previous lab, {{store_hash}} will use your store’s store hash automatically.
- Replace
{cart_id}with the Cart ID created in the previous activity - Replace
{item_id}with the item to be deleted - Select DELETE next to the request url
- Click the Send button
- Observe response and line item deleted
Update a cart to change the customer ID assigned to the cart
- Copy and paste the request below into Postman
If you created your environment correctly in the previous lab, {{store_hash}} will use your store’s store hash automatically.
- Replace
{cart_id}with the Cart ID created in the previous steps - Copy and paste the code below into the Body section of Postman
- Replace
{customer_id}with the ID of a customer in the store - Select PUT next to the request URL
- Click the Send button
- Observe response and customer ID updated
Create a Redirect URL
- Copy and paste the request below into Postman
If you created your environment correctly in the previous lab, {{store_hash}} will use your store’s store hash automatically.
- Replace
{cart_id}with the Cart ID created in the previous steps - Select POST next to the request url
- Click the Send button
- Observe response and redirect URLs created
Delete a Cart
Once a cart has been deleted it cannot be recovered.
- Copy and paste the request below into Postman
If you created your environment correctly in the previous lab, {{store_hash}} will use your store’s store hash automatically.
- Replace
{cart_id}with the Cart ID to be deleted - Select DELETE next to the request URL
- Click the Send button
- Observe response and cart deleted