Follow this tutorial to create coupon code in Magento 2 using REST API.
Cart price rules in Magento 2 help the merchants to create discounts and deals for the customers. The merchant can also create coupon codes in Magento 2 and distribute them to the customers via multiple channels. The coupon codes are some of the amazing ways to attract customers to your online store. If you are creating a new admin panel for your Magento 2 store, you can use the Magento 2 REST APIs to use the functionalities of Magento 2 in your new backend. One such functionality is creating coupon codes in Magento 2 using APIs.
In this blog post, I will show you how to create coupon codes in Magento 2 using API.
Let’s begin!
How to Create Coupon Code in Magento 2 Using REST API?
In order to create a coupon code in Magento 2 using the REST API, the client need to have admin-level access. Therefore, the admin access token should also be passed along with the API request. The client needs to call the API request to the /V1/coupons/generate endpoint along with the data in the body along with the admin token.
Note: In order to create a coupon code using API in Magento 2, you need to have an active cart price rule and pass its rule_id in the request. You can check out our post on creating cart price rules in Magento 2 using REST API for more details.
Provided below is the complete structure of the Magento 2 API to Create Coupon Codes.
Method: POST
URL: store_url/rest/V1/coupons/generate
Headers:
- Content Type: application/json
- Authorization: Bearer <admin token>
Body:
{ "couponSpec": { "rule_id":1, "quantity": 1, "length": 8 } }

Response:
The response will return the discount coupon code.
Conclusion
The Magento 2 API to create coupon code is useful when you are looking to create discount codes for the cart price rules. The developers can use this Magento 2 API for using the create coupon code functionality while developing a new backend. I hope this blog post will guide you through the Magento 2 API, and you will find it helpful.
Still have any queries regarding Magento 2 APIs? Feel free to comment. I will be happy to help you!
Do not forget to check out our other blog posts on Magento 2 APIs.
Also, do not forget to share this tutorial with your developer friends via social media.
Thanks for reading!