# Coupon routes

URL: /code/api/coupon · Id: `page:code/api/coupon`

> Every Coupon Verb as an api.sb route, with its inputs, result, refusals and an example.

Reference for [Coupon](/business/nouns/coupon).

| Noun | System | Run by |
| --- | --- | --- |
| [Coupon](/business/nouns/coupon) | [Billing](/systems/billing) | [Billing Specialist](/agents/finance/billing-specialist) |

### Coupon.create
Creates a Coupon. [Verb page](/business/nouns/coupon/create?interface=api).

```text
POST /{namespace}/coupons
```

Body, as JSON: the inputs. Emits `Coupon.created`.

**Inputs**

| Input | Type | Required | Description |
| --- | --- | --- | --- |
| `percentOff` | `number` | No | The percent taken off each charge it applies to; set this or `amountOff`. Never randomized. |
| `amountOff` | `money` | No | The fixed amount taken off each charge it applies to. Never randomized. |
| `duration` | `string` | Yes | How long it applies: `once`, `repeating` for `months`, or `forever`. |
| `months` | `integer` | No | How many months a `repeating` Coupon applies. |
| `code` | `string` | No | The promotion code a buyer types at checkout, when the Coupon has one. |
| `redeemBy` | `datetime` | No | The last moment it can be redeemed. |
| `maxRedemptions` | `integer` | No | How many times it can be redeemed across all Customers. |

**Returns**

| Output | Type | Required | Description |
| --- | --- | --- | --- |
| `coupon` | [Coupon](/business/nouns/coupon) | Yes | The Coupon. |

**Refuses when**

| Check | Error | Fix | Then call |
| --- | --- | --- | --- |
| The key is valid, but its scopes do not allow this operation. | [`forbidden`](/code/api/errors/forbidden) | Use a key whose scopes cover the operation, or ask an owner of the namespace to grant them. | None. |

**Example**

```bash
curl -X POST https://api.sb/leaseline.app/coupons \
  -H "Authorization: Bearer $STARTUP_BUILDER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"duration":"once"}'
```

```json
{
  "api": "https://api.sb/leaseline.app",
  "coupon": {
    "$id": "https://api.sb/leaseline.app/coupons/coupon_rAfxMa2HGB",
    "$type": "Coupon",
    "duration": "once"
  },
  "action": {
    "$id": "https://api.sb/leaseline.app/actions/act_br07HsG8MG",
    "operation": "Coupon.create",
    "status": "done"
  },
  "links": {
    "self": "https://api.sb/leaseline.app/actions/act_br07HsG8MG"
  }
}
```

### Coupon.get
Returns one Coupon by id. [Verb page](/business/nouns/coupon/get?interface=api).

```text
GET /{namespace}/coupons/{id}
```

Path: `id`. Query: the other inputs. Reads only: it emits no Event.

**Inputs**

| Input | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | `string` | Yes | The id of the Coupon. |
| `at` | `string` | No | A time or Version name: the record as it stood then. Unset: as it stands now. |

**Returns**

| Output | Type | Required | Description |
| --- | --- | --- | --- |
| `coupon` | [Coupon](/business/nouns/coupon) | Yes | The Coupon. |

**Refuses when**

| Check | Error | Fix | Then call |
| --- | --- | --- | --- |
| The key is valid, but its scopes do not allow this operation. | [`forbidden`](/code/api/errors/forbidden) | Use a key whose scopes cover the operation, or ask an owner of the namespace to grant them. | None. |

**Example**

```bash
curl https://api.sb/leaseline.app/coupons/coupon_rAfxMa2HGB \
  -H "Authorization: Bearer $STARTUP_BUILDER_API_KEY"
```

```json
{
  "api": "https://api.sb/leaseline.app",
  "$type": "Coupon",
  "$id": "https://api.sb/leaseline.app/coupons/coupon_rAfxMa2HGB",
  "coupon": {
    "$id": "https://api.sb/leaseline.app/coupons/coupon_rAfxMa2HGB",
    "$type": "Coupon",
    "duration": "once"
  },
  "links": {
    "self": "https://api.sb/leaseline.app/coupons/coupon_rAfxMa2HGB",
    "up": "https://api.sb/leaseline.app/coupons"
  }
}
```

### Coupon.list
Returns Coupons, newest first, one page at a time. [Verb page](/business/nouns/coupon/list?interface=api).

```text
GET /{namespace}/coupons
```

Query: the inputs. Reads only: it emits no Event.

**Inputs**

| Input | Type | Required | Description |
| --- | --- | --- | --- |
| `limit` | `integer` | No | At most this many records, up to 100. The default is 20. |
| `after` | `string` | No | The cursor from the previous page. |
| `where` | `string` | No | A filter over the properties, such as `paidAt = null and total.amount > 500`. |
| `sort` | `string` | No | Properties to sort by, comma-separated; a leading minus sorts descending. |
| `total` | `boolean` | No | True adds the match count as `meta.total`, exact up to 10,000 matches and estimated above. |
| `facets` | `string` | No | Properties to count by value, comma-separated, returned as `meta.facets`. |
| `fields` | `string` | No | Properties to return on each record, comma-separated; `$id` and `$type` always return. |
| `include` | `string` | No | Related records to embed in place of their URLs, comma-separated. |
| `depth` | `integer` | No | How many levels of related records to embed. The default is 1. |
| `q` | `string` | No | Words to match across the text properties, ranked by relevance. |
| `match` | `string` | No | Ranks the search by `words`, or `meaning`; words is the default. |

**Returns**

| Output | Type | Required | Description |
| --- | --- | --- | --- |
| `coupons` | list of [Coupon](/business/nouns/coupon) | Yes | One page of Coupons. |

**Refuses when**

| Check | Error | Fix | Then call |
| --- | --- | --- | --- |
| The key is valid, but its scopes do not allow this operation. | [`forbidden`](/code/api/errors/forbidden) | Use a key whose scopes cover the operation, or ask an owner of the namespace to grant them. | None. |

**Example**

```bash
curl "https://api.sb/leaseline.app/coupons?limit=20" \
  -H "Authorization: Bearer $STARTUP_BUILDER_API_KEY"
```

```json
{
  "api": "https://api.sb/leaseline.app",
  "$type": "Coupon",
  "coupons": [
    {
      "$id": "https://api.sb/leaseline.app/coupons/coupon_rAfxMa2HGB",
      "$type": "Coupon",
      "duration": "once"
    }
  ],
  "meta": {
    "limit": 20,
    "after": "coupon_rAfxMa2HGB"
  },
  "links": {
    "self": "https://api.sb/leaseline.app/coupons?limit=20",
    "next": "https://api.sb/leaseline.app/coupons?after=coupon_rAfxMa2HGB"
  }
}
```

### Coupon.update
Changes the given properties of a Coupon. [Verb page](/business/nouns/coupon/update?interface=api).

```text
PATCH /{namespace}/coupons/{id}
```

Path: `id`. Body, as JSON: the other inputs. Emits `Coupon.updated`.

**Inputs**

| Input | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | `string` | Yes | The id of the Coupon. |
| `percentOff` | `number` | No | The percent taken off each charge it applies to; set this or `amountOff`. Never randomized. |
| `amountOff` | `money` | No | The fixed amount taken off each charge it applies to. Never randomized. |
| `duration` | `string` | No | How long it applies: `once`, `repeating` for `months`, or `forever`. |
| `months` | `integer` | No | How many months a `repeating` Coupon applies. |
| `code` | `string` | No | The promotion code a buyer types at checkout, when the Coupon has one. |
| `redeemBy` | `datetime` | No | The last moment it can be redeemed. |
| `maxRedemptions` | `integer` | No | How many times it can be redeemed across all Customers. |

**Returns**

| Output | Type | Required | Description |
| --- | --- | --- | --- |
| `coupon` | [Coupon](/business/nouns/coupon) | Yes | The Coupon. |

**Refuses when**

| Check | Error | Fix | Then call |
| --- | --- | --- | --- |
| The key is valid, but its scopes do not allow this operation. | [`forbidden`](/code/api/errors/forbidden) | Use a key whose scopes cover the operation, or ask an owner of the namespace to grant them. | None. |

**Example**

```bash
curl -X PATCH https://api.sb/leaseline.app/coupons/coupon_rAfxMa2HGB \
  -H "Authorization: Bearer $STARTUP_BUILDER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"percentOff":1}'
```

```json
{
  "api": "https://api.sb/leaseline.app",
  "coupon": {
    "$id": "https://api.sb/leaseline.app/coupons/coupon_rAfxMa2HGB",
    "$type": "Coupon",
    "duration": "once",
    "percentOff": 1
  },
  "action": {
    "$id": "https://api.sb/leaseline.app/actions/act_br07HsG8MG",
    "operation": "Coupon.update",
    "status": "done"
  },
  "links": {
    "self": "https://api.sb/leaseline.app/actions/act_br07HsG8MG"
  }
}
```