$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 Overflow
🌐
Shopify
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.
🌐
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!)
🌐
Digioh Help Docs
help.digioh.com › home › shopify dynamic couponing
Shopify Discount Code API Integration - Digioh Help Docs
February 13, 2025 - Learn how to use the Shopify Discount Code API integration to create Shopify coupon codes dynamically. You need: API token and price rule ID.
🌐
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.
Find elsewhere
🌐
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.
🌐
Shopify Community
community.shopify.com › retired boards › appdev › custom storefronts
Create / Apply Discount through Storefront API
October 17, 2023 - Hello. I am reading the documentation on Storefront API and I’m incredibly confused. I want to apply a discount with a dynamic amount based on the items in my cart. How do I do this? Can I do this without creating a fixe…
🌐
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() {} }
🌐
Shopifyseoconsultant
shopifyseoconsultant.com › use-shopify-discount-code-api
Shopify Discount Code API: Discount Management Simplified
Sellers can use the API endpoints to generate unique codes with specific properties. This can include a discount percentage or fixed-amount discounts, free shipping, or buy-one-get-one offers.
🌐
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 Help Center
help.shopify.com › en › manual › shopify-flow › reference › actions › get-discount-data
Shopify Help Center | Get discount data
Emails a summary of active discounts that use a code. The workflow runs on a schedule every monday at 9am. The workflow can be customized to change the timing, how you receive the summary, the type of discounts you want to include, and mode.
🌐
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 ·
🌐
Beehexa
beehexa.com › devdocs › shopify-api-create-update-delete-a-discount-code-with-postman-in-shopify
Shopify API 2025 - Create, Update, and Delete A Discount Code With Postman In Shopify - Beehexa
April 15, 2025 - 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": ...