Create, Update, Delete A Discount Code

Shopify API 2023 – Create, Update, and Delete A Discount Code With Postman In Shopify

In a previous article, I showed you how to create a Price Rule with Postman, Price rule is a necessary condition to create a discount code. You can define the parameters for a discount with the Price Rule resource, but you’ll need to create associated discount codes for customers. I will show you how to create, update and delete a discount code with Postman In Shopify

Create A Discount Code Using Postman In Shopify

Step 1: Generate API credentials from the Shopify admin

  • Log in to your Shopify admin
  • Go to Apps – Manage private apps
Generate API credentials from the Shopify admin
Shopify API 2023 - Create, Update, and Delete A Discount Code With Postman In Shopify 14
  • Click Create a new private app
  • In the App details section, enter a name for your app, and an emergency developer email
  • In the Admin API section, select the areas of your store that you want the app to access
Generate API credentials from the Shopify admin
Shopify API 2023 - Create, Update, and Delete A Discount Code With Postman In Shopify 15
  • Click Save

The Admin API section displays the app’s API key and password when you save the app’s details. 

Step 2: Create a discount code using Postman in Shopify

Before creating a discount code using Postman, you must create a Price Rule. Remember that Price Rule ID is to use in this step. 

  • Log in to your Postman and create a new workspace
  • Create a new POST with this URL:
https://{username}:{password}@{shop}.myshopify.com/admin/api/{api-version}/{resource}.json
Code language: JavaScript (javascript)
  1. {username} – The API key that you generated.
  2. {password} – The API password that you generated.
  3. {shop} – The name of your development store.
  4. {api-version} – The supported API version that you want to use.
  5. {resource} – A resource endpoint from the REST Admin API.

Or you can copy this example in the Shopify Admin, remember to change {resource} to {price_rules/price rule ID/discount_codes}:

https://33a72f01e02987636e1c1a9a4e95c840:[email protected]/admin/api/2021-10/price_rules/1001616998463/discount_codes.json
Code language: JavaScript (javascript)
Example URL
Shopify API 2023 - Create, Update, and Delete A Discount Code With Postman In Shopify 16
  • Paste the URL above.
Post
Shopify API 2023 - Create, Update, and Delete A Discount Code With Postman In Shopify 17

In the body section, enter this code, then click Send.

{ "discount_code": { "code": "SUMMERSALE10OFF" } }
Code language: JSON / JSON with Comments (json)

Response:

The API will return all information about the created discount code.

{ "discount_code": { "id": 12231414743103, "price_rule_id": 1001616998463, "code": "SUMMERSALE10OFF", "usage_count": 0, "created_at": "2021-12-16T16:44:57+09:00", "updated_at": "2021-12-16T16:44:57+09:00" } }
Code language: JSON / JSON with Comments (json)
Response
Shopify API 2023 - Create, Update, and Delete A Discount Code With Postman In Shopify 18

Step 3: Verify The Result

In the Shopify Admin, Choose Discounts 

Result
Shopify API 2023 - Create, Update, and Delete A Discount Code With Postman In Shopify 19

As you can see, the discount code has been created successfully.

Update An Existing Discount Code Using Postman In Shopify

Step1: Update An Existing Discount Code Using Postman In Shopify

  • Create a new PUT with this URL:
https://{username}:{password}@{shop}.myshopify.com/admin/api/{api-version}/{resource}.json
Code language: JavaScript (javascript)

Or you can copy this example in the Shopify Admin,

Remember to change {resource} to {price_rules/price rule ID/discount_codes/discount code ID}:

https://33a72f01e02987636e1c1a9a4e95c840:[email protected]/admin/api/2021-10/price_rules/1001616998463/discount_codes/12231414743103.json
Code language: JavaScript (javascript)
Example URL
Shopify API 2023 - Create, Update, and Delete A Discount Code With Postman In Shopify 20
  • Paste the URL above.
Put
Shopify API 2023 - Create, Update, and Delete A Discount Code With Postman In Shopify 21

In the body section, enter this code, then click Send.

{ "discount_code": { "Id":12231414743103, "code": "WINTERSALE10OFF" } }
Code language: JSON / JSON with Comments (json)

Response:

The API will return all information about the updated discount code.

{ "discount_code": { "id": 12231414743103, "price_rule_id": 1001616998463, "code": "WINTERSALE10OFF", "usage_count": 0, "created_at": "2021-12-16T16:44:57+09:00", "updated_at": "2021-12-16T16:46:26+09:00" } }
Code language: JSON / JSON with Comments (json)
Response
Shopify API 2023 - Create, Update, and Delete A Discount Code With Postman In Shopify 22

Step 2: Verify the Updated Discount Code

In Admin, choose Discounts 

Result
Shopify API 2023 - Create, Update, and Delete A Discount Code With Postman In Shopify 23

As you can see, the discount code has been updated. 

Delete A Discount Code Using Postman In Shopify

Step 1: Delete A Discount Code Using Postman In Shopify

  • Create a new DELETE with this URL:
https://{username}:{password}@{shop}.myshopify.com/admin/api/{api-version}/{resource}.json
Code language: JavaScript (javascript)

Or you can copy this example in the Shopify Admin, remember to change {resource} to {price_rules/price rule ID/discount_codes/discount code ID}:

https://33a72f01e02987636e1c1a9a4e95c840:[email protected]/admin/api/2021-10/price_rules/1001616998463/discount_codes/12231414743103.json
Code language: JavaScript (javascript)
Example URL
Shopify API 2023 - Create, Update, and Delete A Discount Code With Postman In Shopify 24
  • Paste the URL above.
Delete
Shopify API 2023 - Create, Update, and Delete A Discount Code With Postman In Shopify 25

Then Click SEND

Response:

An empty array

Step 2: Verify the Result

In Admin, choose Discounts 

Result
Shopify API 2023 - Create, Update, and Delete A Discount Code With Postman In Shopify 26

As you can see, the discount code has been deleted. 

I have shown you all steps to create, update and delete a discount code with Shopify API using Postman. If you have any questions, please leave a comment below or refer to our blog: What is an API?. Besides, you can see more Shopify API documentation in the developer docs.

If you still do not understand the tutorial’s content, you can watch the video below for a more overview.

Shopify API – Create, Update, and Delete A Discount Code

Hopefully, you can do it!