Skip to content

create-cart ​

Method: POST
Path: /carts

Tags: Carts

Summary ​

Create cart

Description ​

Creates a new cart with an initial set of items. Returns a Cart object. Attach optional metadata for custom key-value pairs.

Storefront SDK Usage ​

SDK Method: sdk.cart.createCart()

Example:

typescript
const { data, error, events } = await sdk.cart.createCart({
  items: [
    {
      product_id: "01H9XYZ12345ABCDE",
      variant_id: null,
      quantity: 2
    },
    {
      product_id: "01H9ABC67890FGHIJ",
      variant_id: "01H9XYZ67890KLMNO",
      quantity: 1
    }
  ],
  metadata: {
    "source": "web",
    "campaign": "summer_sale"
  }
});

if (error) {
  console.error("Failed to create cart:", error.message);
} else {
  console.log("Cart created:", data.cart.id);
  // Forward `events` to `@commercengine/analytics` so the browser and
  // server copies dedupe on their server-issued `event_id`
  console.log("Events:", events.map((event) => event.event_id));
}

TypeScript Definition ​

typescript
"create-cart": {
        parameters: {
            query?: never;
            header?: {
                /** @description A ULID you propose as the `event_id` of the first entry this request returns in `events`. Used only when it is a valid ULID minted within a day of now and not already used; otherwise the server mints one. The `event_id` returned in `events` is the one to send. */
                "X-CE-Event-ID"?: components["parameters"]["ProposedEventId"];
            };
            path?: never;
            cookie?: never;
        };
        requestBody: {
            content: {
                "application/json": {
                    items: components["schemas"]["UpdateCartItem"][];
                    metadata?: {
                        [key: string]: string;
                    };
                };
            };
        };
        responses: {
            /** @description OK */
            200: {
                headers: {
                    [name: string]: unknown;
                };
                content: {
                    "application/json": {
                        message: string;
                        success: boolean;
                        content: {
                            cart: components["schemas"]["Cart"];
                        };
                        /** @description Analytics events to emit for the change this request made, each with a server-issued `event_id`. Replay them to your analytics destinations so your client-side and server-side copies of the same action dedupe. Empty when the request produced no analytics event. */
                        events: components["schemas"]["StorefrontResponseEvent"][];
                    };
                };
            };
            400: components["responses"]["BadRequest"];
            401: components["responses"]["Unauthorized"];
            404: components["responses"]["NotFound"];
        };
    };

Component References ​

ReferenceResolves To
components["parameters"]["ProposedEventId"]ProposedEventId
components["schemas"]["UpdateCartItem"]UpdateCartItem
components["schemas"]["Cart"]Cart
components["schemas"]["StorefrontResponseEvent"]StorefrontResponseEvent
components["responses"]["BadRequest"]BadRequest
components["responses"]["Unauthorized"]Unauthorized
components["responses"]["NotFound"]NotFound

Parameters ​

  • X-CE-Event-ID (header): A ULID you propose as the event_id of the first entry this request returns in events. Used only when it is a valid ULID minted within a day of now and not already used; otherwise the server mints one. The event_id returned in events is the one to send.

Request Body ​

Content Types: application/json

Responses ​

200 ​

OK

400 ​

Bad request

401 ​

Not authorized for given operation on the Resource

404 ​

Requested resource not found

OpenAPI Definition ​

json
{
  "tags": [
    "Carts"
  ],
  "operationId": "create-cart",
  "summary": "Create cart",
  "description": "Creates a new cart with an initial set of items. Returns a `Cart` object. Attach optional `metadata` for custom key-value pairs.",
  "externalDocs": {
    "url": "https://llm-docs.commercengine.io/storefront/operations/create-cart",
    "description": "API reference for the create-cart operation"
  },
  "parameters": [
    {
      "$ref": "#/components/parameters/ProposedEventId"
    }
  ],
  "requestBody": {
    "required": true,
    "content": {
      "application/json": {
        "schema": {
          "type": "object",
          "required": [
            "items"
          ],
          "properties": {
            "items": {
              "type": "array",
              "items": {
                "$ref": "#/components/schemas/UpdateCartItem"
              }
            },
            "metadata": {
              "type": "object",
              "additionalProperties": {
                "type": "string"
              }
            }
          }
        }
      }
    }
  },
  "responses": {
    "200": {
      "description": "OK",
      "content": {
        "application/json": {
          "schema": {
            "type": "object",
            "required": [
              "message",
              "success",
              "content",
              "events"
            ],
            "properties": {
              "message": {
                "type": "string"
              },
              "success": {
                "type": "boolean"
              },
              "content": {
                "properties": {
                  "cart": {
                    "$ref": "#/components/schemas/Cart"
                  }
                },
                "required": [
                  "cart"
                ],
                "type": "object"
              },
              "events": {
                "description": "Analytics events to emit for the change this request made, each with a server-issued `event_id`. Replay them to your analytics destinations so your client-side and server-side copies of the same action dedupe. Empty when the request produced no analytics event.",
                "type": "array",
                "items": {
                  "$ref": "#/components/schemas/StorefrontResponseEvent"
                }
              }
            }
          }
        }
      },
      "links": {
        "AddItemToCart": {
          "operationId": "update-cart",
          "parameters": {
            "id": "$response.body#/content/cart/id"
          },
          "description": "Add items to the newly created cart."
        },
        "SetCartAddress": {
          "operationId": "create-cart-address",
          "parameters": {
            "id": "$response.body#/content/cart/id"
          },
          "description": "Set billing and shipping address on this cart."
        },
        "SetCartFulfillment": {
          "operationId": "update-fulfillment-preference",
          "parameters": {
            "id": "$response.body#/content/cart/id"
          },
          "description": "Set the fulfillment preference on this cart."
        }
      }
    },
    "400": {
      "$ref": "#/components/responses/BadRequest"
    },
    "401": {
      "$ref": "#/components/responses/Unauthorized"
    },
    "404": {
      "$ref": "#/components/responses/NotFound"
    }
  },
  "security": [
    {
      "Authorization": []
    }
  ],
  "x-speakeasy-group": "carts",
  "x-speakeasy-ignore": false,
  "x-speakeasy-name-override": "createCart"
}

Auto-generated from OpenAPI spec and TypeScript definitions

Last updated: