Create Cart in the Storefront

Create a cart and store its ID.

On this page

In this guide, you'll learn how to create and store a cart in your storefront.

For the recommended Next.js App Router pattern (server actions, cookies, store code vs country), see Build a Next.js Storefront.

Create Cart on First Access

Create a cart the first time a customer needs one (add to cart, open the cart page, or start checkout). Persist the cart ID in an httpOnly cookie (_aiminaabee_cart_id in the Next.js template). Do not use localStorage on Next.js App Router.

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

Tip: Learn how to install and configure the JS SDK in the Getting started guide.

Put this in src/lib/data/cart.ts with "use server". Resolve the region from the URL country, not from the store code:

ts

tsCopy
"use server"
export async function getOrSetCart(storeCode: string, countryCode: string) {
const region = await getRegion(storeCode, countryCode)
if (!region) {
throw new Error(`Region not found for country code: ${countryCode}`)
}
let cart = await retrieveCart(storeCode)
const headers = { ...(await getAuthHeaders()) }
if (!cart) {
const { cart: createdCart } = await sdk.store.cart.create(
storeCode,
{ region_id: region.id },
{},
headers
)
await setCartId(createdCart.id)
revalidateTag(await getCacheTag("carts"))
cart = createdCart
}
if (cart.region_id !== region.id) {
await sdk.store.cart.update(
storeCode,
cart.id,
{ region_id: region.id },
{},
headers
)
revalidateTag(await getCacheTag("carts"))
}
return cart
}

setCartId writes _aiminaabee_cart_id. getAuthHeaders reads _aiminaabee_jwt. Both live in src/lib/data/cookies.ts.

Call getOrSetCart(storeCode, countryCode) from add-to-cart and checkout — not from a client useEffect on every page.

JS SDK

ts

tsCopy
sdk.store.cart.create(storeCode, {
region_id: region.id,
})
.then(({ cart }) => {
// persist cart.id (httpOnly cookie on Next.js)
})

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.

Cart Currency

A cart's currency is set from the region you associate it with. If you pass a currency_code when creating a cart, it must match the region's currency. Otherwise, an error is thrown.

To avoid errors, omit currency_code and let Aiminaabee set it from the region automatically.


Cart's Sales Channel Scope

Always pass the publishable API key in the header of the request (the JS SDK does this automatically, as explained in Publishable API Keys). Aiminaabee associates the cart with the sales channel(s) of that key.

This scopes the products you retrieve, available inventory, and the sales channel of the order created from the cart. If you want a different sales channel, or the key has several and you need to pick one, pass sales_channel_id to the Create Cart API route.

Aiminaabee doesn't reject a product variant added to the cart when its product isn't available in the cart's sales channel. Sales channel availability filters the products you retrieve, but Aiminaabee doesn't enforce it when adding items. Refer to Enforce Sales Channel Availability in Carts to add that validation.

ts

tsCopy
sdk.store.cart.create(storeCode, {
region_id: region.id,
sales_channel_id: "sc_123",
})
.then(({ cart }) => {
console.log(cart)
})

Associate Customer with Cart

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

If the cart was created for a guest, then the customer logs in, transfer the cart as explained in Update Cart. On Next.js, call transferCart(storeCode) after setAuthToken.


Set Cart's Locale

Prerequisites

  • Translation Module Configured

By default, items in the cart use the product's original content. If your storefront supports localization, set the cart's locale so item contents match the customer's language.

Pass locale to the Create Cart API route:

ts

tsCopy
sdk.store.cart.create(storeCode, {
region_id: region.id,
locale: "fr-FR",
})
.then(({ cart }) => {
console.log(cart)
})

When the locale is set, cart items use that locale if translations exist. You can also update the cart's locale later.


Store Cart Details in React Context

A React context that creates the cart in useEffect is for SPAs only. On Next.js App Router, keep the cart ID in the cookie and load the cart in server components or server actions. See Cart React Context.