Dev.to WebDev 🛠 Dev 👁 0 📖 9 min read

Shopify Create Discount Code API 2026: Codes, Automatic, Buy X Get Y & Free Shipping in Your App

Shopify create discount code API tutorial (2026): build a real discounts manager inside your Shopify app with the GraphQL Admin API 2026-10 - create discount codes and automatic discounts (percentage off, buy X get Y, fr

Shopify create discount code API tutorial (2026): build a real discounts manager inside your Shopify app with the GraphQL Admin API 2026-10 - create discount codes and automatic discounts (percentage off, buy X get Y, free shipping), list them, edit, deactivate and delete them, on a real development store. Shopify app development series, episode 6 - everything shown is real, nothing skipped.

What you'll learn

  • discountNodes: one list for codes AND automatic discounts (+ search: method, status)
  • discountCodeBasicCreate, discountCodeBxgyCreate, discountAutomaticFreeShippingCreate
  • Why context is now required ("Context can't be blank")
  • Why 20% is 0.2 (VALUE_OUTSIDE_RANGE)
  • Unique codes, edit, deactivate/activate, delete
  • Polaris web components

💻 Free source code for this episode: https://github.com/fayyaz315/shopify-app-development-2026/tree/ep06

3 quick fixes from this episode

Shopify Discount API: Why 20% Off Fails (Send 0.2)

Shopify discount API (GraphQL 2026-10): when you create a discount code, the percentage is a fraction from 0.0 to 1.0. I sent 20 for 20% and Shopify refused it with VALUE_OUTSIDE_RANGE - 0.2 works. Real error, then the working call.

▶ 60-second video: https://youtube.com/shorts/MJBLskYXHBw

Shopify GraphQL Discount Code: Context Can't Be Blank (2026 Fix)

Shopify GraphQL discount code (2026-10): discountCodeBasicCreate now needs context - who can use the code. Without it Shopify answers "Context can't be blank"; customerSelection is deprecated. Real error, then the fix.

▶ 60-second video: https://youtube.com/shorts/rOrKEpw-U-s

Shopify Create Discount API: Buy 2 Get 1 Free in One Call

Shopify create discount API (GraphQL 2026-10): a buy 2 get 1 free code from my own app with discountCodeBxgyCreate - customerBuys, customerGets with 100% off one item. Plus the one thing it refuses: 'all items'.

▶ 60-second video: https://youtube.com/shorts/riDFaSFiVrM

Step by step

Preview + intro (0:00)

In the next ten minutes, you'll create real Shopify discounts from inside your own app. A discount code, buy two get one free, and free shipping that applies by itself, plus the two errors that break discount code from older tutorials. This is the Shopify create discount code API, running inside our own app.

One click, and Black Friday Sale is a real, active code in the store. This is episode six of the Shopify app development series. Products, variants, collections and inventory are done.

Today: discounts. The full code is free on GitHub, and the link is in the description.

How Shopify models discounts (0:41)

First, how Shopify thinks about discounts. Every discount has a method and a type. The method is how it reaches the customer.

A discount code, which the customer types at checkout. Or automatic, which applies by itself when the cart qualifies. No code at all.

The type is what it gives. Amount off, as a percentage or a fixed amount. Buy X get Y, like buy two, get one free.

Free shipping, usually with a minimum order. And app discounts, where your own Shopify Function decides the logic. That's a different topic with its own videos on this channel.

Method times type gives the API its names: discountCodeBasicCreate, discountAutomaticFreeShippingCreate, and so on.

The discounts page (1:30)

Now our app. There's a new Discounts page: every discount in the store in one table, codes and automatic ones together. A new page is a new file.

The route file app.discounts becomes the URL /app/discounts. And the menu link is one line in the app layout. This episode needs one new permission: write discounts, in shopify.app.toml.

Write includes read. Every request takes the same path as before: the page asks the loader, buttons call the action, both talk to the Admin API.

Read: discountNodes (2:07)

Reading first. One query for every kind of discount: discountNodes. Older code used codeDiscountNodes and automaticDiscountNodes.

Both are deprecated in 2026-10. Each node wraps the real discount. Its type name tells you which one it is, like discount code basic.

That's why the query has one fragment per type: title, status and summary, plus the first code for code discounts. sortKey createdAt with reverse true puts the newest discount first. That's why new ones appear at the top.

The summary is Shopify's own sentence, like 20% off one time purchase products. We show it under the title, for free. Status is active, scheduled or expired.

A start date in the future gives scheduled. I tested that too. The loader turns each node into a simple row.

Automatic or code comes from the ID, and the type from the type name. The show select filters the list. Codes: only the discount codes.

Automatic: four discounts. Three of them were made by another app, the Kaching bundles app, so they have no buttons. There's no special code for that.

Each option is just discountNodes search syntax: method:code, or status:active. App discounts are owned by the app that made them. Our app can list them, but editing belongs to that app's Function.

The 2026 rules (3:38)

Before we create anything, here's the same list in GraphiQL, filtered to codes. Four discount code basic results, each with title, status and summary. Exactly what the table shows.

Now discountCodeBasicCreate, the way many tutorials still write it: a title, a code, a start date and 10% off. Refused. Context can't be blank.

Shopify now wants to know who can use the code. Context replaces customerSelection, which is deprecated. For everyone, it's simply context, all, all.

So we add context, and while we're here, I write ten for 10%. That looks reasonable. Refused again.

Value must be between zero and one. Shopify's percentage is a fraction. 10% is 0.1.

Same request, with 0.1. Created. Welcome ten is active, and the summary says 10% off one time purchase products.

Those are the three rules for codes in 2026: context, a fraction for the percentage, and a unique code. One more useful query: find a discount by its code. I type it in lower case on purpose.

Found anyway. Codes don't care about upper or lower case, and that matters in a minute.

Create a discount code (4:58)

Back in the app. Create discount opens one form for everything: method, type, title and code. Black Friday Sale, code BLACKFRIDAY, 20%.

Create, and it's in the list. Six combinations means six create mutations. Instead of six copies, one table maps method and type to the mutation name, argument and input type.

And one small function builds the mutation text from that table. One lesson from episode four: keep that table as plain data. Functions are only called for the one combination you need.

Input for builds what Shopify expects from the form. For codes: the code, and context, all. For percentage off: the form says twenty, so we divide by a hundred.

That's the 0.2 Shopify wants. Items all means every product. You could also pass products or collections here.

Shopify also sets the discount class from the items. 20% off everything came back as an order discount, a product list as a product discount. And the create case adds a start date of now.

Without starts at, Shopify can't make it active.

Codes must be unique (6:10)

Now a mistake on purpose. A second discount, with the same code in lower case. Code must be unique.

Please try a different code. Shopify compares codes ignoring case, like we saw in GraphiQL. In the action, every mutation returns userErrors.

If there are any, we send them to the page instead of a success message. And the page shows Shopify's own message in a red toast. No guessing, no generic something went wrong.

After a success, a toast, the modal closes, and React Router reloads the list by itself.

Buy X get Y (6:50)

Next, buy X get Y. Same form, type buy X get Y, and the fields change: a product, how many they buy, and how many they get free. Buy two tees, get one free.

Code TEE3, the everyday crew neck tee, buys two, gets one. Created. Shopify's summary: buy two items, get one item free.

The input has two halves. Customer buys: a quantity of two, of this product. Customer gets: one item, with an effect of 100% off.

That's what free means to the API. I tested one shortcut: customer gets all items. Shopify refuses it.

Buy X get Y always needs real products or collections. The product list in the form comes from the same loader query: our ten demo products, sorted by title.

Automatic free shipping (7:41)

Now an automatic one. Method automatic, type free shipping. The code field disappears.

Free shipping over $75, with a minimum purchase of seventy five. Created. Free shipping on all products, minimum purchase $75, for all countries.

No code to type. Free shipping needs a destination. Ours is all countries.

And the minimum becomes a minimumRequirement with a subtotal. I tried a minimum subtotal and a minimum quantity together. Conflict: pick one.

And automatic discounts don't need context. That rule is only for codes. In the form, the method and type selects control which fields appear.

Polaris selects need the change event hook from episode four.

Edit, deactivate, delete (8:31)

Edit Black Friday. The form opens with the current twenty, and I make it twenty five. Saved.

The summary now says 25% off. Update takes the same input as create. You only send the fields you change, and Shopify keeps the rest.

Notice what an update doesn't send: no code, no context, no destination. Those stay exactly as they were. The current percentage comes back as a fraction too, so the loader multiplies by a hundred for the form.

Deactivate. The badge turns to expired, and the button becomes activate. Activate, and it's active again.

On the page, an expired row gets activate instead of deactivate. Everything else stays the same. There's no paused state.

Deactivate simply sets the end date to now, so the status becomes expired. Activate removes the end date. And delete the buy two get one.

Discount deleted. It's gone from the store. Codes and automatic discounts have separate activate, deactivate and delete mutations.

The action picks by method. I tested the wrong one: an automatic delete with a code ID. Shopify just says invalid ID.

Honest notes (9:41)

A few honest notes before the recap. One: I load twenty five discounts. A busy store needs pagination, like the products page in episode two.

Two: the form covers the common cases. The API also has fixed amounts, customer segments, markets, usage limits and end dates. Three: when the built-in types aren't enough, like tiered pricing, that's Shopify Functions.

Linked in the description. Want to practise? Add an end date field, or a usage limit, so a code can only be used a hundred times.

And everything you saw was tested on my real development store on API version 2026-10, before I pressed record.

Recap (10:26)

So, the recap. Read every discount with discountNodes, and filter with search syntax. Create with one mutation per method and type.

For codes, always send context, and percentages from zero to one. Update with the same input, and end a discount with deactivate or delete. That's the Shopify create discount code API in 2026, inside a real app, on a real store.

The full code for this episode is free on GitHub. The link is in the description. Next episode: metafields.

Your own fields on products, with definitions and values. See you there.

📖 The full written tutorial with every episode: https://www.wixob.com/shopify-create-discount-code-api-tutorial/
📚 Course page: https://www.wixob.com/shopify-app-development-2026/
🔔 New episodes on YouTube: https://www.youtube.com/@fayyaz-ahmed-shopify-developer?sub_confirmation=1

Questions about the Shopify GraphQL Admin API? Ask in the comments — I answer every one.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.