Create, Update, Delete A Discount Code

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

In a previous article, I showed you how to create a Price Rule with the 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 to use. This is a reason today 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

Generate API credentials from the Shopify admin
  • 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
  • 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 need to create a Price Rule. Remember that Price Rule ID 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 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
  • Paste the URL above.
Post

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

Step 3: Verify The Result

In the Shopify Admin, Choose Discounts 

Result

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 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
  • Paste the URL above.
Put

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

Step 2: Verify the Updated Discount Code

In Admin, choose Discounts 

Result

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 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
  • Paste the URL above.
Delete

Then Click SEND

Response:

An empty array

Step 2: Verify the Result

In Admin, choose Discounts 

Result

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 Devdocs

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

Shopify API – Create, Update, Delete A Discount Code

Hopefully, you can do it!

Related Articles

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on print
Share on email