{"id":2608,"date":"2024-12-31T20:23:15","date_gmt":"2024-12-31T20:23:15","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/magento-2-api-create-coupon-code\/"},"modified":"2025-03-17T08:50:59","modified_gmt":"2025-03-17T08:50:59","slug":"magento-2-api-create-coupon-code","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/magento-2-api-create-coupon-code\/","title":{"rendered":"Magento 2 API &#8211; Create Coupon Code"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Follow this tutorial to&nbsp;<em><strong>create coupon code in Magento 2 using REST API<\/strong><\/em>.  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Cart price rules in Magento 2 help the merchants to create discounts and deals for the customers. The merchant can also&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/add-magento-shopping-cart-price-rules\/\" target=\"_blank\" rel=\"noreferrer noopener\">create coupon codes in Magento 2<\/a>&nbsp;and distribute them to the customers via multiple channels. The coupon codes are&nbsp; 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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this blog post, I will show you how to create coupon codes in Magento 2 using API.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s begin!  <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Create Coupon Code in Magento 2 Using REST API?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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&nbsp;<em><strong>\/V1\/coupons\/generate&nbsp;<\/strong><\/em>endpoint along with the data in the body along with the admin token.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:&nbsp;<\/strong>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&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/magento-2-api-create-cart-price-rule\/\" target=\"_blank\" rel=\"noreferrer noopener\">cart price rules in Magento 2 using REST API<\/a>&nbsp;for more details.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Provided below is the complete structure of the Magento 2 API to Create Coupon Codes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Method:&nbsp;<\/strong>POST<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>URL:&nbsp;<\/strong><em>store_url\/rest\/V1\/coupons\/generate<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Headers:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Content Type:<\/strong>&nbsp;application\/json<\/li>\n\n\n\n<li><strong>Authorization:<\/strong>&nbsp;Bearer &lt;admin token&gt;<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Body:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{\n    \"couponSpec\": {\n        \"rule_id\":1,\n        \"quantity\": 1,\n        \"length\": 8\n    }\n}<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2022\/09\/Create-Coupon-Code-in-Mageto-2-REST-API.png\" alt=\"Create Coupon Code in Magento 2 REST API\" class=\"wp-image-50734\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Response:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The response will return the discount coupon code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Still have any queries regarding Magento 2 APIs? Feel free to comment. I will be happy to help you!  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do not forget to check out our other blog posts on&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/magento-2-api\/\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 APIs<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, do not forget to share this tutorial with your developer friends via social media.  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks for reading!  <\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button btn-orange\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/meetanshi.com\/blog\/magento-2-api-create-cart-price-rule\/\">Magento 2 API \u2013 Create Cart Price Rule<\/a><\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button btn-orange\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/meetanshi.com\/blog\/magento-2-api-wishlist\/\">Magento 2 API \u2013 Wishlist<\/a><\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Follow this tutorial to&nbsp;create coupon code in Magento 2 using REST API. Cart price rules in Magento 2 help the merchants to create discounts and&#8230;<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-2608","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2608","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=2608"}],"version-history":[{"count":2,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2608\/revisions"}],"predecessor-version":[{"id":9489,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2608\/revisions\/9489"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=2608"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=2608"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=2608"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}