Backend API

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 instance and are also tracked with 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
Fields
idobjectId

Unique identifier for the coupon code.

codestringauto

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_createddateauto

Date and time the coupon code was created.

date_updateddateauto

Date and time the coupon code was last updated.

gen_idobjectId

Unique ID corresponding to a coupon code generation.

ordersOrder

Expandable list of orders that have applied the coupon code.

parent_idobjectIdrequired

ID of the parent coupon, if applicable.

parentCoupon

Expandable link to the parent coupon.

subscriptionsSubscription

Expandable list of subscriptions that have applied the coupon code.

use_countint

Number of times the coupon code has been used.

The coupon code model
{
 "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 new coupon code.

Arguments
parent_idobjectIdrequired

ID of the parent coupon.

idobjectId

ID of the coupon code.

codestring

Unique code to identify the coupon.

date_createddateauto

Date and time the coupon code was created.

date_updateddateauto

Date and time the coupon code was last updated.

gen_idobjectId

The ID of the generation instance for a particular coupon code.

ordersOrder
parentCoupon

Expandable link to the parent coupon.

subscriptionsSubscription
use_countint
use_totalint
POST/coupons:codes
const { swell } = require('swell-node');
swell.init('store-id', 'secret-key');

await swell.post('/coupons:codes', {
code: 'ALCHEMY25',
parent_id: '62cc86897ad4f100132c953d'
});
The coupon code model
{
 "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 an existing coupon code using the ID that was returned when created.

Arguments
idobjectIdrequired

ID of the coupon code.

GET/coupons:codes/:id
const { swell } = require('swell-node');
swell.init('store-id', 'secret-key');

await swell.get('/coupons:codes/{id}', {
  id: '62cc86897ad4f100132c953e'
});
The coupon code model
{
 "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 an existing coupon code using the ID that was returned when created.

Arguments
idobjectIdrequired

Unique identifier for the coupon code.

PUT/coupons:codes/:id
const { swell } = require('swell-node');
swell.init('store-id', 'secret-key');

await swell.put('/coupons:codes/{id}', {
  id: '62cc86897ad4f100132c953e',
  code: 'SKOOMA15'
});
The coupon code model
{
 "gen_id": "62b5cdffd7ad010018c66249",
 "parent_id": "62b5cdff2e40ab001ac27240",
 "use_count": 0,
 "date_created": "2022-06-24T14:45:20.941Z",
 "use_total": 0,
 "code": "PNB83100017",
 "id": "62b5ce0079c5d80012fe5f65"
}

Return a list of coupon codes.

Arguments
expandstring

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 for more details.

fieldsstring

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.
includeobject

Include one or more arbitrary queries in the response which are potentially related to the main query.

See including for more details.

limitint

Limit the number of records returned, ranging between 1 and 1000. Defaults to 15.

pageint

The page number of results to return given the specified or default limit.

searchstring

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 for more details.

sortstring

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 for more details.

whereobject

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 for more details.

GET/coupons:codes
const { swell } = require('swell-node');
swell.init('store-id', 'secret-key');

await swell.get('/coupons:codes', {
});
The coupon code model
{
 "gen_id": "62b5cdffd7ad010018c66249",
 "parent_id": "62b5cdff2e40ab001ac27240",
 "use_count": 0,
 "date_created": "2022-06-24T14:45:20.941Z",
 "use_total": 0,
 "code": "PNB83100017",
 "id": "62b5ce0079c5d80012fe5f65"
}

Delete a coupon code permanently.

Arguments
idobjectIdrequired

ID of the coupon code.

DELETE/coupons:codes/:id
const { swell } = require('swell-node');
swell.init('store-id', 'secret-key');

await swell.delete('/coupons:codes/{id}', {
  id: '62cc86897ad4f100132c953e'
});
The coupon code model
{
 "gen_id": "62b5cdffd7ad010018c66249",
 "parent_id": "62b5cdff2e40ab001ac27240",
 "use_count": 0,
 "date_created": "2022-06-24T14:45:20.941Z",
 "use_total": 0,
 "code": "PNB83100017",
 "id": "62b5ce0079c5d80012fe5f65"
}