# Create Coupon - Add Discount Codes to Your Subscription Bot

> Create a discount coupon with custom percentage or fixed amount. Set usage limits and expiration.

Source: https://docs.botsubscription.com/api/coupons/create/

Generate a new discount code for your subscription bot. Configure percentage or fixed-amount discounts, set auto-apply rules, restrict usage to specific plans or user types, and control whether the coupon applies to first payments only or renewals too.

<ApiEndpoint method="POST" endpoint="/v2/projects/{project_id}/coupons" />

<ApiAuthorization type="Bearer API Key" />

**API key access:** Unrestricted project API key required. Restricted keys cannot call this endpoint.

<ApiSection title="Path Parameters">
  <ApiParameter name="project_id" type="string" required description="The unique identifier of the project." />
</ApiSection>

<ApiSection title="Request Body">
  <ApiParameter name="code" type="string" required description="The coupon code users will enter." />
  <ApiParameter name="display_name" type="string" required description="Display name for the coupon." />
  <ApiParameter name="description" type="string" description="A description of the coupon." />
  <ApiParameter name="coupon_type" type="string" required description="Type of discount: percentage or fixed." />
  <ApiParameter name="percentage" type="string|number|null" description="Discount percentage as a decimal string, greater than 0 and at most 100 (required for percentage type)." />
  <ApiParameter name="amount" type="string|number|null" description="Fixed discount amount as a decimal string (required for fixed type)." />
  <ApiParameter name="currency" type="string" description="Currency for fixed discounts (e.g., USD). Required for fixed type." />
  <ApiParameter name="min_spend_amount" type="string | number | null" description="Optional inclusive minimum checkout amount. Prefer an unsigned decimal string; nonnegative JSON numbers are also accepted. Requires min_spend_currency. Null means no minimum." />
  <ApiParameter name="min_spend_currency" type="string | null" description="Currency for the minimum checkout amount: 3 to 9 uppercase ASCII letters or digits." />
  <ApiParameter name="max_spend_amount" type="string | number | null" description="Optional inclusive maximum checkout amount. Prefer an unsigned decimal string; nonnegative JSON numbers are also accepted. Requires max_spend_currency. Null means no maximum." />
  <ApiParameter name="max_spend_currency" type="string | null" description="Currency for the maximum checkout amount: 3 to 9 uppercase ASCII letters or digits." />
  <ApiParameter name="auto_apply" type="boolean" description="Whether to auto-apply this coupon. Default: false" />
  <ApiParameter name="usage_limit" type="integer" description="Total redemptions allowed across all users." />
  <ApiParameter name="per_user_limit" type="integer" description="Redemptions allowed per user." />
  <ApiParameter name="invitee_mode" type="string" description="Who can use: all, inviteesOnly, nonInvitees. Default: all" />
  <ApiParameter name="renewal_constraint" type="string" description="When applicable: any, renewalOnly, churnedOnly. Default: any" />
  <ApiParameter name="plan_scope" type="string" description="Which plans: all, include, exclude. Default: all (plan_ids required when include or exclude)." />
  <ApiParameter name="plan_ids" type="array" description="Plan UUIDs to include or exclude, per plan_scope." />
  <ApiParameter name="starts_at" type="number | string | null" description="Validity start: 13-digit Unix milliseconds as a number or digit string, or null for no bound. ISO dates and seconds are not accepted." />
  <ApiParameter name="ends_at" type="number | string | null" description="Validity end: 13-digit Unix milliseconds as a number or digit string, or null for no bound. Must be after starts_at. ISO dates and seconds are not accepted." />
  <ApiParameter name="metadata" type="object" description="Custom metadata for the coupon." />
</ApiSection>

Numeric coupon values follow the [decimal input contract](https://docs.botsubscription.com/api/overview/#decimal-amounts). A nullable value may be cleared only when the resulting coupon still satisfies its type requirements.

Spend bounds apply to this checkout's amount. Both endpoints are inclusive. Each amount needs its currency, and when both bounds are set their currencies must match and the minimum must be no greater than the maximum. Equal bounds are valid; invalid combinations return `422 VALIDATION_ERROR`. The checkout currency must match the configured bound; no currency conversion is performed.

All monetary fields and `percentage` follow the [decimal input rules](https://docs.botsubscription.com/api/overview/#decimal-amounts). Percentage bounds are checked exactly: `"100"` is valid, but `"100.000000000000000001"` returns `422 VALIDATION_ERROR`. Spend bounds are compared as exact decimals rather than rounded floating-point values.

<Note>
Response examples use the [UTC ISO date format](https://docs.botsubscription.com/api/overview/#utc-iso-response-dates).
</Note>

## Recover a saved creation

If the response fails, check `Resource-Created` and `Response-Validation-Failed` before repeating the operation. Preserve the original request and retry key. A nonreplayable result can identify the saved resource in `error.details`; retrieve it before considering another create. See [creation outcome recovery](https://docs.botsubscription.com/api/overview/#creation-outcomes-after-a-failed-response).

<ApiSamplePane>

<ApiRequestSample title="Request">

```bash
curl -X POST "https://api.botsubscription.com/v2/projects/f47ac10b-58cc-4372-a567-0e02b2c3d479/coupons" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "code": "SUMMER2024",
    "display_name": "Summer Sale",
    "description": "Summer promotion discount",
    "coupon_type": "percentage",
    "percentage": "20",
    "min_spend_amount": "1",
    "min_spend_currency": "USD",
    "max_spend_amount": "100",
    "max_spend_currency": "USD",
    "usage_limit": 100,
    "plan_scope": "include",
    "plan_ids": ["b2c9f6f0-7a47-4c2e-9a0e-4a4fa1d8c1c4"],
    "starts_at": 1717200000000,
    "ends_at": 1725148799000
  }'
```

```javascript
const project_id = 'f47ac10b-58cc-4372-a567-0e02b2c3d479';
const response = await fetch(`https://api.botsubscription.com/v2/projects/${project_id}/coupons`, {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer YOUR_API_KEY',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    code: 'SUMMER2024',
    display_name: 'Summer Sale',
    description: 'Summer promotion discount',
    coupon_type: 'percentage',
    percentage: '20',
    min_spend_amount: '1',
    min_spend_currency: 'USD',
    max_spend_amount: '100',
    max_spend_currency: 'USD',
    usage_limit: 100,
    plan_scope: 'include',
    plan_ids: ['b2c9f6f0-7a47-4c2e-9a0e-4a4fa1d8c1c4'],
    starts_at: 1717200000000,
    ends_at: 1725148799000
  })
});
const data = await response.json();
```

```python
import requests

project_id = 'f47ac10b-58cc-4372-a567-0e02b2c3d479'
response = requests.post(
    f'https://api.botsubscription.com/v2/projects/{project_id}/coupons',
    headers={
        'Authorization': 'Bearer YOUR_API_KEY',
        'Content-Type': 'application/json'
    },
    json={
        'code': 'SUMMER2024',
        'display_name': 'Summer Sale',
        'description': 'Summer promotion discount',
        'coupon_type': 'percentage',
        'percentage': '20',
        'min_spend_amount': '1',
        'min_spend_currency': 'USD',
        'max_spend_amount': '100',
        'max_spend_currency': 'USD',
        'usage_limit': 100,
        'plan_scope': 'include',
        'plan_ids': ['b2c9f6f0-7a47-4c2e-9a0e-4a4fa1d8c1c4'],
        'starts_at': 1717200000000,
        'ends_at': 1725148799000
    }
)
data = response.json()
```

```php
$project_id = 'f47ac10b-58cc-4372-a567-0e02b2c3d479';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.botsubscription.com/v2/projects/{$project_id}/coupons");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: Bearer YOUR_API_KEY',
    'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
    'code' => 'SUMMER2024',
    'display_name' => 'Summer Sale',
    'description' => 'Summer promotion discount',
    'coupon_type' => 'percentage',
    'percentage' => '20',
    'min_spend_amount' => '1',
    'min_spend_currency' => 'USD',
    'max_spend_amount' => '100',
    'max_spend_currency' => 'USD',
    'usage_limit' => 100,
    'plan_scope' => 'include',
    'plan_ids' => ['b2c9f6f0-7a47-4c2e-9a0e-4a4fa1d8c1c4'],
    'starts_at' => 1717200000000,
    'ends_at' => 1725148799000
]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
```

</ApiRequestSample>

<ApiResponseTabs>
  <ApiResponse status={201} description="Coupon created">

```json
{
  "ok": true,
  "request_id": "11111111-1111-1111-1111-111111111111",
  "method": "POST",
  "path": "/v2/projects/11111111-1111-4111-8111-111111111111/coupons",
  "code": 201,
  "data": {
    "coupon_id": "11111111-1111-1111-1111-111111111111",
    "code": "SUMMER2024",
    "display_name": "Summer Sale",
    "description": "Summer promotion discount",
    "coupon_type": "percentage",
    "percentage": "20",
    "auto_apply": false,
    "status": "active",
    "total_redemptions": 0,
    "total_reservations": 0,
    "invitee_mode": "all",
    "renewal_constraint": "any",
    "payment_count_comparator": "any",
    "lifetime_revenue_comparator": "any",
    "plan_scope": "all",
    "starts_at": "2024-06-01T00:00:00.000Z",
    "ends_at": "2024-08-31T23:59:59.000Z",
    "created_at": "2024-05-15T10:00:00.000Z",
    "metadata": {},
    "min_spend_amount": "1",
    "min_spend_currency": "USD",
    "max_spend_amount": "100",
    "max_spend_currency": "USD"
  }
}
```

  </ApiResponse>
  <ApiResponse status={422} description="Validation error">

```json
{
  "ok": false,
  "request_id": "11111111-1111-1111-1111-111111111111",
  "method": "POST",
  "path": "/v2/projects/11111111-1111-4111-8111-111111111111/coupons",
  "code": 422,
  "errors": [
    {
      "message": "Invalid coupon_type: must be one of: percentage, fixed",
      "error_code": "VALIDATION_ERROR"
    }
  ]
}
```

  </ApiResponse>
  <ApiResponse status={401} description="Invalid or expired API key">

```json
{
  "ok": false,
  "request_id": "44444444-4444-4444-8444-444444444444",
  "method": "POST",
  "path": "/v2/projects/11111111-1111-4111-8111-111111111111/coupons",
  "code": 401,
  "error": {
    "message": "Invalid or expired authentication token.",
    "error_code": "UNAUTHORIZED"
  }
}
```

  </ApiResponse>
<ApiResponse status={201} description="Fixed-amount coupon">

```json
{
  "ok": true,
  "request_id": "11111111-1111-1111-1111-111111111111",
  "method": "POST",
  "path": "/v2/projects/11111111-1111-4111-8111-111111111111/coupons",
  "code": 201,
  "data": {
    "coupon_id": "11111111-1111-1111-1111-111111111111",
    "code": "FIXED10",
    "display_name": "Ten dollar discount",
    "description": "Summer promotion discount",
    "coupon_type": "fixed",
    "auto_apply": false,
    "status": "active",
    "total_redemptions": 0,
    "total_reservations": 0,
    "invitee_mode": "all",
    "renewal_constraint": "any",
    "payment_count_comparator": "any",
    "lifetime_revenue_comparator": "any",
    "plan_scope": "all",
    "starts_at": "2024-06-01T00:00:00.000Z",
    "ends_at": "2024-08-31T23:59:59.000Z",
    "created_at": "2024-05-15T10:00:00.000Z",
    "metadata": {},
    "min_spend_amount": "1",
    "min_spend_currency": "USD",
    "max_spend_amount": "100",
    "max_spend_currency": "USD",
    "amount": "10",
    "currency": "USD"
  }
}
```

</ApiResponse>
<ApiResponse status={409} description="Created resource response cannot be replayed">

```json
{
  "ok": false,
  "request_id": "44444444-4444-4444-8444-444444444444",
  "method": "POST",
  "path": "/v2/projects/11111111-1111-4111-8111-111111111111/coupons",
  "code": 409,
  "error": {
    "error_code": "IDEMPOTENCY_RESPONSE_NOT_REPLAYABLE",
    "message": "A request with this Idempotency-Key was already processed, but its response cannot be replayed",
    "details": [
      {
        "resource_type": "coupon",
        "resource_id": "55555555-5555-4555-8555-555555555555"
      }
    ]
  }
}
```

</ApiResponse>
</ApiResponseTabs>

</ApiSamplePane>
