Create Cart in Storefront

In this document, you'll learn how to create and store a cart.

Create Cart on First Access#

It's recommended to create a cart the first time a customer accesses a page, then store the cart's ID in the localStorage.

To create a cart, send a request to the Create Cart API route.

For example:

The response of the Create Cart API route has a cart field, which is a cart object.

Refer to the Create Cart API reference for details on other available request parameters.

Publishable API Key#

When you create a cart, you pass the publishable API key in the header of the request. This associates the cart with the sales channel(s) of the publishable API key.

This is necessary, as only products matching the cart's sales channel(s) can be added to the cart.


Associate Customer with Cart#

When the cart is created for a logged-in customer, it's automatically associated with that customer.

However, if the cart is created for a guest customer, then the customer logs in, then you have to set the cart's customer as explained in this guide.

Was this page helpful?
Edit this page
Ask Anything
FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break