Skip to content

Add an item to a guest cart

POST
/public/stores/{storeRef}/cart/items
curl --request POST \
--url https://apibase.ecomweb.app/api/v1/public/stores/example/cart/items \
--header 'Content-Type: application/json' \
--header 'x-storefront-api-key: <x-storefront-api-key>' \
--data '{ "product_id": 1, "variant_id": 12, "quantity": 1 }'

Adds a product variant to the guest cart for the x-session-id header and creates the cart when there is none. Adding a product and variant that is already in the cart raises that line’s quantity by the amount sent. Prices are not stored here: read the cart to get priced lines. The body is not validated up front: a missing or invalid product_id fails with a server error rather than a 400.

storeRef
required
string

The store’s ref, as returned by GET /stores.

x-session-id
string

An ID that identifies the visitor’s guest cart. Generate a random value (a UUID works) the first time, keep it in the browser and send it on every guest cart request. Without it, requests are not tied to a cart.

Media typeapplication/json
object
product_id
required

ID of the product to add.

number

Example

1
variant_id

ID of the variant to add. Every purchasable product has at least one variant, so send it; the cart keeps the line without it, but checkout needs it.

number

Example

12
quantity

Units to add. Defaults to 1. Adding a product and variant that is already in the cart increases that line’s quantity.

number
default: 1

Example

2
Media typeapplication/json
object
cart_id
required

ID of the cart the line was added to. A cart is created when the shopper has none.

string
item
required

The line, with its quantity after the change.

object
id

Cart line ID.

string
cart_id

ID of the cart the line belongs to.

string
product_id
required

ID of the product.

number
variant_id

ID of the variant, or null when the line has none.

number
nullable
quantity
required

Units in the line.

number
created_at

When the line was added, as an ISO 8601 timestamp.

string
updated_at

When the line last changed, as an ISO 8601 timestamp.

string

Example generated

{
"cart_id": "example",
"item": {
"id": "example",
"cart_id": "example",
"product_id": 1,
"variant_id": 1,
"quantity": 1,
"created_at": "example",
"updated_at": "example"
}
}

The key is missing, invalid, revoked or expired.

Media typeapplication/json
One of:
object
statusCode
required

The HTTP status code.

number
message
required

What went wrong; validation failures list one message per problem.

One of:
string
error

The HTTP status text, such as Bad Request.

string

Example generated

{
"statusCode": 1,
"message": [
"example"
],
"error": "example"
}

The key may not do this: a missing scope, a readonly key, a publishable key on a Store API route, or a disallowed origin.

Media typeapplication/json
One of:
object
statusCode
required

The HTTP status code.

number
message
required

What went wrong; validation failures list one message per problem.

One of:
string
error

The HTTP status text, such as Bad Request.

string

Example generated

{
"statusCode": 1,
"message": [
"example"
],
"error": "example"
}