Run code
The Run code action executes JavaScript. You can pass data from previous steps in the workflow as input to the Run code action, and return values to be used in subsequent steps.
On this page
Fields
The Run code action contains the following fields.
Field | Description |
---|---|
Input | Required. A GraphQL query to use data from previous steps as input to the Run code action. |
Output | Required. A representation of the data to be returned by the action, defined in GraphQL's schema definition language (SDL). |
Code | Required. The JavaScript that the Run code action will execute. |
Input data
Input data can be passed from steps that happen before the Run code action. To include this data, you can write a GraphQL query in the Input field. The data returned from the query will be available as the input
argument to the function marked with export default
, called main
by convention.
The input query is a query for Flow environment data not a query to the Shopify Admin API. Therefore you can't insert a Shopify query in the input data. Additionally, Flow handles the edges
and nodes
in the query, so you don't need to add those or other paging syntax to your query.
An example input that gets an order note and the title of a line item:
This data is converted to an input
variable that can be used in the code:
Inputs can also be destructured in the function signature:
Metafields
To access single metafield values in Run code, you must first add the metafield to Flow's environment data. This be done in any action or condition other than Run code. For example, you can add a Log output action and then follow the instructions to add the metafield.
After a metafield has been added to the workflow environment, you can access it in Run code. For example, suppose that you added an Order metafield with the alias name of giftMessage
. You can then access it in your input query:
Output data
The Run code action can return custom data. To define the type of the data that the code will return, use the Output field and GraphQL's Schema definition language (SDL). For example, to return a string called giftMessage
and a number called totalGifts
:
The comments are optional, but will be used to describe the data in the Flow UI. To output this data in the JavaScript code, return an object matching the type:
You can also define a custom type to return more complex data. For example, to return a type called Gift
that contains a string called message
and a number called amount
:
To access this data in steps that follow this action, use the variable named Run code, which will be typed according to the Output
schema you define in the Run code action configuration. You can use this variable in both conditions and actions.
Console.log
You can use console.log
to output data to Flow's workflow run log for troubleshooting purposes. The output will be visible in the Run log for the workflow. For example the following are valid:
You cannot use console.info
, console.error
, or other functions in the Run code action.
Example
Examples for the code action can be found in the Flow examples repository.
Limitations
The Run code action has the following limitations:
- The Run code action supports ECMA2020 JavaScript. It does not support the NodeJS or CommonJS APIs, or importing modules.
- Your code cannot make http calls (
fetch
). - Random and clock-based functions cannot be used. Date data, such as a
scheduledAt
orcreatedAt
can be passed in as an input. - Console.log does not log to the browser console.
- You cannot add metafield alias directly in the Run code action. View the metafields section for how to accomplish this.
In addition, the following limits are enforced:
- Input data query is limited to 5000 characters.
- Output data schema is limited to 5000 characters.
- Output data payload and Console.log output are limited to a combined 50kb.
- Code cannot be longer than 50000 characters.
- Total execution duration is limited to 5 seconds.
- Memory usage is limited to 10MB.
Roadmap
The Flow team plans to add capabilities to the Run code action over time. The following table outlines the planned improvements and their estimated delivery dates.
Improvement | Description | Estimated Delivery |
---|---|---|
Logging | Use `console.log` to output data to the Run log for troubleshooting purposes. | Done. |
External API calls | Use JavaScript's fetch to call APIs. | Q1 2025 |
Feedback
The Run code action is a new type of step in Shopify Flow. If you have feedback and questions, then please comment on this Flow community post.