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. You don't need to handle paging or use edges and nodes in the query, as Flow handles those for you.

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
}

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.

Example

Examples for the code action can be found in the Flow examples repository.

Limitations

The Run code action has the following limitations:

  • Your code cannot import modules.
  • Your code cannot make http calls (fetch).
  • Your code cannot use console.log.
  • Random and clock-based functions cannot be used. Date data, such as a scheduledAt or createdAt can be passed in as an input.

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 is limited to 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
Bug fixesBug fixes and cleanupFebruary 2024
LoggingUse `console.log` to output data to the Run log for troubleshooting purposes.Early April 2024
External API callsUse JavaScript's fetch to call APIs.Q3 2024

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.

Ready to start selling with Shopify?Try it free