Create and manage discounts with the GraphQL Admin API
This guide describes how to create and retrieve discounts by using the GraphQL Admin API. It provides examples for two different types of discounts (automatic discounts and code discounts), and the recommended best practices for each type.
For a full list of the schema, see the GraphQL Admin API reference.
If you aren’t familiar with GraphQL, then refer to the Getting started with the GraphQL Admin and REST Admin APIs guide or The GraphQL Learning Kit to learn more.
Requirements
Anchor link to section titled "Requirements"- The mutations described here require the
write_discounts
permission. - The queries described here require the
read_discounts
permission.
Creating automatic discounts
Anchor link to section titled "Creating automatic discounts"Automatic discounts refer to the set of objects and mutations in GraphQL that let you create and manage discounts that are automatically applied to a cart if prerequisites are met. This method is available only in the GraphQL Admin API.
The following examples show two different types of automatic discounts:
Buy X Get Y or Spend X Get Y automatic discount
Anchor link to section titled "Buy X Get Y or Spend X Get Y automatic discount"This example shows how to create an automatic Buy One Get One discount that will be applied to a single product any time it’s included in a cart. In this case, it applies a 100% discount on the second item in a cart, as long as it’s the same product as the first item.
Based on that example, you can make a few different changes to offer specific types of discounts:
- If you want to change the discount amount, then edit
customerGets.value.discountOnQuantity.effect.percentage
. - If you want to apply the discount to a different item, then use
customerGets.items.products.productsToAdd
andcustomerGets.items.products.productsToRemove
to update the entitlements. - If you want to apply the discount to all items, then use
customerGets.items.all
instead of a specific product ID. - If you want a customer to get a discount no matter which item they purchase, then set the
customerBuys.items.all
prerequisite totrue
instead of a specific product.
Percentage or fixed-amount automatic discount
Anchor link to section titled "Percentage or fixed-amount automatic discount"This example shows how to create an automatic discount of 10% that will be applied to a single product any time it’s included in a cart. The percentage
input requires a value between 0 and 1. In the example below 0.10
represents 10% off. A 100% discount would be 1.0
and a 50% discount would be 0.5
.
Creating code discounts
Anchor link to section titled "Creating code discounts"Code discounts refer to the set of objects and mutations in GraphQL that let you create and manage discounts that include a unique code. Code discounts enable specific discounts to be redeemed when the codes are applied manually by the customer during the checkout. This method is available only in the GraphQL Admin API.
The following examples show three different types of code discounts:
- Buy X Get Y or Spend X Get Y code discount
- Free Shipping code discount
- Percentage or fixed-amount discount code
Buy X Get Y or Spend X Get Y code discount
Anchor link to section titled "Buy X Get Y or Spend X Get Y code discount"This example shows how to create a Spend $100 Get One discount that requires the customer to enter a code at checkout. In this case, it lets all customers use this code, and offers a spend $100 and get one discount on the same product. You can also change the discount so that it applies to a different product than the original.
Percentage or fixed-amount discount code
Anchor link to section titled "Percentage or fixed-amount discount code"This example shows how to associate a flat percentage or fixed-amount discount with a code that the customer can use at checkout. Similar in format to creating a buy one get one discount, here we will create a $1.00 off discount for a specific product.
Free Shipping code discount
Anchor link to section titled "Free Shipping code discount"This example shows how to associate a Free Shipping discount with a code that will grant the customer free shipping when they use it. In this example, the discount applies to a saved search of customers in customerSelection.customerSavedSearches
if those customers are from Canada. If you want to provide the discount to all countries, then you can replace destination.countries.add
with destination.all
set to true
. Note that if you specify a country where you want to offer free shipping, the shop must have a shipping zone configured for that country before it will work.
Managing Discounts
Anchor link to section titled "Managing Discounts"You can use the API to manage and make changes to your current discounts.
Query automatic discounts on a shop
Anchor link to section titled "Query automatic discounts on a shop"When making GraphQL queries, you can return multiple objects at a time. The following example shows how to request the first 10 automatic discount codes, which returns the details about whether they’re either the BxGy
or Basic
type.
Query a specific automatic discount
Anchor link to section titled "Query a specific automatic discount"To query a specific automatic discount, all you need is its ID. The following example query might look complicated because it uses a concept called fragments. This lets you create a query that returns the information only if it matches your criteria. In this case, we are requesting all possible information about this discount no matter what type it is, in case we don’t know the details of the automatic discount beforehand.
Activate and deactivate automatic discounts
Anchor link to section titled "Activate and deactivate automatic discounts"To activate or deactivate an automatic discount, you can use the mutations discountAutomaticActivate
or discountAutomaticDeactivate
. The only input required is the ID of the automatic discount to perform the action on.
Query code discounts on a shop
Anchor link to section titled "Query code discounts on a shop"The following example shows how to query basic code discounts on a shop. As with all GraphQL queries, you can request only the details that you want in the query to keep it short. We’ll expand on all possible fields here in the next examples so no need to include them all twice.
Query a specific discount code
Anchor link to section titled "Query a specific discount code"Similar to automatic discounts, you can query a specific code by ID and request only the information you want. Here is an example of all possible fields on a basic discount code. In the following example, you could replace the DiscountCodeBasic
fragment with DiscountCodeBxgy
or DiscountCodeFreeShipping
. Although they all have a slight variation in the return fields, the basic format is the same.
Update an automatic discount
Anchor link to section titled "Update an automatic discount"Updating a discount is done in the same way as creating a discount, but it has a unique mutation. Use discountAutomaticBxgyUpdate
if it’s a BxGy discount, or discountAutomaticBasicUpdate
if it’s a basic discount. Note that when updating a discount code, you have to explicitly add and remove entitled products by using productsToAdd
and productsToRemove
as shown in the example below. You can add or remove multiple products by passing them in a comma-separated list.
Update a code discount
Anchor link to section titled "Update a code discount"Updating a discount is done in the same way as creating a discount, but it has a unique mutation. Use discountCodeBxgyUpdate
if it’s a BxGy discount, discountCodeBasicUpdate
if it's a basic discount, or discountCodeFreeShippingUpdate
if it’s a Free Shipping discount. If you want to update a discount, then you need to pass in the ID of the code that you want to update. Also note that you only need to include the fields that you want to update (in the following example, only the title is included).
Delete an automatic discount
Anchor link to section titled "Delete an automatic discount"To delete an automatic discount, all you need is its ID.
Bulk delete automatic discount
Anchor link to section titled "Bulk delete automatic discount"The following example shows how to delete automatic discount codes in bulk by specifying an array of IDs.
Bulk activate code discounts
Anchor link to section titled "Bulk activate code discounts"Similarly, you can activate code discounts in bulk by using the discountCodeBulkActivate
mutation.
Check on job status
Anchor link to section titled "Check on job status"To verify if a previous bulk discount operation has completed, you can query the mutation's returned job.
Paginate through discount codes
Anchor link to section titled "Paginate through discount codes"If you aren't familiar with pagination through GraphQL, then see the Shopify GraphQL Learning Kit. The way that the pageInfo
and cursor
parameters are structured in the discounts object is similar to how they're structured elsewhere in GraphQL. The following example shows a sample query with these parameters.