Retrieve Cart in Storefront

You can retrieve a cart by sending a request to the Get a Cart API route.

Assuming you stored the cart's ID in the localStorage as explained in the Create Cart guide, pass that ID as a path parameter to the request.

For example:

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


Format Prices#

When displaying the cart's totals or line item's price, make sure to format the price as implemented in the formatPrice function shown in the above snippet:

Code
1const formatPrice = (amount: number): string => {2  return new Intl.NumberFormat("en-US", {3    style: "currency",4    currency: cart?.currency_code,5  })6  .format(amount)7}

Since this is the same function used to format the prices of products, you can define the function in one place and re-use it where necessary. In that case, make sure to pass the currency code as a parameter.

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