Check if a Shopify discount code is valid from the storefront or Script Tag

If you're building a custom shopping cart page, you might want to add a discount code textbox which customers can use to enter a discount code before they check out. Depending on how "smart" you want the shopping cart to be, you may also want to validate that discount code first, so you can warn the customer if it's wrong.

This is something that I had to do for one of my clients, when I replaced a store's checkout with a custom draft order checkout process managed by an app. Since the draft order checkout doesn't have any place to enter discount codes, we had to let the customer enter it before they got to checkout. What we did was set up small <input type="text" name="discount" /> textbox inside the cart form, and then we set an onsubmit listener on the cart form itself. When submitted, we'd check if the discount code was valid before we let the customer start checkout.

Note: if you're doing this, it's important that the input's name is discount! Shopify will look for that when the cart form is submitted and automatically apply it to the order.

It took me a long time to figure out how to validate whether a discount code is valid or not from the storefront. It turns out that there's a special, completely undocumented (as far as I can tell) endpoint that you can call from the storefront -- i.e. without an access token or private app password. All you need is a little bit of JavaScript; if you've got an app, just create a Script Tag using the Shopify API, and if you're editing a theme, just create a <script> element somewhere on the cart page.

Here's what you need to do: use the fetch function to make a GET request to /admin/discount_codes/lookup.json?code=CODE_NAME. If the discount exists, you'll receive a 200 OK response. If it does not exist, you'll receive a 404. Just check the status code once the fetch is complete and you'll have your answer to whether the code exists or not:

 * Checks if a discount code exists.
async function doesDiscountCodeExist(discountCode) {
    const result = await fetch(`/admin/discount_codes/lookup.json?code=${discountCode}`);

    return result.status === 200;

console.log(await doesDiscountCodeExist("FOO")) // true
console.log(await doesDiscountCodeExist("BAR")); // false

Unfortunately, the only thing you can check is whether the code exists or not. 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. The fetch request automatically follows that redirect, and therefore, if you check the response URL when looking up a discount code, you'll have a URL you can send to your app to get the full discount code object.

 * Checks if a discount code exists, and returns its API URL if it does.
async function getDiscountCodeUrl(discountCode) {
    const result = await fetch(`/admin/discount_codes/lookup.json?code=${discountCode}`);

    if (result.status === 200) {
        return { ok: true, url: result.url };

    return { ok: false };

console.log(await getDiscountCodeUrl("FOO")); 
// { ok: true, url: "" }
console.log(await getDiscountCodeUrl("BAR"));
// { ok: false }

Just remember that getting the full discount code via the API requires the read_price_rules API permission, so if you're planning on using it, make sure your app has that permission first!

Learn how to build rock solid Shopify apps with C# and ASP.NET!

Did you enjoy this article? I wrote a premium course for C# and ASP.NET developers, and it's all about building rock-solid Shopify apps from day one.

Enter your email here and I'll send you a free sample from The Shopify Development Handbook. It'll help you get started with integrating your users' Shopify stores and charging them with the Shopify billing API.

We won't send you spam. Unsubscribe at any time.