Shopify Functions overview
Shopify Functions allow developers to customize the backend logic of Shopify. This guide introduces how Shopify Functions work and the benefits of using Shopify Functions.
How Shopify Functions work
Anchor link to section titled "How Shopify Functions work"Like a typical function you define an input, write the logic of your function, and then write out the output. Let’s dissect how each of these work:
- Input: The function input is a subset of the Cart API and is written as a GraphQL query. Functions can also query metafields so you can store necessary data and retrieve it.
- Function logic: The function logic can be written in any language that can compile a WebAssembly module which meets function requirements. Function templates and client libraries are available for JavaScript and Rust.
- Output: The function output is a payload that conforms to a GraphQL schema relevant to the specific domain. You can learn more about specific schemas in the Functions API reference.
Lifecycle of a Shopify Function
Anchor link to section titled "Lifecycle of a Shopify Function"The following diagram shows an example lifecycle of a Shopify Function:
App developers create and deploy apps that contain functions.
Merchants install the app on their Shopify store and configure the function. An API call is made with the function configuration.
Customers interact with a Shopify store and Shopify executes the function.
For example, an app developer might create and deploy an app with a function that defines a new discount type. The merchant can then install the app on their Shopify store and create a new discount from a discount type provided by the app. Shopify executes the function to calculate the discount when a customer adds a product to their cart.
Shopify Functions are never invoked directly by URL or otherwise. Shopify invokes them as-needed within the customer journey.
Getting started
Anchor link to section titled "Getting started"Learn how to use Shopify Functions by following one of our use case tutorials:
Developer tools and resources
Anchor link to section titled "Developer tools and resources"Explore the developer tools and resources available for Shopify Functions.
Limitations and considerations
Anchor link to section titled "Limitations and considerations"The following limitations and considerations apply to all functions:
- Apps can reference only their own functions in GraphQL Admin API mutations, such as
discountAutomaticAppCreate
andcartTransformCreate
. Referencing a function from another app results in aFunction not found
error. - Shopify doesn't allow nondeterminism in functions, which means that you can't use any randomizing or clock functionality in your functions.
- The compiled function can't exceed 256 kB in size.
- You can't debug your function by printing out
STDOUT
, which is reserved for function output. UseSTDERR
instead. Function logs to
STDERR
are truncated after 1 kB. Some Wasm toolchains might crash ifSTDERR
fails to write full logs.The logic of the function must execute in a maximum of 11 million instructions, which can be tested locally using
function-runner
.At runtime, a function can't exceed the following memory limits:
- Linear memory limit: 10 MB
- Stack memory limit: 512 kB
A function's input cannot exceed 64,000 bytes in size.
Refer to input query limitations for more information.
A function's output can't exceed 20 kB in size.
An app can deploy a maximum of 50 functions.
Network access isn't supported. For example, you can't send HTTP requests to your app's backend as part of the function's logic. However, you can pre-populate data by using metafields on products and customers, or passing data using cart attributes.
- Learn about how data is input to and output from Shopify Functions.
- Explore the references for each Function API.