# Create a coupon

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

A code buyers type at checkout, with the dashboard's rules: a percent or an amount off, every product or the ones you name, a usage limit, one use per buyer email, a start and an end. It works at once unless starts_at is later.

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

## Body (application/json)

- `code` (string, required): What buyers type: 3 to 32 letters and digits, kept in capitals. Up to 64 characters.
- `percent_off` (number): Percent off each product it applies to: 0.01 to 100, at most two decimals. Send this or amount_off. 0.01 to 100.
- `amount_off` (integer): Off the order once, in minor units of the store currency: 500 is $5.00. Send this or percent_off. At least 1.
- `currency` (string): Optional check: the store's currency, ISO 4217. Another currency is refused, since amounts off are in the store's. 3 characters.
- `products` (array of strings): Product IDs: it applies to these only, up to 200. Left out, every product. 1 to 200 items.
- `usage_limit` (integer): Uses allowed, counted in each mode on its own. 1 to 10,000,000.
- `once_per_email` (boolean): One use per buyer email.
- `starts_at` (string): When it starts working, ISO 8601.
- `ends_at` (string): When it stops working, ISO 8601: after starts_at, and not in the past.

## 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 **coupon** 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/coupons \
  -H "Authorization: Bearer $ANYSHOP_LIVE_KEY" \
  -H "Idempotency-Key: $(uuidgen)" \
  -H "Content-Type: application/json" \
  -d '{
    "code": "LAUNCH20",
    "percent_off": 20,
    "usage_limit": 100,
    "once_per_email": true
  }'
```

Node:

```js
const response = await fetch("https://api.anyshop.io/v1/coupons", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.ANYSHOP_LIVE_KEY}`,
    "Idempotency-Key": crypto.randomUUID(),
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "code": "LAUNCH20",
    "percent_off": 20,
    "usage_limit": 100,
    "once_per_email": true
  }),
});
const answer = await response.json();
```

Python:

```python
import os
import requests
import uuid

response = requests.post(
    "https://api.anyshop.io/v1/coupons",
    headers={
        "Authorization": f"Bearer {os.environ['ANYSHOP_LIVE_KEY']}",
        "Idempotency-Key": str(uuid.uuid4()),
    },
    json={
      "code": "LAUNCH20",
      "percent_off": 20,
      "usage_limit": 100,
      "once_per_email": True
    },
)
answer = response.json()
```

Answer (200):

```json
{
  "object": "coupon",
  "id": "cpn_01m3rq8c2e4g6j8l0n2p4r6t8v",
  "code": "LAUNCH20",
  "percent_off": 20,
  "amount_off": null,
  "currency": "USD",
  "products": null,
  "usage_limit": 100,
  "once_per_email": true,
  "starts_at": "2026-10-02T14:21:07.000Z",
  "ends_at": null,
  "times_redeemed": 0,
  "status": "running",
  "created_at": "2026-10-02T14:21:07.000Z",
  "updated_at": "2026-10-02T14:21:07.000Z",
  "archived_at": null
}
```

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.
