# Coupon generations

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

Coupon generations track the instances for which coupon codes are generated for a particular coupon. Each batch of generations is tied to an entry and is stored on the coupon model.

## The coupon generation model

### Fields

- `id` (objectId): Unique identifier for the coupon codes generated.
- `codes` (Coupon code): An array of coupon codes to be generated.
- `count` (int, required): Specifies the number of codes to be generated and limit for generations is 10,000. For example, `count=100` would generate 100 coupon codes. Default: `0`.
- `date_created` (date, auto): Date the coupon code is generated.
- `date_updated` (date, auto): Date the coupon code was last updated.
- `parent` (Coupon): Expandable link to the parent coupon.
- `parent_id` (objectId, required): Unique identifier of the parent coupon.
- `pattern` (string): Specifies the code pattern for generated coupons.
- `pattern_type` (enum): Determines the pattern type for the codes generated, which can be either `default` or `custom`. Specify the `pattern` when using `custom`. Possible values: `default`, `custom`. Default: `"default"`.

### Example response

```json
{
  "complete": true,
  "count": 1000,
  "date_created": "2015-09-02T20:57:03.441Z",
  "date_updated": "2015-09-02T20:57:20.033Z",
  "parent_id": "559c5799bcb1de1d1c4821da",
  "pattern": "TABATHA-{0000000}",
  "pattern_type": "custom",
  "id": "55e7629f5c0dfa1e3c2dd4c1"
},
```


## Create a coupon generation

Create a new coupon generation.

### Arguments

- `id` (objectId): ID of the coupon generation.
- `count` (int, required): The number of coupon codes generated in the generation. Minimum of 0 and maximum of 1000. Default: `0`.
- `parent_id` (objectId, required): ID of the parent coupon.
- `codes` (Coupon code): Expandable link to the coupon code.
- `complete` (boolean): Indicates that the coupon code generation is complete. Default: `false`.
- `date_created` (date, auto): Date and time the coupon generation was created.
- `date_updated` (date, auto): Date and time the coupon generation was last updated.
- `error` (string): Indicates if there was an error with the coupon code generation.
- `parent` (Coupon): Expandable link to the parent coupon.
- `pattern` (string): Specifies the code pattern for generated coupons.
- `pattern_type` (string): Determines the pattern type for the codes generated, which can be either `default` or `custom`. Specify the pattern when using `custom`.

### Example request

`POST /coupons:generations`

**cURL**

```sh
$ curl https://api.swell.store/coupons:generations \
  -u store-id:secret-key \
	-d pattern_type:"default" \
	-d count:10 \
```

**Node**

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

await swell.post('/coupons:generations', {
pattern_type: default
count: 10
});
```

**PHP**

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

$swell->post('/coupons:generations', [
'pattern_type' => 'default'
'count' => 10
]);
```

### Example response

```json
{
  "complete": true,
  "count": 1000,
  "date_created": "2015-09-02T20:57:03.441Z",
  "date_updated": "2015-09-02T20:57:20.033Z",
  "parent_id": "559c5799bcb1de1d1c4821da",
  "pattern": "TABATHA-{0000000}",
  "pattern_type": "custom",
  "id": "55e7629f5c0dfa1e3c2dd4c1"
},
```


## Retrieve a coupon generation

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

### Arguments

- `id` (objectId, required): The ID of the coupon generation to retrieve.

### Example request

`GET /coupons:generations/:id`

**cURL**

```sh
$ curl https://api.swell.store/coupons:generations/611e890866ceea71e6704d0e \
  -u store-id:secret-key
```

**Node**

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

await swell.get('/coupons:generations/{id}', {
  id: '611e890866ceea71e6704d0e'
});
```

**PHP**

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

$swell->get('/coupons:generations/{id}', [
  'id' => '611e890866ceea71e6704d0e'
]);
```

### Example response

```json
{
  "complete": true,
  "count": 1000,
  "date_created": "2015-09-02T20:57:03.441Z",
  "date_updated": "2015-09-02T20:57:20.033Z",
  "parent_id": "559c5799bcb1de1d1c4821da",
  "pattern": "TABATHA-{0000000}",
  "pattern_type": "custom",
  "id": "55e7629f5c0dfa1e3c2dd4c1"
},
```


## Update a coupon generation

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

### Arguments

- `id` (objectId, required): Unique identifier for the coupon generation.
- `count` (int): Default: `0`.

### Example request

`PUT /coupons:generations/:id`

**cURL**

```sh
$ curl https://api.swell.store/coupons:generations/611e890866ceea71e6704d0e \
  -u store-id:secret-key \
  -d count=10 \
  -X PUT
```

**Node**

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

await swell.put('/coupons:generations/{id}', {
  id: '611e890866ceea71e6704d0e',
  count: 10
});
```

**PHP**

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

$swell->put('/coupons:generations/{id}', [
  'id' => '611e890866ceea71e6704d0e',
  'count' => 10
]);
```

### Example response

```json
{
  "complete": true,
  "count": 1000,
  "date_created": "2015-09-02T20:57:03.441Z",
  "date_updated": "2015-09-02T20:57:20.033Z",
  "parent_id": "559c5799bcb1de1d1c4821da",
  "pattern": "TABATHA-{0000000}",
  "pattern_type": "custom",
  "id": "55e7629f5c0dfa1e3c2dd4c1"
},
```


## List all coupon generations

Return a list of coupon generations.

### 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:generations`

**cURL**

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

**Node**

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

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

**PHP**

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

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

### Example response

```json
{
  "count": 5,
  "results": [
    {
      "parent_id": "611e890827d69001eb2a5c9a",
      "count": 5,
      "date_created": "2021-08-19T16:38:32.602Z",
      "pattern_type": "default",
      "complete": true,
      "date_updated": "2021-08-19T16:38:33.940Z",
      "id": "611e890866ceea71e6704d0e"
    },
    {
      "complete": true,
      "count": 1000,
      "date_created": "2015-09-02T20:57:03.441Z",
      "date_updated": "2015-09-02T20:57:20.033Z",
      "parent_id": "559c5799bcb1de1d1c4821da",
      "pattern": "TABATHA-{0000000}",
      "pattern_type": "custom",
      "id": "55e7629f5c0dfa1e3c2dd4c1"
    },
    {
      "complete": true,
      "count": 1000,
      "date_created": "2015-08-18T22:18:19.028Z",
      "date_updated": "2015-08-18T22:18:33.920Z",
      "parent_id": "559c5799bcb1de1d1c4821da",
      "pattern": "OMG{1234}",
      "pattern_type": "custom",
      "id": "55d3af2b4a399f7341dfcb3d"
    },
    {
      "complete": true,
      "count": 10,
      "date_created": "2015-07-08T14:35:36.468Z",
      "date_updated": "2015-07-08T14:35:36.651Z",
      "parent_id": "559c5799bcb1de1d1c4821da",
      "pattern": "OMG{000}",
      "pattern_type": "custom",
      "id": "559d3538b4ca1ef662dbde39"
    },
    {
      "complete": true,
      "count": 1000,
      "date_created": "2015-07-07T22:50:11.932Z",
      "date_updated": "2015-07-07T22:50:25.781Z",
      "parent_id": "559c5799bcb1de1d1c4821da",
      "pattern_type": "default",
      "id": "559c57a3bcb1de1d1c4821db"
    }
  ],
  "page": 1,
  "page_count": 1,
  "limit": 15
}
```


## Delete a coupon generation

Delete a coupon generation permanently.

### Arguments

- `id` (objectId, required): The ID of the coupon generation to delete.

### Example request

`DELETE /coupons:generations/:id`

**cURL**

```sh
$ curl https://api.swell.store/coupons:generations/611e890866ceea71e6704d0e \
  -u store-id:secret-key \
  -X DELETE
```

**Node**

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

await swell.delete('/coupons:generations/{id}', {
  id: '611e890866ceea71e6704d0e'
});
```

**PHP**

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

$swell->delete('/coupons:generations/{id}', [
  'id' => '611e890866ceea71e6704d0e'
]);
```

### Example response

```json
{
  "complete": true,
  "count": 1000,
  "date_created": "2015-09-02T20:57:03.441Z",
  "date_updated": "2015-09-02T20:57:20.033Z",
  "parent_id": "559c5799bcb1de1d1c4821da",
  "pattern": "TABATHA-{0000000}",
  "pattern_type": "custom",
  "id": "55e7629f5c0dfa1e3c2dd4c1"
},
```

