> ## Documentation Index
> Fetch the complete documentation index at: https://memberful.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Create coupons in bulk via API

> Create coupon codes in bulk through the Memberful API using a mutation that generates multiple codes from an existing coupon and groups them in your dashboard.

In this help article, we'll show you how to create a base coupon, find its ID, and then use that ID to generate a mutation that will create multiple coupon codes.

You can also [add up to 1,000 coupon codes at a time from your Memberful dashboard](https://memberful.com/docs/discounts-and-gifting/coupons/create-a-coupon.md#add-coupon-codes) without using the API.

## Use the GraphQL API Explorer

You can make requests to our API in all the regular ways you're used to, but the easiest method is through our [GraphQL API Explorer](https://memberful.com/docs/api-reference/memberful-api.md#using-the-graphql-api-explorer). To access it, go to **Settings** → **Custom applications** in your Memberful dashboard.

Queries and mutations that you execute in the API Explorer work the same as requests that come from elsewhere, so you can generate your coupons in bulk using the API Explorer.

## Create a base coupon

Start by [creating a base coupon](https://memberful.com/docs/discounts-and-gifting/coupons/create-a-coupon.md#create-a-coupon). Make sure to set the max redemption to "1" to ensure each code is single-use.

All your new coupons inherit the same settings as this base coupon, and they're grouped under it in your Memberful dashboard.

## Find the base coupon's ID

From the **Edit coupon** page, take note of the coupon's ID. You'll find it under the label *Coupon ID for generating bulk coupon codes.*

![Finding Coupon ID](https://mintcdn.com/memberful/mYciGmTXw7Ae6ay0/images/custom-development-and-api/create-coupons-in-bulk-via-api/finding-coupon-id.png?fit=max\&auto=format\&n=mYciGmTXw7Ae6ay0\&q=85\&s=0b94f269e5a25eeda7189af1a2fba4ca)

## Review a mutation example

Go to the [API Explorer](https://memberful.com/docs/api-reference/memberful-api.md#using-the-graphql-api-explorer) and run your mutation. Keep your coupon ID handy because you're about to use it.

This is the format you need to follow for this mutation:

```graphql theme={null}
mutation {
  createCoupons(discountId: YOURCOUPONID, coupons: [{ code: "NEWCOUPONCODE1"}, { code: "NEWCOUPONCODE2"}, { code: "NEWCOUPONCODE3" }]) {
    coupons {
      code
    }
    errors {
      key
      messages {
        attribute
        message
      }
    }
  }
}
```

You should replace **YOURCOUPONID** with the coupon ID you copied before, and also replace **NEWCOUPONCODEX** with the coupon codes you'd like to create.

Here's an example mutation that creates 3 codes from a base coupon with the ID **24**:

```graphql theme={null}
mutation {
  createCoupons(discountId: 24, coupons: [{ code: "membersonly1"}, { code: "membersonly2"}, { code: "membersonly3" }]) {
    coupons {
      code
    }
    errors {
      key
      messages {
        attribute
        message
      }
    }
  }
}
```

## Generate a mutation for a large number of coupons

If you need to create a large number of coupons, use the following form to generate a mutation that assigns unique names to your coupons.

<form>
  Coupon ID:

  Base coupon name:

  Number of coupons:

  Include random characters (Recommended)

  Generate Mutation
</form>

Including random characters (letters and numbers) in your coupon codes ensures that members won't be able to guess other codes based on the one they got.

For example, if a member received the coupon code **memberdiscount22**, it would be easy for them to guess that **memberdiscount21** and **memberdiscount23** exist, and they might abuse that knowledge to get more discounts or to share those coupons with others who aren't entitled to a discount.

## Review bulk coupon requirements and troubleshooting steps

If your coupon codes were created successfully, the result will show the successfully created codes inside the *coupons* array, and *errors* will show an empty array.

```json theme={null}
{
  "data": {
    "createCoupons": {
      "coupons": [
        {
          "code": "membersonly1"
        },
        {
          "code": "membersonly2"
        },
        {
          "code": "membersonly3"
        }
      ],
      "errors": []
    }
  }
}
```

Now, here's an example result in which some coupons failed to be created, while others succeeded:

```json theme={null}
{
  "data": {
    "createCoupons": {
      "coupons": [
        {
          "code": "membersonly4"
        },
        {
          "code": "membersonly5"
        },
        {
          "code": "membersonly6"
        }
      ],
      "errors": [
        {
          "key": "membersonly_7",
          "messages": [
            {
              "attribute": "code",
              "message": "Code is invalid."
            }
          ]
        },
        {
          "key": "membersonly1",
          "messages": [
            {
              "attribute": "code",
              "message": "Code is taken."
            }
          ]
        }
      ]
    }
  }
}
```

To troubleshoot a result like this, review the error messages, then check that all of your requested coupon codes meet the following requirements:

* Maximum 255 characters
* Contains no symbols or spaces
* The code doesn't already exist

Coupon codes are case-insensitive, so *mycouponZ* will fail to be created if *mycouponz* already exists.

Here are some valid and invalid examples for a base coupon with the code *membersonly:*

* ✅ membersonly1
* ✅ membersonly987
* ✅ membersonlyA
* ✅ membersonlyxyz
* ✅ summersale (The new code doesn't need to resemble the base coupon code)
* ❌ membersonly\_1 (Symbols aren't allowed)
* ❌ membersonly (Already exists - this is the same code as the base coupon)
* ❌ MembersOnly (Already exists - uppercase/lowercase doesn't make a difference)

If some of your coupon codes were created successfully and others failed, there's no need to resubmit the ones that were successful — those have already been created. Fix the issues with the failed ones and resubmit them.

## Manage your bulk coupon codes

After creating bulk coupon codes, you'll see how many codes are associated with the base coupon when you edit it.

![Editing Bulk Coupon](https://mintcdn.com/memberful/mYciGmTXw7Ae6ay0/images/custom-development-and-api/create-coupons-in-bulk-via-api/editing-bulk-coupon.png?fit=max\&auto=format\&n=mYciGmTXw7Ae6ay0\&q=85\&s=9d756362b565ee572f1d4497fd4e74a8)

You can access a [more detailed breakdown](https://memberful.com/docs/discounts-and-gifting/coupons/apply-and-manage-coupons.md#track-coupon-usage) of how your coupon has been used by clicking **View coupon usage**.

Purchase links with a [pre-applied coupon code](https://memberful.com/docs/discounts-and-gifting/coupons/apply-and-manage-coupons.md#apply-a-coupon-code-at-checkout) also work with coupon codes that were created in bulk.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.