Using transform-data steps

Use a transform-data step to run code that formats, filters, combines, or calculates Flow data. The Flow editor labels this step Run Code.

Before you begin

Open a Flow in the Flow editor. For instructions, see Creating and editing flows.

Add a transform-data step

To add and configure a transform-data step, follow these steps:

  1. In the Flow editor, click the + control where you want to insert the step.
  2. In the step picker, select Run Code. The step card opens.
  3. In Execution environment, choose the environment that matches your code. The default Cloudflare Worker (Default) environment runs JavaScript.
  4. In Input mode, select Code (Write your own logic).
  5. Select a code template or click Write code to open the editor.
  6. In the code editor, write a script that returns the value that you want to store.
  7. In Output variable, enter the name for the returned value.
  8. In the toolbar, click Create for a new Flow or Save for an existing Flow.

Optional: Rename the step in its header to describe its transformation.

Choose an execution environment

The Flow editor provides these execution environments:

  • Cloudflare Worker (Default): Runs JavaScript with async/await, exposes built-in helpers under helpers.*, and blocks network access by default.
  • Runtype Sandbox: Runs JavaScript, TypeScript, and Python in a container. It supports package installation and sandbox persistence.
  • Daytona Sandbox: Runs JavaScript, TypeScript, and Python in a container. It supports package installation and sandbox persistence.
  • QuickJS (Legacy): Runs JavaScript synchronously and exposes built-in helpers as global functions.

To run TypeScript or Python, choose Runtype Sandbox or Daytona Sandbox, then set Language to the language that you want to use.

To install packages in a container environment, enter the package metadata in Package.json (Optional). For HTTP requests that need step-level request and response configuration, use the fetch-url and api-call steps.

Write a script

Write JavaScript that returns a value. The runtime makes earlier step outputs available by their outputVariable names. Valid, non-reserved JavaScript variable names are available at the top level, and the complete variable map is available through input.

Use the following script to format a customer object:

1const formatted = {
2 name: customer_data.name.toUpperCase(),
3 email: customer_data.email.toLowerCase(),
4 timestamp: new Date().toISOString(),
5}
6
7return formatted

Replace customer_data with the earlier step’s outputVariable name.

Reference output from an earlier step by its outputVariable name. The following script counts words in a generated summary:

1const summary = generate_summary
2const trimmedSummary = summary.trim()
3const wordCount = trimmedSummary ? trimmedSummary.split(/\s+/).length : 0
4
5return {
6 summary,
7 wordCount,
8 readingTime: `${Math.ceil(wordCount / 200)} minutes`,
9}

Replace generate_summary with the earlier step’s outputVariable name.

Common transformations

Use the following patterns to transform arrays, objects, strings, and numbers.

Filter and map arrays

Filter an array and create a new object for each remaining item:

1const highValueOrders = input.order_data.orders
2 .filter((order) => order.amount > 1000)
3 .map((order) => ({
4 id: order.id,
5 total: order.amount,
6 formattedTotal: '$' + order.amount.toFixed(2),
7 }))
8
9return highValueOrders

Replace order_data with the variable that contains the order list.

Merge objects

Combine values from multiple sources into one object:

1return {
2 ...input.customer_data,
3 ...input.order_history,
4 enrichedAt: new Date().toISOString(),
5}

Replace customer_data and order_history with the variables that you want to merge.

Format strings

Create a name and a URL-friendly slug from contact information:

1const name = `${input.contact_info.firstName} ${input.contact_info.lastName}`
2const slug = name.toLowerCase().replace(/\s+/g, '-')
3
4return { name, slug }

Replace contact_info with the variable that contains the contact information.

Calculate values

Calculate a cart subtotal, tax, and total:

1const items = input.cart.items
2const subtotal = items.reduce((sum, item) => sum + item.price, 0)
3const tax = subtotal * 0.08
4const total = subtotal + tax
5
6return { subtotal, tax, total }

Replace cart with the variable that contains the cart items.

Use available JavaScript features

JavaScript transform scripts support these features:

  • Array methods such as map, filter, and reduce.
  • Destructuring and spread operators.
  • Arrow functions.
  • Template literals.
  • async/await in environments that support asynchronous execution.
  • Date objects.
  • Math functions.
  • JSON.parse and JSON.stringify.
  • Built-in helper functions in environments that provide them.

External libraries are unavailable in Cloudflare Worker (Default) and QuickJS (Legacy). Choose Runtype Sandbox or Daytona Sandbox when your script needs packages.

Handle errors

Transform-data steps continue after a script error by default. The step stores its configured default value in the output variable, or null when no default exists, and records the error on the step result. To stop the Flow, choose Stop on error in the step’s error-handling control.

Wrap operations that can throw in try/catch when you want to return a structured result:

1try {
2 const parsed = JSON.parse(input.raw_data)
3 return { success: true, data: parsed }
4} catch (error) {
5 return {
6 success: false,
7 error: error instanceof Error ? error.message : String(error),
8 }
9}

Replace raw_data with the variable that contains the JSON string. Use a downstream Conditional Logic step to branch on success.

Test a transformation

To test a top-level transform-data step against variables from the last run, follow these steps after the Flow has run once:

  1. Run the Flow once.
  2. On the top-level step card, click the action menu ().
  3. Choose Test with last run. The test sheet opens.
  4. Review the output and update the script if needed.

Follow best practices

Use these practices when you write transform-data scripts:

  • Keep each transformation focused.
  • Name each step and output variable to describe its result.
  • Check for null and undefined before you access properties.
  • Return the same structure from each successful run.

Next steps

Continue with these guides: