How to create Shopify Functions in 2027, step by step: episode 2 of the Shopify Functions 2027 series (API version 2027-01). We create our first Shopify Function in JavaScript - 10% off every product tagged sale-10 - and take it all the way to a real store checkout.

3 quick fixes from this episode

Create a Shopify Function in 60 Seconds (2027)

Create a Shopify Function in 60 seconds: generate a Discount extension, read product tags in the input query, return productDiscountsAdd, activate it - real 10% off at a real checkout.
Full episode: {TUTORIAL_URL}

Shopify #ShopifyFunctions #ShopifyDeveloper

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

Shopify Discount Not Working? Your Function Isn't Activated

Shopify discount not working after you deployed your discount Function? Deploying is not enough: create the discount with discountAutomaticAppCreate and your functionHandle. Real before/after: $72 with no discount, then 10% off at checkout.
Full episode: {TUTORIAL_URL}

Shopify #ShopifyFunctions #ShopifyDeveloper

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

shopify app function run: Test a Shopify Function Locally

shopify app function run tests your Shopify Function with a JSON cart input before you deploy: real input, real output and the instruction count. Blocked on Windows by Smart App Control? Run the same command in GitHub Actions.
Full episode: {TUTORIAL_URL}

Shopify #ShopifyFunctions #ShopifyDeveloper

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

Step by step

Intro: 10% off from our own Function (0:00)

This shirt was $48. At checkout it is forty-three twenty: 10% off. It comes from a few dozen lines of our own JavaScript, a Shopify Function, running inside Shopify's checkout.

In this episode you will learn how to create Shopify Functions step by step, and by the end you will have built this exact one. We generate the Function with the Shopify CLI. We write the input query and the code.

We run it with a real cart input, before it touches a store. And we deploy it, activate it, and watch it work in a real cart.

What we build (0:37)

Here is the plan. 10% off every product that has the tag sale ten. Products without the tag stay at full price, so the merchant controls the sale with nothing more than a product tag.

It is a Discount Function, and it runs on the cart lines target: cart lines discounts generate run. We write it in JavaScript, on Functions API version 2027, oh one. All the code is in the series repository, under the tag e p zero two.

Every Shopify Function you will ever build follows the same six steps. Generate the extension. Write the input query.

Write the code. Generate the types and run it locally. Deploy it to a store.

And then activate it. That last step is the one most people forget, and we will see exactly what happens when you do.

Generate the Function (1:33)

Step one. Inside our app folder, we run shopify app generate extension. Template discount, flavor vanilla J S, which means plain JavaScript, and the name first discount.

The CLI creates the folder, installs the dependencies and builds the GraphQL types. This is the real output from our run. One folder in extensions is one Function.

Our app already has an admin block extension and a test Function from episode one, so they all live side by side. Let's open it. shopify extension.toml describes the Function: its handle, the API version and the target.

The.graphql file is the input query, the data we ask Shopify for. And the.j s file is the Function itself. generated/api.t s holds the types, made by typegen.

And tests holds fixture files: an input, and the output we expect. We use those in a moment. The template also creates a second target for shipping discounts.

We removed it to keep this first Function small; shipping discounts are episode three.

The TOML file (2:47)

First line: the API version. The CLI template still writes 2026, oh seven, so we change it to 2027, oh one. The handle, first discount, is important.

It is how we will find this Function later, when we activate it. Then the targeting block. The target says when Shopify runs our code: when it calculates discounts for the cart lines.

input query points at our GraphQL file, and export is the name of the JavaScript function that Shopify calls. One more thing: after editing this file while shopify app dev is running, save it again so the dev server picks up the new version. Ours showed the old version until we did.

Input query (3:32)

Step two, the input query. This is the only data our Function will ever see, so we ask for exactly what the logic needs. For every cart line, the line I D, the quantity, and the merchandise.

When the merchandise is a product variant, we ask for its product. And here is the key field: has any tag, with sale ten. Shopify answers true or false for each product.

We never download the whole tag list. At the end we ask for the discount classes, which tell us whether the merchant allowed this discount to change product prices. There is a budget: an input query can cost at most thirty.

Has any tag costs three. Our whole query stays well under the limit.

The code (4:23)

Step three, the code. We import two enums from the generated types: discount class and the product discount selection strategy. Returning an empty list of operations means: no discount.

We will return that in several places. Check number one. If the merchant's discount does not include the product class, we stop right away.

A Function must never touch prices it was not allowed to touch. Notice there is no fetch and no database here. The input is everything.

Check number two. We filter the cart lines: keep only product variants whose product has the tag, and turn each one into a target, the cart line I D. One log line.

It counts how many lines matched. Logs are limited to one kilobyte, so keep them short. No tagged lines?

No discount. Otherwise we return one operation: product discounts add. One candidate with the message 10% off sale, our targets, and a 10% value.

Our code never changes the cart. It describes the discount, and Shopify applies it. That is the whole Function.

typegen, build and run (5:34)

Step four. shopify app function typegen reads the input query and writes the types. Every time you change the query, run typegen again, or your types are out of date.

build compiles the JavaScript to WebAssembly. And function run runs the WebAssembly on your own machine, with an input file we wrote: a cart with two lines. On this Windows PC, that last command failed.

This is the real error. Windows Smart App Control blocked function runner, the program the CLI uses to run Functions. The build tool was allowed, the runner was not.

Please do not switch off Smart App Control for this. Instead, we run the exact same command in GitHub Actions, on Ubuntu, every time we push. The workflow file is in the repository, so you can copy it.

And that gives us the real output. The input: two lines. A linen summer shirt where has any tag is true, and a canvas tote where it is false.

Our log line says: sale ten lines, one of two. And the output: one product discount, 10% off sale, targeting only cart line one, the shirt. The tote is not touched.

Exactly what we wanted, and we have not deployed anything yet. The runner also measures the Function. It used 284,000 instructions, out of a limit of eleven million.

The WebAssembly module is two kilobytes. Memory: one point two five megabytes, of ten allowed. And our three fixture tests pass: tagged and untagged lines, no tagged lines, and a discount without the product class.

Deploy with app dev (7:20)

Step five. shopify app dev builds the Function and puts a development version on our development store. Ready, watching for changes.

Every time we save the code, it rebuilds and redeploys. It also starts GraphiQL for the Admin API. We will need that in a second.

Now the Function is on the store. So the discount should work, right?

Deployed, not active (7:43)

Here is our development store. The linen summer shirt, $48, is tagged sale ten. We add it to the cart.

And a tee for $24, without the tag. The cart: $72, no discount. The Function is deployed, but nothing uses it yet.

Activate in GraphiQL (8:03)

Step six. In GraphiQL, the one shopify app dev started, we run one mutation: discount automatic app create. The variables: a title, our function handle, first discount, and the product discount class.

The handle is the one in our TOML file. No function ID to copy. Run it.

Status active, and the userErrors list is empty. This is the real response.

The real result (8:30)

Back in the store. In our recording, the cart page still showed $72, so the real test is checkout. Checkout.

The linen shirt: 10% off sale, minus $4 eighty. The tee has no tag, so our Function left it at $24. Total sixty-seven twenty.

We stop here: nothing is paid in this video. In the admin it is a normal automatic discount: sale 10%, type first discount, active. Merchants can schedule it, combine it or switch it off, without touching our code.

Common errors (9:08)

Common errors. Number one: function not found. This is the real response when I typed the handle wrong, frist instead of first.

The handle must match the TOML file exactly, the Function must be deployed, and it must belong to the same app that runs the mutation. You cannot activate another app's Function. Here are the others, all of which we hit or measured while building this one.

Deployed, but no discount: create the discount, as we just did. An old API version after editing the TOML: save it again while app dev runs. Type errors after changing the query: run typegen.

A rejected query: stay under the cost of thirty. And function run blocked on Windows: run it in continuous integration.

Recap (9:56)

Let's recap. You now know how to create Shopify Functions: generate, query, code, typegen and run, deploy, activate. The input query asks only for what you need, like has any tag, and the Function returns operations such as product discounts add.

Our real run used 284,000 of eleven million instructions, in a two kilobyte module. And a deployed discount Function does nothing until a discount uses it. The code is in the shopify functions 2027 repository, tag e p zero two, linked below.

In episode three we go deeper into discounts: product, order and shipping discounts in one Function. See you there.

📖 The full written tutorial with every episode: https://www.wixob.com/create-shopify-function/
📚 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.