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.

Fields

The Run code action contains the following fields.

Fields used in the Run code action.
FieldDescription
InputRequired. A GraphQL query to use data from previous steps as input to the Run code action.
OutputRequired. A representation of the data to be returned by the action, defined in GraphQL's schema definition language (SDL).
CodeRequired. 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:

{
  order {
    note
    lineItems {
      title
    }
  }
}

This data is converted to an input variable that can be used in the code:

export default function main(input) {
  // input.order.note
  // input.order.lineItems[0].title
}

Inputs can also be destructured in the function signature:

export default function main({order}) {
  // order.note
  // order.lineItems[0].title
}

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:

{
  order {
    giftMessage {
      value
    }
  }
}

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:

type Output {
  "The message to include in the gift"
  giftMessage: String!
  "The total number of gifts"
  totalGifts: Int!
}

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:

export default function main(input) {
  // your code
  return {
    giftMessage: 'Hello',
    totalGifts: 1,
  };
}

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:

type Output {
  "The gift to send"
  gifts: [Gift!]!
}
type Gift {
  "The message to include in the gift"
  message: String!
  "The total number of gifts"
  amount: Int!
}

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:

export default function main(input) {
  console.log('Hello, world!');
  //Hello, world!
  console.log(input);
  // { order: { note: 'Hello', lineItems: [ { title: 'World' } ] } }
  console.log(input.order, "is the order");
  // { note: 'Hello', lineItems: [ { title: 'World' } ] }
  // is the order
  return {
    giftMessage: 'Hello',
    totalGifts: 1,
  };
}

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 or createdAt 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.

Roadmap for improvements to the Run code action
ImprovementDescriptionEstimated Delivery
LoggingUse `console.log` to output data to the Run log for troubleshooting purposes.Done.
External API callsUse 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.

Can’t find the answers you’re looking for? We’re here to help.