# POST /coupon/{{product_id}} — Create Coupon

> Product: **Pabbly Subscription Billing** (v1)
> Base URL: `https://payments.pabbly.com/api/v1`
> Auth: Basic via `Authorization` header
> Canonical: `/subscription-billing/coupon/create-coupon`

The POST request API, which is used to create a new coupon. product_id will be added in the Request URL.

**Path parameters:**

| Name | Type | Required | Description |
|------|------|----------|-------------|
| product_id | string | Yes |  |

**Body parameters:**

| Name | Type | Required | Description |
|------|------|----------|-------------|
| coupon_name | string | No | Name of your coupon. |
| coupon_code | string | No | This code will be used by the customer to avail discounts. |
| discount | string | No | Discounted number either percent or flat. |
| discount_type | string | No | It can be - percent - Coupon will be applied as a percent on a subscription. flat - Coupon will be applied as flat on a subscription. |
| redemption_type | string | No | Choose how many times this coupon will be used. onetime– Will be applied for a single time on a subscription. forever – Forever type discount. number_time – For limited redemption type discount. |
| redemption_cycle | string | No | Filled when the redemption type is number time. |
| associate_plans | string | No | It can be "all_plans" or "selected_plans". Specify this discount for All plans or specific plans. |
| plans_array | string | No | If associated plans is ‘selected_plans’ then add the plan Id in array. |
| valid_upto | string | No | Select the future date in which the coupon can be applied. |
| apply_to | string | No | It can be total_amount - 'The coupon will be applied for total amount including plan, setup fee, addon amount etc.' subscription_amount - 'Apply coupon only to the subscription fee and not to the setup fee or other fees.' addon_amount - 'Apply coupon only to the addon fee and not to the subscription fee or other fees.' |
| maximum_redemption | string | No | How many times this coupon can be used. |
| apply_affiliate | string | No | It can be "yes" or "no". |
| affiliate_id | string | No | Affiliate ID. |

**Example request body:**

```json
{
  "coupon_name": "Special Offer",
  "coupon_code" : "SPL-OFF",
  "discount": 25,
  "discount_type": "flat", //Required if, discount type is set as flat | percent
  "redemption_type": "forever", //Required if, redemption type is set as number_time | onetime | forever
  "redemption_cycle": 2,
  "associate_plans": "selected_plans",
  "plans_array": ["62a354b2ffddb0508a05ed91","62d69e07ad0fcc7f11214798"],
  "valid_upto": "2030-10-02",
  "apply_to":"total_amount", //Required if, apply to is set as subscription_amount | addon_amount | total_amount
  "maximum_redemption":10,
  "status": "active",
  "apply_affiliate": "yes",
  "affiliate_id": "5ff6984857b2331ca3011fbb"
}
```

**Response (200)** — Create Coupon:

```json
{
    "status": "success",
    "message": "Coupon created",
    "data": {
        "status": "active",
        "apply_to": "total_amount",
        "createdAt": "2020-03-04T05:25:50.089Z",
        "updatedAt": "2020-03-04T05:25:50.089Z",
        "id": "5e5f3bde7c5aeb3675f41685",
        "product_id": "5e3a95143c92e44b424b6d47",
        "coupon_name": "Special Offer",
        "coupon_code": "SPL-OFF",
        "discount": 25,
        "discount_type": "flat",
        "redemption_type": "number_time",
        "redemption_cycle": 2,
        "associate_plans": "selected_plans",
        "plans_array": [
            "5e3bf8b8db85462760295d2f",
            "5e3aa7133c92e44b424b6dec"
        ],
        "valid_upto": "2022-10-02",
        "maximum_redemption": 10,
        "used_redemption": 0
    }
}
```

**Code examples:**

_cURL_

```curl
curl -X POST https://payments.pabbly.com/api/v1/coupon/{{product_id}} \
  -u {{YOUR_API_KEY}}:{{YOUR_SECRET_KEY}}
```

_Ruby_

```ruby
require 'net/http'
require 'json'

uri = URI('https://payments.pabbly.com/api/v1/coupon/{{product_id}}')
request = Net::HTTP::Post.new(uri)
request.basic_auth '{{YOUR_API_KEY}}', '{{YOUR_SECRET_KEY}}'

response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
  http.request(request)
end

data = JSON.parse(response.body)
```

_Python_

```python
import requests
from requests.auth import HTTPBasicAuth

response = requests.post(
    'https://payments.pabbly.com/api/v1/coupon/{{product_id}}',
    auth=HTTPBasicAuth('{{YOUR_API_KEY}}', '{{YOUR_SECRET_KEY}}'),
)

data = response.json()
```

_PHP_

```php
<?php
$ch = curl_init('https://payments.pabbly.com/api/v1/coupon/{{product_id}}');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_USERPWD, '{{YOUR_API_KEY}}:{{YOUR_SECRET_KEY}}');

$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
```

_Java_

```java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Base64;

String credentials = Base64.getEncoder().encodeToString("{{YOUR_API_KEY}}:{{YOUR_SECRET_KEY}}".getBytes());

HttpClient client = HttpClient.newHttpClient();
HttpRequest.Builder builder = HttpRequest.newBuilder()
    .uri(URI.create("https://payments.pabbly.com/api/v1/coupon/{{product_id}}"))
    .header("Authorization", "Basic " + credentials)
    .method("POST", HttpRequest.BodyPublishers.noBody());

HttpRequest request = builder.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
```

_Node.js_

```node
const credentials = Buffer.from('{{YOUR_API_KEY}}:{{YOUR_SECRET_KEY}}').toString('base64');

const response = await fetch('https://payments.pabbly.com/api/v1/coupon/{{product_id}}', {
  method: 'POST',
  headers: {
    'Authorization': `Basic ${credentials}`,
  },
});

const data = await response.json();
```

_Go_

```go
package main

import (
    "fmt"
    "io"
    "net/http"
)

func main() {
    req, _ := http.NewRequest("POST", "https://payments.pabbly.com/api/v1/coupon/{{product_id}}", nil)
    req.SetBasicAuth("{{YOUR_API_KEY}}", "{{YOUR_SECRET_KEY}}")

    res, _ := http.DefaultClient.Do(req)
    defer res.Body.Close()
    body, _ := io.ReadAll(res.Body)
    fmt.Println(string(body))
}
```

_.NET_

```dotnet
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes("{{YOUR_API_KEY}}:{{YOUR_SECRET_KEY}}"));

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://payments.pabbly.com/api/v1/coupon/{{product_id}}");
request.Headers.TryAddWithoutValidation("Authorization", $"Basic {credentials}");

var response = await client.SendAsync(request);
var data = await response.Content.ReadAsStringAsync();
Console.WriteLine(data);
```

---

**Other endpoints in Coupon:**

- [GET /coupon/{{product_id}} — List All Coupons By Product](/subscription-billing/coupon/list-all-coupons-by-product)
- [DELETE /coupons/{{coupon_id}} — Delete Coupon](/subscription-billing/coupon/delete-coupon)
- [PUT /coupons/{{coupon_id}} — Update Coupon](/subscription-billing/coupon/update-coupon)

