$shop = ShopifyApp::shop();
if( $shopSettings->discount_id == '' || $shopSettings->discount_id == null || $shopSettings->discount_id == undefined ) {
$price_rule = $shop->api()->rest(
'POST',
'/admin/price_rules.json',
[
"price_rule" => [
"title" => "SUMMERSALE10OFF",
"target_type" => "line_item",
"target_selection" => "all",
"allocation_method" => "across",
"value_type" => "percentage",
"value" => "-"+request('discount_percentage'),
"customer_selection" => "all",
"starts_at" => "2017-01-19T17:59:10Z"
]
]
);
$shopSettings->price_rule_id = $price_rule->body->price_rule->id;
$discount_code = $shop->api()->rest(
'POST',
'/admin/price_rules/'.$price_rule->body->price_rule->id.'/discount_codes.json',
[
"discount_code" => [
"code" => $price_rule->body->price_rule->title
]
]
);
$shopSettings->discount_id = $discount_code->body->discount_code->id;
} else if( request('discount_percentage') && request('discount_type') == "percentage" ) {
$price_rule = $shop->api()->rest(
'POST',
'/admin/price_rules/'.$shopSettings->price_rule_id.'.json',
[
"price_rule" => [
"value" => "-"+request('discount_percentage')
]
]
);
}
Answer from YoJey Thilipan on Stack OverflowShopify
shopify.dev › docs › api › admin-graphql › latest › objects › discountcodenode
DiscountCodeNode - GraphQL Admin
A list of custom fields that a merchant associates with a Shopify resource. ... Connection! ... Returns a code discount resource by ID.
Shopify
shopify.dev › docs › api › functions › latest › discount
Discount Function API
The Discount Function API provides a unified schema for creating Function extensions. A single Function processes one discount (either code-based or automatic), but can apply savings across three discount classes: product, order, and shipping.
Videos
Shopify Functions - New Discount Function | Discount API - YouTube
30:03
Build a Custom Discount app with Shopify Admin API - YouTube
shopify api create discount code
Shopify Tutorial - Create a Discount Shopify Function
05:31
How to Create Discount Code & Price Rule Through Shopify Rest Admin ...
Shopify
shopify.dev › docs › api › checkout-ui-extensions › 2024-10 › apis › discounts
Discounts API - Shopify.dev
Returns a function to add or remove discount codes. (change: DiscountCodeChangeDiscountCodeChange) => Promise<DiscountCodeChangeResultDiscountCodeChangeResult> ... The type of the `DiscountCodeChange` API.
Shopify
shopify.dev › docs › api › admin-graphql › latest › mutations › discountCodeBasicCreate
discountCodeBasicCreate - GraphQL Admin
{ "basicCodeDiscount": { "title": "$20 off for VIP customers", "code": "VIP20OFF", "startsAt": "2025-07-24T16:16:22-04:00", "endsAt": null, "context": { "customerSegments": { "add": [ "gid://shopify/Segment/210588551" ] } }, "customerGets": { "value": { "discountAmount": { "amount": "20.00", "appliesOnEachItem": false } }, "items": { "all": true } }, "appliesOncePerCustomer": true } } curl -X POST \ https://your-development-store.myshopify.com/admin/api/2025-10/graphql.json \ -H 'Content-Type: application/json' \ -H 'X-Shopify-Access-Token: {access_token}' \ -d '{ "query": "mutation CreateSegmentDiscountCode($basicCodeDiscount: DiscountCodeBasicInput!)
Top answer 1 of 3
5
$shop = ShopifyApp::shop();
if( $shopSettings->discount_id == '' || $shopSettings->discount_id == null || $shopSettings->discount_id == undefined ) {
$price_rule = $shop->api()->rest(
'POST',
'/admin/price_rules.json',
[
"price_rule" => [
"title" => "SUMMERSALE10OFF",
"target_type" => "line_item",
"target_selection" => "all",
"allocation_method" => "across",
"value_type" => "percentage",
"value" => "-"+request('discount_percentage'),
"customer_selection" => "all",
"starts_at" => "2017-01-19T17:59:10Z"
]
]
);
$shopSettings->price_rule_id = $price_rule->body->price_rule->id;
$discount_code = $shop->api()->rest(
'POST',
'/admin/price_rules/'.$price_rule->body->price_rule->id.'/discount_codes.json',
[
"discount_code" => [
"code" => $price_rule->body->price_rule->title
]
]
);
$shopSettings->discount_id = $discount_code->body->discount_code->id;
} else if( request('discount_percentage') && request('discount_type') == "percentage" ) {
$price_rule = $shop->api()->rest(
'POST',
'/admin/price_rules/'.$shopSettings->price_rule_id.'.json',
[
"price_rule" => [
"value" => "-"+request('discount_percentage')
]
]
);
}
2 of 3
4
To all those coming here, Shopify has recently developed an endpoint to create discount codes and price rules.
Price Rule
Discount Code
Shopify
shopify.dev › docs › api › functions › reference › product-discounts
Discount Function API - Shopify.dev
The Discount Function API provides a unified schema for creating Function extensions. A single Function processes one discount (either code-based or automatic), but can apply savings across three discount classes: product, order, and shipping.
Shopify
shopify.dev › docs › api › checkout-ui-extensions › latest › apis › discounts
Discounts
Returns a function to add or remove discount codes. (change: DiscountCodeChangeDiscountCodeChange) => Promise<DiscountCodeChangeResultDiscountCodeChangeResult> ... The type of the `DiscountCodeChange` API.
Shopify
shopify.dev › docs › api › admin-rest › latest › resources › discountcode
DiscountCode
After setting up the logic for a discount using the PriceRule API, use the DiscountCode API to associate the discount with a code, which can be entered at the checkout to apply the discount.
Yepcode
yepcode.io › recipes › rest-api-to-shopify-discount-codes
REST API to Shopify discount codes | YepCode Recipes | Code snippets for solving common problems
More info at https://yepcode.io/docs/processes/team-variables // TODO: Add your http credential with Shopify information: // baseUrl: https://your-development-store.myshopify.com/admin/api/2022-04 // HTTP Headers: { "X-Shopify-Access-Token": "your-access-token" } this.httpClient = axios.create({ baseURL: yepcode.env.SHOPIFY_BASE_URL, headers: { "X-Shopify-Access-Token": yepcode.env.SHOPIFY_ACCESS_TOKEN, }, }); } async consume(item) { // TODO: Customize your request checking the API documentation: // https://shopify.dev/api/admin-rest/2022-04/resources/discountcode#post-price-rules-price-rule-id-discount-codes await this.httpClient.post( "/price_rules/<price-rule-id>/discount_codes.json", { discount_code: { code: item.code + "OFF", }, } ); } async close() {} }
Shopify
shopify.dev › docs › apps › build › discounts
About discounts
Build configuration UIs with Admin UI extensions or React Router App UI, to allow merchants to configure the discount functionality in the Shopify admin. The GraphQL Admin API enables you to create and manage Shopify discounts.
Nozzlegear
nozzlegear.com › shopify › check-if-a-Shopify-discount-code-is-valid-from-the-storefront-or-script-tag
Check if a Shopify discount code is valid from the storefront or Script Tag | Nozzlegear Software
Script Tags and scripts on the storefront can not access the discount code object itself. There's no way to get the usage count, expiration, and most importantly, the amount, from the storefront. If you need to know that information, you need to use the Price Rules API from an app. Shopify does offer a modicum of help here at least: if you open up the network tab in your browser and monitor the lookup request, you'll see that Shopify is responding with a 303 redirect to where the discount code API object is located.
Shopify Community
community.shopify.com › retired boards › appdev › customers discounts and orders
API Discount Codes
April 16, 2021 - I have a client I am building a site for. They are interested in an API Coupon code generation and is wondering how our systems will speak to each other. Is this possible to have within the Shopify store? Is so, how do I get this all set up? They have roughly 12K users that will be using this ...
GitHub
github.com › thirdweb-example › shopify-discount-codes
GitHub - thirdweb-example/shopify-discount-codes: Grant users who hold an NFT from your collection a discount code they can use in your Shopify store!
This API route interacts with the Shopify API to generate a discount code for the user after checking their NFT balance.
Starred by 13 users
Forked by 10 users
Languages TypeScript 63.2% | CSS 35.1% | JavaScript 1.7%
Shopify
shopify.dev › docs › storefronts › headless › hydrogen › cart › discount-codes
Update discount codes
placeholder="Discount code" /> <button> Apply Discount · </button> </CartForm> ); } 99 · 1 · 2 · 3 · 4 · 5 · 6 · 7 · 8 · 9 · 10 · 11 · 12 · 13 · 14 · 15 · 16 · 17 · 18 · 19 · 20 · 21 · 22 · 23 · 24 · 25 · 26 · 27 · 28 · 29 · import {CartForm} from '@shopify/hydrogen'; import type {Cart} from '@shopify/hydrogen/storefront-api-types'; export default function DiscountCodeForm({discountCodes}: { discountCodes: Cart['discountCodes']; }) { const codes: string[] = discountCodes ·