Hanzo
CommerceAPI Reference

Coupons & Promotions API

Manage promotions, discount rules, and campaign budgets

The Coupons and Promotions API handles discount logic including promotion rules, application methods, coupon codes, and campaign budget tracking.

Promotion Structure

{
  "id": "promo_abc123",
  "code": "SUMMER25",
  "type": "standard",
  "status": "active",
  "applicationMethod": {
    "type": "percentage",
    "value": 25,
    "allocation": "across",
    "maxQuantity": null,
    "buyRulesMinQuantity": null
  },
  "rules": [
    {
      "attribute": "subtotal",
      "operator": "gte",
      "values": ["5000"]
    }
  ],
  "campaign": {
    "id": "camp_summer",
    "name": "Summer Sale 2024",
    "budgetLimit": 100000,
    "budgetUsed": 34500
  },
  "startsAt": "2024-06-01T00:00:00Z",
  "endsAt": "2024-08-31T23:59:59Z",
  "usageLimit": 1000,
  "usageCount": 342,
  "createdAt": "2024-05-15T10:00:00Z"
}

Endpoints

List Promotions

GET /promotion

Query Parameters:

ParameterTypeDescription
pagenumberPage number
displaynumberItems per page
sortstringSort field
statusstringactive, draft, expired, disabled
typestringstandard, buyget, freeShipping
campaignIdstringFilter by campaign
curl "https://api.hanzo.ai/promotion?status=active&display=20" \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN"

Response:

{
  "count": 8,
  "models": [
    {
      "id": "promo_abc123",
      "code": "SUMMER25",
      "type": "standard",
      "status": "active",
      "usageCount": 342,
      "usageLimit": 1000
    }
  ]
}

Get Promotion

GET /promotion/:id

curl https://api.hanzo.ai/promotion/promo_abc123 \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN"

Create Promotion

POST /promotion

curl -X POST https://api.hanzo.ai/promotion \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "code": "SUMMER25",
    "type": "standard",
    "status": "active",
    "applicationMethod": {
      "type": "percentage",
      "value": 25,
      "allocation": "across"
    },
    "rules": [
      {
        "attribute": "subtotal",
        "operator": "gte",
        "values": ["5000"]
      }
    ],
    "startsAt": "2024-06-01T00:00:00Z",
    "endsAt": "2024-08-31T23:59:59Z",
    "usageLimit": 1000
  }'

Update Promotion

PUT /promotion/:id

curl -X PUT https://api.hanzo.ai/promotion/promo_abc123 \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "status": "disabled"
  }'

Delete Promotion

DELETE /promotion/:id

curl -X DELETE https://api.hanzo.ai/promotion/promo_abc123 \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN"

Application Methods

Application methods define how a discount is calculated.

Create Application Method

POST /applicationmethod

curl -X POST https://api.hanzo.ai/applicationmethod \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "promotionId": "promo_abc123",
    "type": "fixed",
    "value": 1000,
    "allocation": "each",
    "maxQuantity": 3
  }'

Application Method Types:

TypeDescriptionExample
percentagePercentage off25% off
fixedFixed amount off$10 off each item
freeShippingWaives shipping costFree shipping

Allocation:

ValueDescription
acrossSpread across all eligible items
eachApply to each eligible item individually

Promotion Rules

Rules determine when a promotion can be applied.

Create Promotion Rule

POST /promotionrule

curl -X POST https://api.hanzo.ai/promotionrule \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "promotionId": "promo_abc123",
    "attribute": "product.collection",
    "operator": "in",
    "values": ["summer-collection", "clearance"]
  }'

Rule Attributes:

AttributeDescription
subtotalCart subtotal in cents
itemCountNumber of items in cart
product.collectionProduct collection slug
product.tagProduct tags
customer.tagCustomer segment tags
currencyCart currency code

Rule Operators:

OperatorDescription
eqEquals
gteGreater than or equal
lteLess than or equal
inValue in list
ninValue not in list

Evaluate Promotion

POST /promotion/evaluate

Test which promotions apply to a given cart without modifying it:

curl -X POST https://api.hanzo.ai/promotion/evaluate \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "cartId": "cart_abc123"
  }'

Response:

{
  "applicable": [
    {
      "id": "promo_abc123",
      "code": "SUMMER25",
      "discount": 1499,
      "description": "25% off orders over $50"
    }
  ],
  "automatic": [
    {
      "id": "promo_auto01",
      "discount": 500,
      "description": "Free shipping on orders over $75"
    }
  ]
}

Campaign Budgets

Campaigns group promotions and enforce a shared budget.

curl -X POST https://api.hanzo.ai/campaign \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Summer Sale 2024",
    "budgetLimit": 100000,
    "startsAt": "2024-06-01T00:00:00Z",
    "endsAt": "2024-08-31T23:59:59Z"
  }'

When a campaign budget is exhausted, all promotions in that campaign are automatically paused.

SDK Examples

JavaScript

import { Commerce } from '@hanzo/commerce'

const commerce = new Commerce({ apiKey: 'your_key' })

// Create promotion
const promo = await commerce.promotions.create({
  code: 'SUMMER25',
  type: 'standard',
  applicationMethod: { type: 'percentage', value: 25 },
  rules: [{ attribute: 'subtotal', operator: 'gte', values: ['5000'] }],
  startsAt: '2024-06-01T00:00:00Z',
  endsAt: '2024-08-31T23:59:59Z'
})

// Evaluate cart promotions
const result = await commerce.promotions.evaluate({ cartId: 'cart_abc123' })

Go

promo, err := client.Promotions.Create(ctx, &sdk.PromotionInput{
    Code:   "SUMMER25",
    Type:   "standard",
    Status: "active",
    ApplicationMethod: &sdk.ApplicationMethod{
        Type:  "percentage",
        Value: 25,
    },
    StartsAt: "2024-06-01T00:00:00Z",
    EndsAt:   "2024-08-31T23:59:59Z",
})

How is this guide?

Last updated on

On this page