# Create a cart

URL: https://docs.sparklayer.io/developers/api/ordering/create-cart
Operation: `POST /api/v1/carts` (operationId `createCart`)
OpenAPI spec: https://docs.sparklayer.io/openapi/ordering.yaml

Creates an empty cart for a customer and returns its `cart_id` with a `201`. Identify the customer with `customer_id` and `customer_lookup_by`: `sparklayer` (the customer's UUID), `platform` (the ID from your eCommerce platform) or `email`. An unknown customer returns `400` (`resource-id-not-found`), and the `Accept-Language` header sets the cart's locale (default `en-US`).

`POST /api/v1/carts`

**Base URLs:** `https://app.sparklayer.io` (Live), `https://test.app.sparklayer.io` (Test)

**Authentication:** send `Authorization: Bearer <access_token>` and `Site-Id: <site id>` with every request. Get the token from [Get an access token](https://docs.sparklayer.io/developers/api/core/get-an-access-token.md); see [Authentication](https://docs.sparklayer.io/developers/authentication.md).

### Example request

```bash
curl -X POST "https://app.sparklayer.io/api/v1/carts" \
  -H "Authorization: Bearer $SPARKLAYER_TOKEN" \
  -H "Site-Id: $SPARKLAYER_SITE_ID" \
  -H "Content-Type: application/json" \
  -d '{
  "customer_id": "<customer_id>",
  "customer_lookup_by": "sparklayer"
}'
```

Set `SPARKLAYER_TOKEN` to an access token and `SPARKLAYER_SITE_ID` to your Site ID. For the test environment, use `https://test.app.sparklayer.io`.

### Header parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `Site-Id` | string | Yes | Your SparkLayer Site ID, from Settings > API in the SparkLayer Dashboard. Example: `jones-climbing` |

### Request body (`application/json`) (required)

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `customer_id` | string | | The customer the cart is for, identified as set by `customer_lookup_by`. |
| `customer_lookup_by` | "sparklayer" \| "platform" \| "email" | | What `customer_id` is: `sparklayer` (the customer's UUID), `platform` (the ID from your eCommerce platform) or `email`. |

```json
{
  "customer_id": "<customer_id>",
  "customer_lookup_by": "sparklayer"
}
```

### Responses

#### 201: The cart was created. Use its `cart_id` in every later request about the cart.

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `cart_id` | string (uuid) | | |

```json
{
  "cart_id": "9151f21f-43ae-43b4-92f3-f4af67cdf544"
}
```

#### Other statuses: An error. The body describes the problem: see [Errors](https://docs.sparklayer.io/developers/errors.md).

`application/problem+json`: the standard error body (below).

### Error body

Error responses with a body use this RFC 7807 problem details object. See [Errors](https://docs.sparklayer.io/developers/errors.md) for every status code and which errors to retry.

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `detail` | string | | Human-readable summary of the error |
| `status` | integer | | HTTP Status code returned from API |
| `title` | string | | Machine-readable error code |
| `type` | string \| null | | |
| `errors` | object[] \| null | | A list of errors related to the call |
| `errors[].code` | string | | Machine-readable error code |
| `errors[].property` | string | | The property the error relates to |
| `errors[].message` | string | | Human readable summary of the error |

```json
{
  "detail": "Data Validation Failed",
  "status": 400,
  "title": "invalid-api-request-contents",
  "type": "https://docs.sparklayer.io/api/errors#invalid-api-request-contents",
  "errors": [
    {
      "code": "resource-not-found",
      "property": "stock_location_id",
      "message": "stock_location_id not found"
    }
  ]
}
```
