# Store promotions

> What a discount code leaves as profit on your B7R store, the codes, creating, changing and stopping them, and free shipping.

Source: https://docs.b7r.app/store-promotions

> Ids, names, prices and times in the examples are samples. Read the real values from the API.

Reading needs the key area `my_store:read` and changing needs `my_store:write` beside it. The calls answer as the [My Store](https://docs.b7r.app/my-store) calls do when B7R stores are not open to your account (`409 feature_not_available`), when B7R cannot tell (`503 service_unavailable`), and before you open a store in the B7R app (`404 not_found` with `store_missing`). A change counts against `store_writes_per_minute` (`GET /v1/me?include=limits`) and takes an `Idempotency-Key`; a call sent again with the same key answers what the first answered.

A floor is the lowest price the store allows a product, worked out from its cost and B7R’s settings: it is null, and the promotions say why, when it cannot be worked out right now.

## Read what a code leaves as profit

`GET /v1/my-store/promotions`

For each product on the store, at most 1,000 (`truncated: true` when the store holds that many or more): its price, its floor, the profit on one item sold with no code, and the largest percent and fixed code that keep it at its floor. A code applies to the whole store, so the top-level `max_percent` and `max_fixed` are the largest the whole store allows: a code above them is refused.

When the floors cannot be worked out right now, the answer is `available: false` with the reason, `no_active_carrier` or `pricing_unavailable`; a code cannot be saved until they can.

Key: A key with the `my_store:read` area.

Answer:



Errors: `unauthorized` (401), `area_forbidden` (403), `account_inactive` (403), `connection_disabled` (403), `https_required` (403), `not_found` (404), `invalid_request` (400), `rate_limited` (429), `internal_error` (500), `upstream_unavailable` (502), `api_unavailable` (503), `request_expired` (503), `service_unavailable` (503), `upstream_timeout` (504), `feature_not_available` (409). See [Errors](https://docs.b7r.app/errors).

Request:

```bash
curl https://api.b7r.app/v1/my-store/promotions \
  -H "Authorization: Bearer $B7R_API_KEY"
```

```javascript
const res = await fetch("https://api.b7r.app/v1/my-store/promotions", {
  headers: {
    Authorization: `Bearer ${process.env.B7R_API_KEY}`,
  },
});
const data = await res.json();
```

```python
import os, requests

res = requests.get(
    "https://api.b7r.app/v1/my-store/promotions",
    headers={
        "Authorization": f"Bearer {os.environ['B7R_API_KEY']}",
    },
    timeout=30,
)
data = res.json()
```

Answer (200 OK):

```json
{
  "available": true,
  "max_percent": 30,
  "max_fixed": "36.00",
  "products": [
    {
      "store_product_id": "3c4d5e6f-7a8b-4c9d-8e1f-2a3b4c5d6e7f",
      "product_id": "4d5e6f7a-8b9c-4d0e-9f2a-3b4c5d6e7f8a",
      "name": {
        "ar": "قميص الخط العربي",
        "en": "Calligraphy shirt"
      },
      "retail_price": "120.00",
      "floor_price": "84.00",
      "profit": "31.20",
      "max_percent": 30,
      "max_fixed": "36.00"
    }
  ],
  "truncated": false
}
```

## List the discount codes

`GET /v1/my-store/codes`

Newest first, stopped and ended codes included, at most 1,000 (`truncated: true` when the list reached that many). `value` has two decimals: a percent for `percent`, SAR for `fixed`. `uses` counts the orders that used the code; `active` is false once you stopped it.

Key: A key with the `my_store:read` area.

Answer:

- `data` (array of object, required): At most 1000 items.
  - `id` (string (uuid), required)
  - `code` (string, required)
  - `kind` (string, required): One of `percent`, `fixed`.
  - `value` (string, required)
  - `starts_at` (string, required)
  - `ends_at` (string or null, required)
  - `max_uses` (integer or null, required)
  - `uses` (integer, required)
  - `min_order` (string or null, required)
  - `active` (boolean, required)
- `truncated` (boolean, required)

Errors: `unauthorized` (401), `area_forbidden` (403), `account_inactive` (403), `connection_disabled` (403), `https_required` (403), `not_found` (404), `invalid_request` (400), `rate_limited` (429), `internal_error` (500), `upstream_unavailable` (502), `api_unavailable` (503), `request_expired` (503), `service_unavailable` (503), `upstream_timeout` (504), `feature_not_available` (409). See [Errors](https://docs.b7r.app/errors).

Request:

```bash
curl https://api.b7r.app/v1/my-store/codes \
  -H "Authorization: Bearer $B7R_API_KEY"
```

```javascript
const res = await fetch("https://api.b7r.app/v1/my-store/codes", {
  headers: {
    Authorization: `Bearer ${process.env.B7R_API_KEY}`,
  },
});
const data = await res.json();
```

```python
import os, requests

res = requests.get(
    "https://api.b7r.app/v1/my-store/codes",
    headers={
        "Authorization": f"Bearer {os.environ['B7R_API_KEY']}",
    },
    timeout=30,
)
data = res.json()
```

Answer (200 OK):

```json
{
  "data": [
    {
      "id": "6f7a8b9c-0d1e-4f2a-9b4c-5d6e7f8a9b0c",
      "code": "RAMADAN",
      "kind": "percent",
      "value": "10.00",
      "starts_at": "2026-11-20T00:00:00.000Z",
      "ends_at": null,
      "max_uses": 100,
      "uses": 3,
      "min_order": null,
      "active": true
    }
  ],
  "truncated": false
}
```

The calls below change the discount codes and free shipping. Each answers the code it changed, as the list gives it, or the threshold; `{id}` is the code’s `id`. One that is not on your store, or is on another account’s, answers `404 not_found`, the same as one that does not exist.

> If an answer is lost and you send the call again with a new key, a second code answers `409 my_store_rejected` with `code_taken`, naming the code in `existing_id`. Read `GET /v1/my-store/codes` to see what happened. The same key always answers what the first call answered.

## Create a discount code

`POST /v1/my-store/codes`

Every field is required; send `null` for one you leave empty. `code` is 2 to 40 characters with no spaces, unique on the store ignoring case (`code_taken`, with `existing_id`). `value` has at most two decimals: a percent above 0 and at most 100 for `percent`, SAR for `fixed`. `starts_at` null starts the code now; `ends_at`, `max_uses` and `min_order` null mean no end, no use limit and no minimum order.

A code applies to the whole store, so it is checked against the floor of every product on it: one that would take a product below its floor is refused with `code_breaks_floor`. `GET /v1/my-store/promotions` gives the largest percent and fixed code the store allows. A value refused at its field names it: `invalid_code`, `invalid_value`, `invalid_window` (at `/body/ends_at`), `invalid_max_uses` or `invalid_min_order`.

Key: A key with the `my_store:write` area. Send `Idempotency-Key` and `Content-Type: application/json`.

Body:

- `code` (string, required): At most 40.
- `kind` (string, required): One of `percent`, `fixed`.
- `value` (string, required)
- `ends_at` (string (date-time) or null, required)
- `max_uses` (integer or null, required)
- `min_order` (string or null, required)
- `starts_at` (string (date-time) or null, required)

Answer:

- `code` (object, required)
  - `id` (string (uuid), required)
  - `code` (string, required)
  - `kind` (string, required): One of `percent`, `fixed`.
  - `value` (string, required)
  - `starts_at` (string, required)
  - `ends_at` (string or null, required)
  - `max_uses` (integer or null, required)
  - `uses` (integer, required)
  - `min_order` (string or null, required)
  - `active` (boolean, required)

Errors: `unauthorized` (401), `area_forbidden` (403), `account_inactive` (403), `connection_disabled` (403), `https_required` (403), `not_found` (404), `invalid_request` (400), `idempotency_key_required` (400), `request_too_large` (413), `unsupported_media_type` (415), `rate_limited` (429), `internal_error` (500), `upstream_unavailable` (502), `api_unavailable` (503), `request_expired` (503), `service_unavailable` (503), `upstream_timeout` (504), `idempotency_mismatch` (409), `idempotency_in_flight` (409), `verification_required` (403), `feature_not_available` (409), `my_store_rejected` (409). See [Errors](https://docs.b7r.app/errors).

Request:

```bash
curl -X POST https://api.b7r.app/v1/my-store/codes \
  -H "Authorization: Bearer $B7R_API_KEY" \
  -H "Idempotency-Key: $(uuidgen 2>/dev/null || node -e 'console.log(crypto.randomUUID())')" \
  -H "Content-Type: application/json" \
  -d '{
    "code": "RAMADAN",
    "kind": "percent",
    "value": "10.00",
    "starts_at": null,
    "ends_at": null,
    "max_uses": 100,
    "min_order": null
  }'
```

```javascript
const res = await fetch("https://api.b7r.app/v1/my-store/codes", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.B7R_API_KEY}`,
    "Idempotency-Key": crypto.randomUUID(),
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "code": "RAMADAN",
    "kind": "percent",
    "value": "10.00",
    "starts_at": null,
    "ends_at": null,
    "max_uses": 100,
    "min_order": null
  }),
});
const data = await res.json();
```

```python
import os, requests, uuid

res = requests.post(
    "https://api.b7r.app/v1/my-store/codes",
    headers={
        "Authorization": f"Bearer {os.environ['B7R_API_KEY']}",
        "Idempotency-Key": str(uuid.uuid4()),
    },
    json={
        "code": "RAMADAN",
        "kind": "percent",
        "value": "10.00",
        "starts_at": None,
        "ends_at": None,
        "max_uses": 100,
        "min_order": None
    },
    timeout=30,
)
data = res.json()
```

Answer (201 Created):

```json
{
  "code": {
    "id": "6f7a8b9c-0d1e-4f2a-9b4c-5d6e7f8a9b0c",
    "code": "RAMADAN",
    "kind": "percent",
    "value": "10.00",
    "starts_at": "2026-11-20T00:00:00.000Z",
    "ends_at": null,
    "max_uses": 100,
    "uses": 3,
    "min_order": null,
    "active": true
  }
}
```

## Change a discount code

`PATCH /v1/my-store/codes/{id}`

Replaces the whole code, as the app’s edit does: send every field, as for creating it, and a field you do not change with the value `GET /v1/my-store/codes` gives. `starts_at` is required here: send back the one you read; `null` is refused at `/body/starts_at` (`invalid_window`). The code is checked against the floors again. The orders that already used it keep their discount.

Key: A key with the `my_store:write` area. Send `Content-Type: application/json`, and `If-Match` with the `ETag` of your last read; `Idempotency-Key` is optional.

Path:

- `id` (string (uuid), required)

Body:

- `code` (string, required): At most 40.
- `kind` (string, required): One of `percent`, `fixed`.
- `value` (string, required)
- `ends_at` (string (date-time) or null, required)
- `max_uses` (integer or null, required)
- `min_order` (string or null, required)
- `starts_at` (string (date-time) or null, required)

Answer:

- `code` (object, required)
  - `id` (string (uuid), required)
  - `code` (string, required)
  - `kind` (string, required): One of `percent`, `fixed`.
  - `value` (string, required)
  - `starts_at` (string, required)
  - `ends_at` (string or null, required)
  - `max_uses` (integer or null, required)
  - `uses` (integer, required)
  - `min_order` (string or null, required)
  - `active` (boolean, required)

Errors: `unauthorized` (401), `area_forbidden` (403), `account_inactive` (403), `connection_disabled` (403), `https_required` (403), `not_found` (404), `invalid_request` (400), `idempotency_key_required` (400), `request_too_large` (413), `unsupported_media_type` (415), `rate_limited` (429), `internal_error` (500), `upstream_unavailable` (502), `api_unavailable` (503), `request_expired` (503), `service_unavailable` (503), `upstream_timeout` (504), `idempotency_mismatch` (409), `idempotency_in_flight` (409), `verification_required` (403), `feature_not_available` (409), `my_store_rejected` (409). See [Errors](https://docs.b7r.app/errors).

Request:

```bash
curl -X PATCH https://api.b7r.app/v1/my-store/codes/{id} \
  -H "Authorization: Bearer $B7R_API_KEY" \
  -H "If-Match: $ETAG" \
  -H "Content-Type: application/json" \
  -d '{
    "code": "RAMADAN",
    "kind": "percent",
    "value": "15.00",
    "starts_at": "2026-11-20T00:00:00.000Z",
    "ends_at": null,
    "max_uses": 100,
    "min_order": null
  }'
```

```javascript
const res = await fetch("https://api.b7r.app/v1/my-store/codes/{id}", {
  method: "PATCH",
  headers: {
    Authorization: `Bearer ${process.env.B7R_API_KEY}`,
    "If-Match": etag, // the ETag header of your last read
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "code": "RAMADAN",
    "kind": "percent",
    "value": "15.00",
    "starts_at": "2026-11-20T00:00:00.000Z",
    "ends_at": null,
    "max_uses": 100,
    "min_order": null
  }),
});
const data = await res.json();
```

```python
import os, requests

res = requests.patch(
    "https://api.b7r.app/v1/my-store/codes/{id}",
    headers={
        "Authorization": f"Bearer {os.environ['B7R_API_KEY']}",
        "If-Match": etag,  # the ETag header of your last read
    },
    json={
        "code": "RAMADAN",
        "kind": "percent",
        "value": "15.00",
        "starts_at": "2026-11-20T00:00:00.000Z",
        "ends_at": None,
        "max_uses": 100,
        "min_order": None
    },
    timeout=30,
)
data = res.json()
```

Answer (200 OK):

```json
{
  "code": {
    "id": "6f7a8b9c-0d1e-4f2a-9b4c-5d6e7f8a9b0c",
    "code": "RAMADAN",
    "kind": "percent",
    "value": "10.00",
    "starts_at": "2026-11-20T00:00:00.000Z",
    "ends_at": null,
    "max_uses": 100,
    "uses": 3,
    "min_order": null,
    "active": true
  }
}
```

## Stop a discount code

`POST /v1/my-store/codes/{id}/deactivate`

Shoppers can no longer use it; the orders that used it keep their record, and it stays in the list with `active: false`.

Key: A key with the `my_store:write` area. Send `Idempotency-Key`.

Path:

- `id` (string (uuid), required)

Answer:

- `code` (object, required)
  - `id` (string (uuid), required)
  - `code` (string, required)
  - `kind` (string, required): One of `percent`, `fixed`.
  - `value` (string, required)
  - `starts_at` (string, required)
  - `ends_at` (string or null, required)
  - `max_uses` (integer or null, required)
  - `uses` (integer, required)
  - `min_order` (string or null, required)
  - `active` (boolean, required)

Errors: `unauthorized` (401), `area_forbidden` (403), `account_inactive` (403), `connection_disabled` (403), `https_required` (403), `not_found` (404), `invalid_request` (400), `idempotency_key_required` (400), `request_too_large` (413), `unsupported_media_type` (415), `rate_limited` (429), `internal_error` (500), `upstream_unavailable` (502), `api_unavailable` (503), `request_expired` (503), `service_unavailable` (503), `upstream_timeout` (504), `idempotency_mismatch` (409), `idempotency_in_flight` (409), `feature_not_available` (409). See [Errors](https://docs.b7r.app/errors).

Request:

```bash
curl -X POST https://api.b7r.app/v1/my-store/codes/{id}/deactivate \
  -H "Authorization: Bearer $B7R_API_KEY" \
  -H "Idempotency-Key: $(uuidgen 2>/dev/null || node -e 'console.log(crypto.randomUUID())')"
```

```javascript
const res = await fetch("https://api.b7r.app/v1/my-store/codes/{id}/deactivate", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.B7R_API_KEY}`,
    "Idempotency-Key": crypto.randomUUID(),
  },
});
const data = await res.json();
```

```python
import os, requests, uuid

res = requests.post(
    "https://api.b7r.app/v1/my-store/codes/{id}/deactivate",
    headers={
        "Authorization": f"Bearer {os.environ['B7R_API_KEY']}",
        "Idempotency-Key": str(uuid.uuid4()),
    },
    timeout=30,
)
data = res.json()
```

Answer (200 OK):

```json
{
  "code": {
    "id": "6f7a8b9c-0d1e-4f2a-9b4c-5d6e7f8a9b0c",
    "code": "RAMADAN",
    "kind": "percent",
    "value": "10.00",
    "starts_at": "2026-11-20T00:00:00.000Z",
    "ends_at": null,
    "max_uses": 100,
    "uses": 3,
    "min_order": null,
    "active": false
  }
}
```

## Set free shipping

`PATCH /v1/my-store/free-shipping`

`threshold` is the order total, in SAR with at most two decimals and above 0, from which shipping is free on your store; `null` switches free shipping off. Another value is refused at `/body/threshold` (`invalid_threshold`).

Key: A key with the `my_store:write` area. Send `Content-Type: application/json`, and `If-Match` with the `ETag` of your last read; `Idempotency-Key` is optional.

Body:

- `threshold` (string or null, required)

Answer:

- `free_shipping_threshold` (string or null, required)

Errors: `unauthorized` (401), `area_forbidden` (403), `account_inactive` (403), `connection_disabled` (403), `https_required` (403), `not_found` (404), `invalid_request` (400), `idempotency_key_required` (400), `request_too_large` (413), `unsupported_media_type` (415), `rate_limited` (429), `internal_error` (500), `upstream_unavailable` (502), `api_unavailable` (503), `request_expired` (503), `service_unavailable` (503), `upstream_timeout` (504), `idempotency_mismatch` (409), `idempotency_in_flight` (409), `verification_required` (403), `feature_not_available` (409). See [Errors](https://docs.b7r.app/errors).

Request:

```bash
curl -X PATCH https://api.b7r.app/v1/my-store/free-shipping \
  -H "Authorization: Bearer $B7R_API_KEY" \
  -H "If-Match: $ETAG" \
  -H "Content-Type: application/json" \
  -d '{
    "threshold": "250.00"
  }'
```

```javascript
const res = await fetch("https://api.b7r.app/v1/my-store/free-shipping", {
  method: "PATCH",
  headers: {
    Authorization: `Bearer ${process.env.B7R_API_KEY}`,
    "If-Match": etag, // the ETag header of your last read
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "threshold": "250.00"
  }),
});
const data = await res.json();
```

```python
import os, requests

res = requests.patch(
    "https://api.b7r.app/v1/my-store/free-shipping",
    headers={
        "Authorization": f"Bearer {os.environ['B7R_API_KEY']}",
        "If-Match": etag,  # the ETag header of your last read
    },
    json={
        "threshold": "250.00"
    },
    timeout=30,
)
data = res.json()
```

Answer (200 OK):

```json
{
  "free_shipping_threshold": "250.00"
}
```
