# Coupon codes

Source: https://developers.swell.is/backend-api/coupon-codes

Coupon codes are a collection of eligible codes that can be applied to a cart, order or subscription, by a customer to receive the discount associated with the coupon.

For coupons with multiple codes, codes are generated in batches and are stored on the coupon. In this case, codes are associated with their [generation](https://developers.swell.is/backend-api/coupon-generations) instance and are also tracked with [coupon uses](https://developers.swell.is/backend-api/coupon-uses).

When a coupon is applied, a validation error may be returned if the code is not considered valid or not applicable. The following are approximate error messages, but actual error message text may vary.

- Coupon code not found
- Coupon is not active
- Coupon is not valid (when `date_valid` > current).
- Coupon is expired (when `date_expired` < current)
- Coupon usage limit has been reached
- Coupon code usage limit has been reached
- Coupon is not applicable for this account
- Coupon account usage limit has been reached

## The coupon code model

### Fields

- `id` (objectId): Unique identifier for the coupon code.
- `code` (string, auto): Code used to apply the coupon to a cart, order or subscription. A validation error will be returned if the code is not valid.
- `date_created` (date, auto): Date and time the coupon code was created.
- `date_updated` (date, auto): Date and time the coupon code was last updated.
- `gen_id` (objectId): Unique ID corresponding to a coupon code generation.
- `orders` (Order): Expandable list of orders that have applied the coupon code.
- `parent_id` (objectId, required): ID of the parent coupon, if applicable.
- `parent` (Coupon): Expandable link to the parent coupon.
- `subscriptions` (Subscription): Expandable list of subscriptions that have applied the coupon code.
- `use_count` (int): Number of times the coupon code has been used.

### Example response

```json
{
 "gen_id": "62b5cdffd7ad010018c66249",
 "parent_id": "62b5cdff2e40ab001ac27240",
 "use_count": 0,
 "date_created": "2022-06-24T14:45:20.941Z",
 "use_total": 0,
 "code": "PNB83100017",
 "id": "62b5ce0079c5d80012fe5f65"
}
```


## Create a coupon code

Create a new coupon code.

### Arguments

- `parent_id` (objectId, required): ID of the parent coupon.
- `id` (objectId): ID of the coupon code.
- `code` (string): Unique code to identify the coupon.
- `date_created` (date, auto): Date and time the coupon code was created.
- `date_updated` (date, auto): Date and time the coupon code was last updated.
- `gen_id` (objectId): The ID of the generation instance for a particular coupon code.
- `orders` (Order)
- `parent` (Coupon): Expandable link to the parent coupon.
- `subscriptions` (Subscription)
- `use_count` (int)
- `use_total` (int): Default: `0`.

### Example request

`POST /coupons:codes`

**cURL**

```sh
$ curl https://api.swell.store/coupons:codes \
  -u store-id:secret-key \
	-d code:"ALCHEMY25" \
	-d parent_id:62cc86897ad4f100132c953d \
```

**Node**

```javascript
const { swell } = require('swell-node');
swell.init('store-id', 'secret-key');

await swell.post('/coupons:codes', {
code: 'ALCHEMY25',
parent_id: '62cc86897ad4f100132c953d'
});
```

**PHP**

```php
<?php $swell = new \Swell\Client('store-id', 'secret-key');

$swell->post('/coupons:codes', [
'code' => 'ALCHEMY25',
'parent_id' => '62cc86897ad4f100132c953d'
]);
```

### Example response

```json
{
 "gen_id": "62b5cdffd7ad010018c66249",
 "parent_id": "62b5cdff2e40ab001ac27240",
 "use_count": 0,
 "date_created": "2022-06-24T14:45:20.941Z",
 "use_total": 0,
 "code": "PNB83100017",
 "id": "62b5ce0079c5d80012fe5f65"
}
```


## Retrieve a coupon code

Retrieve an existing coupon code using the ID that was returned when created.

### Arguments

- `id` (objectId, required): ID of the coupon code.

### Example request

`GET /coupons:codes/:id`

**cURL**

```sh
$ curl https://api.swell.store/coupons:codes/62cc86897ad4f100132c953e \
  -u store-id:secret-key
```

**Node**

```javascript
const { swell } = require('swell-node');
swell.init('store-id', 'secret-key');

await swell.get('/coupons:codes/{id}', {
  id: '62cc86897ad4f100132c953e'
});
```

**PHP**

```php
<?php $swell = new \Swell\Client('store-id', 'secret-key');

$swell->get('/coupons:codes/{id}', [
  'id' => '62cc86897ad4f100132c953e'
]);
```

### Example response

```json
{
 "gen_id": "62b5cdffd7ad010018c66249",
 "parent_id": "62b5cdff2e40ab001ac27240",
 "use_count": 0,
 "date_created": "2022-06-24T14:45:20.941Z",
 "use_total": 0,
 "code": "PNB83100017",
 "id": "62b5ce0079c5d80012fe5f65"
}
```


## Update a coupon code

Update an existing coupon code using the ID that was returned when created.

### Arguments

- `id` (objectId, required): Unique identifier for the coupon code.
- `code` (string)

### Example request

`PUT /coupons:codes/:id`

**cURL**

```sh
$ curl https://api.swell.store/coupons:codes/62cc86897ad4f100132c953e \
  -u store-id:secret-key \
  -d code=SKOOMA15 \
  -X PUT
```

**Node**

```javascript
const { swell } = require('swell-node');
swell.init('store-id', 'secret-key');

await swell.put('/coupons:codes/{id}', {
  id: '62cc86897ad4f100132c953e',
  code: 'SKOOMA15'
});
```

**PHP**

```php
<?php $swell = new \Swell\Client('store-id', 'secret-key');

$swell->put('/coupons:codes/{id}', [
  'id' => '62cc86897ad4f100132c953e',
  'code' => 'SKOOMA15'
]);
```

### Example response

```json
{
 "gen_id": "62b5cdffd7ad010018c66249",
 "parent_id": "62b5cdff2e40ab001ac27240",
 "use_count": 0,
 "date_created": "2022-06-24T14:45:20.941Z",
 "use_total": 0,
 "code": "PNB83100017",
 "id": "62b5ce0079c5d80012fe5f65"
}
```


## List all coupon codes

Return a list of coupon codes.

### Arguments

- `expand` (string): Expand link fields and child collections by using the expand argument.

  - For example, `expand=account` would return a related customer account if one exists.

  When the field represents a collection, you can specify the query limit.

  - For example, `expand=variants:10` would return up to 10 records of the variants collection.

  See [expanding](https://developers.swell.is/backend-api/querying/expanding) for more details.
- `fields` (string): Returns only the specified fields in the result.

  - For example `fields=name,slug` would return only the fields `name` and `slug` in the response.

  Supports nested object and array fields using dot-notation.

  - For example, `items.product_id`. The product `id` is always returned.
- `include` (object): Include one or more arbitrary queries in the response which are potentially related to the main query.

  See [including](https://developers.swell.is/backend-api/querying/including) for more details.
- `limit` (int): Limit the number of records returned, ranging between `1` and `1000`. Defaults to `15`. Default: `15`.
- `page` (int): The page number of results to return given the specified or default `limit`.
- `search` (string): A text search is performed using the search argument. Searchable fields are defined by the model.

  - For example, `search=red` would return records containing the word "red" anywhere in the defined text fields.

  See [searching](https://developers.swell.is/backend-api/querying/searching) for more details.
- `sort` (string): Expression to sort results by using a format similar to a SQL sort statement.

  - For example, `sort=name asc` would return records sorted by name ascending.

  See [sorting](https://developers.swell.is/backend-api/querying/sorting) for more details.
- `where` (object): An object with criteria to filter the result.

  - For example, `active=true` would return records containing a field `active` with the value `true`.

  It's also possible to use query operators, for example, `$eq`, `$ne`, `$gt`, and more.

  See [querying](https://developers.swell.is/backend-api/querying) for more details.

### Example request

`GET /coupons:codes`

**cURL**

```sh
$ curl https://api.swell.store/coupons:codes\
  -u store-id:secret-key \
  -G
```

**Node**

```javascript
const { swell } = require('swell-node');
swell.init('store-id', 'secret-key');

await swell.get('/coupons:codes', {
});
```

**PHP**

```php
<?php $swell = new \Swell\Client('store-id', 'secret-key');

$swell->get('/coupons:codes', [
]);
```

### Example response

```json
{
  "count": 30,
  "results": [
    {
      "code": "FREESKOOMA",
      "parent_id": "62cc86897ad4f100132c953d",
      "use_count": 0,
      "date_created": "2022-07-11T20:22:33.332Z",
      "use_total": 0,
      "date_updated": "2022-07-18T20:42:32.978Z",
      "id": "62cc86897ad4f100132c953e"
    },
    {
      "code": "ARROW2THEKNEE",
      "parent_id": "62a0e2f0473e110013059e9b",
      "use_count": 0,
      "date_created": "2022-06-08T17:57:04.786Z",
      "use_total": 0,
      "id": "62a0e2f0473e110013059e9c"
    },
    {
      "code": "DAGOTHWAVE",
      "parent_id": "62857220dad145001973af16",
      "use_count": 0,
      "date_created": "2022-05-18T22:24:32.130Z",
      "use_total": 0,
      "id": "62857220dad145001973af17"
    },
    {
      "code": "SHIVERINGISLES",
      "parent_id": "628557a32e8aca00180b80d3",
      "use_count": 0,
      "date_created": "2022-05-18T20:31:31.818Z",
      "use_total": 0,
      "date_updated": "2022-05-18T22:23:55.655Z",
      "id": "628557a32e8aca00180b80d4"
    },
    {
      "code": "STOPRIGHTTHERE",
      "parent_id": "6164a07f1f39735b268ebc92",
      "use_count": 5,
      "date_created": "2021-10-11T20:37:19.360Z",
      "use_total": 5,
      "date_updated": "2022-02-07T16:16:08.484Z",
      "id": "6164a07f1f39735b268ebc9b"
    }
  ],
  "page": 1,
  "page_count": 2,
  "limit": 15,
  "pages": {
    "1": {
      "start": 1,
      "end": 15
    },
    "2": {
      "start": 16,
      "end": 30
    }
  }
}
```


## Delete a coupon code

Delete a coupon code permanently.

### Arguments

- `id` (objectId, required): ID of the coupon code.

### Example request

`DELETE /coupons:codes/:id`

**cURL**

```sh
$ curl https://api.swell.store/coupons:codes/62cc86897ad4f100132c953e \
  -u store-id:secret-key \
  -X DELETE
```

**Node**

```javascript
const { swell } = require('swell-node');
swell.init('store-id', 'secret-key');

await swell.delete('/coupons:codes/{id}', {
  id: '62cc86897ad4f100132c953e'
});
```

**PHP**

```php
<?php $swell = new \Swell\Client('store-id', 'secret-key');

$swell->delete('/coupons:codes/{id}', [
  'id' => '62cc86897ad4f100132c953e'
]);
```

### Example response

```json
{
 "gen_id": "62b5cdffd7ad010018c66249",
 "parent_id": "62b5cdff2e40ab001ac27240",
 "use_count": 0,
 "date_created": "2022-06-24T14:45:20.941Z",
 "use_total": 0,
 "code": "PNB83100017",
 "id": "62b5ce0079c5d80012fe5f65"
}
```

