# Create or update a customer record

`POST https://api.anyshop.io/v1/customers`

Keeps a customer record by email: a new one, or the one with this email brought up to date. Consent you send is recorded with the source api and the time. Customer records are live mode only, so this needs a live key. Erasing a customer and exporting their data are not in the API: they are in the dashboard, after the owner's fresh two-factor check.

- Part of the [API reference](https://docs.anyshop.io/reference.md), [Customers](https://docs.anyshop.io/reference.md#customers).
- Scope: `customers:write`.
- Live keys only.
- Idempotent with Idempotency-Key.
- Works while the store is suspended.

## Body (application/json)

- `email` (string, required): Their email address. A customer with this email already is updated instead. Up to 254 characters.
- `name` (string): Their name, up to 200 characters; replaces the one kept. Up to 200 characters.
- `marketing_consent` (boolean): true: they agreed to marketing emails, recorded with the source api and the time. false: they withdrew it, recording when. Left out, consent stays as it is.

## Headers

- `Idempotency-Key` (string): Up to 255 characters. A retry with the same key and request gets the first answer, without running twice (24 hours).

## Returns

The **customer** object.

## Errors

| Status | error.type | When |
| --- | --- | --- |
| 400 | `invalid_request` | The request is not valid; `error.param` names the field |
| 401 | `authentication` | No API key, or it is not valid |
| 403 | `permission` | The key does not have the scope this needs, or the store is suspended (store_suspended) |
| 404 | `not_found` | Not found in the key's store and mode |
| 409 | `conflict` | Conflicts with the object's state (type conflict), or an Idempotency-Key reused for another request (type idempotency, code idempotency_key_reused) |
| 429 | `rate_limited` | Too many requests; see Retry-After |

## Example

curl:

```bash
curl -X POST https://api.anyshop.io/v1/customers \
  -H "Authorization: Bearer $ANYSHOP_LIVE_KEY" \
  -H "Idempotency-Key: $(uuidgen)" \
  -H "Content-Type: application/json" \
  -d '{
    "email": "mara@example.com",
    "name": "Mara Lind",
    "marketing_consent": true
  }'
```

Node:

```js
const response = await fetch("https://api.anyshop.io/v1/customers", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.ANYSHOP_LIVE_KEY}`,
    "Idempotency-Key": crypto.randomUUID(),
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "email": "mara@example.com",
    "name": "Mara Lind",
    "marketing_consent": true
  }),
});
const answer = await response.json();
```

Python:

```python
import os
import requests
import uuid

response = requests.post(
    "https://api.anyshop.io/v1/customers",
    headers={
        "Authorization": f"Bearer {os.environ['ANYSHOP_LIVE_KEY']}",
        "Idempotency-Key": str(uuid.uuid4()),
    },
    json={
      "email": "mara@example.com",
      "name": "Mara Lind",
      "marketing_consent": True
    },
)
answer = response.json()
```

Answer (200):

```json
{
  "object": "customer",
  "id": "cus_01m3rx2b4d6f8h0k2m4p6r8t0v",
  "livemode": true,
  "email": "mara@example.com",
  "name": "Mara Lind",
  "orders": 3,
  "spent": {
    "amount": 5997,
    "currency": "USD"
  },
  "refunds": 0,
  "first_order_at": "2026-10-02T14:21:07.000Z",
  "last_order_at": "2026-10-02T14:21:07.000Z",
  "country": "SE",
  "payment_methods": [
    "stripe"
  ],
  "licenses": 1,
  "marketing_consent": {
    "given": true,
    "at": "2026-10-02T14:21:07.000Z",
    "source": "api"
  }
}
```

Answer (400):

```json
{
  "error": {
    "type": "invalid_request",
    "code": "invalid_body",
    "message": "The request is not valid; `error.param` names the field",
    "param": null
  }
}
```

Answer (409):

```json
{
  "error": {
    "type": "conflict",
    "code": "conflict",
    "message": "Conflicts with the object's state (type conflict), or an Idempotency-Key reused for another request (type idempotency, code idempotency_key_reused)",
    "param": null
  }
}
```

The IDs and keys in these examples are made up. Every schema is in the OpenAPI document, https://api.anyshop.io/v1/openapi.json.
