Coupons

View as Markdown

A coupon is a code a buyer types to pay less: a percent or a fixed amount off, with the limits you set (how many times, until when, from what order size, for which products or customers). Create and manage coupons from your server; buyers redeem them wherever they can enter a code with us.

POST   https://api.worthypayments.com/v1/coupons
GET    https://api.worthypayments.com/v1/coupons
GET    https://api.worthypayments.com/v1/coupons/{id}
POST   https://api.worthypayments.com/v1/coupons/{id}
DELETE https://api.worthypayments.com/v1/coupons/{id}
ScopeGrants
coupons:readList and fetch coupons, look a code up
coupons:writeCreate, update and delete coupons

Both scopes are server-only: a publishable key is refused, so a coupon cannot be minted, widened or listed from a browser. The coupons you create here are the same coupons as the dashboard's Products → Coupons page, in the key's mode (a test key sees test coupons).

Where a buyer redeems a coupon

SurfaceHow the coupon gets there
Hosted checkout, payment links, store cartsThe buyer types the code.
SubscriptionsYour server passes the coupon id as coupon on POST /v1/subscriptions. See Subscriptions.
Your own checkout on the Payment ElementYour server applies it to the payment intent (discounts), or lets the buyer enter a code in the Element (allow_promotion_codes). See On a payment intent.

Related: Products, Idempotency, Pagination.


The coupon object#

json
{
  "id": "3b9d6c1e-7a52-4f0e-8c3d-1e2f4a5b6c7d",
  "object": "coupon",
  "code": "PARTNER-ANA",
  "name": "Partner Ana",
  "description": null,
  "percent_off": 15,
  "amount_off": null,
  "currency": "usd",
  "duration": "once",
  "duration_in_months": null,
  "max_redemptions": 500,
  "max_redemptions_per_customer": 1,
  "times_redeemed": 42,
  "minimum_amount": null,
  "maximum_discount_amount": null,
  "applies_to": [],
  "customers": [],
  "first_time_order_only": false,
  "starts_at": null,
  "expires_at": 1798761599,
  "active": true,
  "valid": true,
  "livemode": false,
  "created": 1796385600,
  "updated": 1796385600
}
FieldNotes
idThe coupon id. Pass it as coupon on POST /v1/subscriptions, or in discounts on a payment intent.
codeWhat the buyer types. Stored upper-cased and matched case-insensitively: partner-ana and PARTNER-ANA are one code. Unique among your live coupons in this mode.
percent_off / amount_offOne of the two is set; it is the coupon's kind. percent_off is a percent (15 takes 15 % off). amount_off is integer minor units of currency, and never takes off more than the order.
currencyThe currency of every amount on the coupon, lowercase. Fixed at create.
duration, duration_in_monthsFor subscriptions: once discounts the first invoice, repeating the first duration_in_months months, forever every invoice. A one-off payment uses the coupon once whatever this says.
max_redemptionsTotal redemptions allowed. null: no limit.
max_redemptions_per_customerRedemptions allowed per customer. Counted only where the buyer is a known customer; a guest checkout is not counted against it. null: no limit.
times_redeemedRedemptions so far.
minimum_amountThe smallest order the coupon applies to, in minor units. null: no minimum.
maximum_discount_amountA cap on what a percent_off coupon takes off. Always null on an amount_off coupon.
applies_toProduct or product category ids. When set, only those lines are discounted and an order with none of them is refused. []: the whole order.
customersCustomer ids (cus_...). When set, only these customers may redeem it.
first_time_order_onlyOnly for a customer with no earlier successful order. A buyer we cannot identify as a customer is refused.
starts_at, expires_atUnix seconds, or null. Not redeemable before starts_at or after expires_at.
activeThe on/off switch you control.
validWhether a buyer could redeem it right now, cart aside: active, inside its window, and under max_redemptions. The cart's own rules (minimum, products, customers, first order, per-customer limit) are checked when the code is applied.

Create#

POST /v1/coupons. Idempotency-Key is required: a retried create would otherwise try to make a second coupon.

bash
curl https://api.worthypayments.com/v1/coupons \
  -H "x-api-key: sk_test_xxx" \
  -H "Idempotency-Key: affiliate-8812-coupon" \
  -H "content-type: application/json" \
  -d '{
    "code": "PARTNER-ANA",
    "name": "Partner Ana",
    "percent_off": 15,
    "max_redemptions": 500,
    "max_redemptions_per_customer": 1,
    "expires_at": 1798761599
  }'
FieldRequiredNotes
codeYes2 to 64 letters, digits, dashes or underscores.
percent_offOne of the twoAbove 0 and at most 100, at most two decimals.
amount_offOne of the twoInteger minor units, at least 1.
currencyNoThree-letter ISO code. Defaults to usd.
nameNoDefaults to the code. At most 200 characters.
descriptionNoAt most 500 characters.
durationNoonce (default), repeating or forever.
duration_in_monthsWith repeatingRefused with any other duration.
max_redemptions, max_redemptions_per_customerNoIntegers, at least 1.
minimum_amountNoInteger minor units. 0 is no minimum.
maximum_discount_amountNoOnly on a percent_off coupon.
applies_to, customersNoUp to 100 ids each.
first_time_order_onlyNoBoolean.
starts_at, expires_atNoUnix seconds. expires_at must be in the future and after starts_at.

A new coupon is active. A field the route does not know (metadata, active on create) is refused with parameter_unknown, never ignored.

One code per affiliate#

When a partner signs up in your affiliate program, create their code from your backend and keep the returned id beside the partner:

  1. Pick the code (for example from the partner's handle) and check it is free: GET /v1/coupons?code=PARTNER-ANA returns an empty data list when no live coupon has it.
  2. POST /v1/coupons with an Idempotency-Key tied to the partner (such as affiliate-<partner id>-coupon), so a retry after a timeout returns the same coupon instead of a 409.
  3. Store the coupon id. A 409 already_exists means another coupon took the code first: pick another code.

When the partnership ends, set active: false (the code stays reserved and the history stays readable) or delete the coupon (the code becomes free for someone else).


List and look up#

GET /v1/coupons, newest first. Deleted coupons are never listed.

QueryNotes
limitPage size. Defaults to 10, capped at 100.
starting_afterThe next_cursor of the previous page.
activetrue or false. Omit it to list both. This is the switch, not valid: an active coupon may still be expired or used up.
codeAn exact code, case-insensitive. Returns that coupon alone, or an empty list.
bash
curl "https://api.worthypayments.com/v1/coupons?code=partner-ana" \
  -H "x-api-key: sk_test_xxx"

GET /v1/coupons/{id} returns one coupon. Another account's coupon, the other mode's, or a deleted one is 404 resource_missing.


Update#

POST /v1/coupons/{id} changes only the fields you send. It takes the create fields except code and currency (both fixed: sending either is refused), plus active. Idempotency-Key is optional.

bash
curl https://api.worthypayments.com/v1/coupons/3b9d6c1e-7a52-4f0e-8c3d-1e2f4a5b6c7d \
  -H "x-api-key: sk_test_xxx" \
  -H "content-type: application/json" \
  -d '{ "max_redemptions": 1000, "expires_at": 1801439999 }'

The body is checked against the stored coupon: duration: repeating needs duration_in_months unless the coupon already has it, maximum_discount_amount is refused on an amount_off coupon, and expires_at must stay after starts_at. Sending percent_off to an amount_off coupon (or the reverse) changes its kind.

Removing a limit

To removeSend
max_redemptions, max_redemptions_per_customer, minimum_amountnull
maximum_discount_amountnull
starts_at, expires_atnull
applies_to, customers[]
descriptionnull or ""

A removed starts_at no longer holds expires_at back, so { "starts_at": null, "expires_at": ... } may set an end before the old start. To stop a coupon now, send active: false.

A change applies to every redemption from now on. Payments already completed keep the discount they were given.


Pause or delete#

active: falseDELETE /v1/coupons/{id}
Buyers can redeem itNoNo
Listed and retrievableYesNo (404)
The codeStays reservedFree for a new coupon
Undoactive: trueNo

Delete keeps the record behind the scenes (payments that used the coupon stay explained); it answers { "id": "...", "object": "coupon", "deleted": true }.


On a payment intent#

A payment intent takes a coupon in one of two ways. Pick one per intent; sending both is refused.

Your server applies it. Name the coupon by id or by code when you create the intent, or later with PATCH:

bash
curl https://api.worthypayments.com/v1/payment_intents \
  -H "x-api-key: sk_test_xxx" \
  -H "Idempotency-Key: order-1234" \
  -H "content-type: application/json" \
  -d '{ "amount": 10000, "currency": "usd", "customer": "cus_...",
        "discounts": [{ "promotion_code": "PARTNER-ANA" }] }'
json
{
  "id": "pi_...",
  "amount": 8500,
  "amount_subtotal": 10000,
  "amount_details": { "discount_amount": 1500 },
  "allow_promotion_codes": false,
  "discounts": [{ "coupon": "3b9d6c1e-7a52-4f0e-8c3d-1e2f4a5b6c7d", "applied_by": "merchant" }],
  "client_secret": "pi_..._secret_..."
}

amount is what the buyer is charged. The amount you send is the subtotal the coupon comes off. A PATCH with a new amount keeps the coupon and takes it off the new subtotal; a PATCH with discounts: [] removes it.

The buyer enters a code. Create the intent with allow_promotion_codes: true. The Payment Element then draws an "Add promotion code" field and shows the discount once a code applies; an intent without it draws no field. Or keep your own input, mount with fields: { promotionCode: 'never' }, and call element.applyPromotionCode(code) / element.removePromotionCode(). The Element reads the new total from the intent; your page never sends an amount. Without allow_promotion_codes: true on the intent nothing a buyer's page sends can add a code to it or remove the one your server applied.

What is checked. The coupon must be yours, in the key's mode, and redeemable now (active, inside its window, under max_redemptions). Then, against the payment:

RuleWhy
An amount_off coupon, or one with minimum_amount / maximum_discount_amount, must be in the intent's currency. A plain percent_off works in any currency.Its amounts mean nothing in another currency.
A coupon with applies_to set is refused.An intent has one amount, no products to match.
minimum_amount, customers, first_time_order_only and max_redemptions_per_customer are checked against the intent's customer.Give the intent a customer to use a coupon limited to customers.
The discounted amount must stay at least 50 minor units.The same minimum as any charge.

Your server's refusal says why (coupon_invalid with the reason, or resource_missing). A buyer's code that does not apply always reads promotion_code_invalid, whatever the reason, and the Element allows 10 tries per intent every 10 minutes: the answer never tells a guesser which codes exist.

When it counts. The coupon is redeemed once, when the payment succeeds (times_redeemed goes up then, not when the code is applied). The charge's webhooks carry it as detail.discount (Webhooks); the charge's amount is already the discounted total.


Errors#

HTTPcodeWhen
400idempotency_key_requiredCreate without an Idempotency-Key
400parameter_invalidA field fails the rules above. param names it
400parameter_unknownA field this route does not take
403permission_deniedThe key lacks coupons:read / coupons:write, or is publishable
404resource_missingNo such coupon for this key (param: id)
409already_existsAnother live coupon of yours in this mode has the code (param: code)
409idempotency_in_progressThe same Idempotency-Key is still running

See Errors for the envelope.