Using transform-data steps

Transform-data steps manipulate data using JavaScript in a sandboxed environment, letting you format, filter, merge, or calculate values within Flows.

Add a transform step

  1. In the Flow editor, click Add Step
  2. Select Run Code
  3. Configure:
    • Name: Descriptive label (e.g., “Format response”)
    • Script: Write your JavaScript transformation logic
    • Output Variable: Name for the result variable
  4. Click Save Step

Writing scripts

Write JavaScript that returns a value. All Flow variables from previous steps are available directly by their outputVariable name:

1// Access a variable called "customer_data" from a prior step
2const formatted = {
3 name: customer_data.name.toUpperCase(),
4 email: customer_data.email.toLowerCase(),
5 timestamp: new Date().toISOString()
6};
7
8return formatted;

Reference output from earlier steps by their outputVariable name:

1const summary = generate_summary; // outputVariable from a prior prompt step
2const wordCount = summary.split(' ').length;
3
4return {
5 summary: summary,
6 wordCount: wordCount,
7 readingTime: Math.ceil(wordCount / 200) + ' minutes'
8};

Common transformations

Array operations

1// Filter and map array — "order_data" is an outputVariable from a prior step
2const highValueOrders = order_data.orders
3 .filter(order => order.amount > 1000)
4 .map(order => ({
5 id: order.id,
6 total: order.amount,
7 formattedTotal: '$' + order.amount.toFixed(2)
8 }));
9
10return highValueOrders;

Object merging

1// Combine data from multiple sources — reference outputVariable names directly
2return {
3 ...customer_data,
4 ...order_history,
5 enrichedAt: new Date().toISOString()
6};

String formatting

1// "contact_info" is an outputVariable from a prior step
2const name = contact_info.firstName + ' ' + contact_info.lastName;
3const slug = name.toLowerCase().replace(/\s+/g, '-');
4
5return { name, slug };

Calculations

1// "cart" is an outputVariable from a prior step
2const items = cart.items;
3const subtotal = items.reduce((sum, item) => sum + item.price, 0);
4const tax = subtotal * 0.08;
5const total = subtotal + tax;
6
7return { subtotal, tax, total };

Available JavaScript features

Transform steps run in a sandboxed environment (Cloudflare Worker by default) and support modern JavaScript:

  • Array methods (map, filter, reduce, etc.)
  • Destructuring and spread operators
  • Arrow functions
  • Template literals
  • async/await
  • Date objects
  • Math functions
  • JSON.parse and JSON.stringify
  • Built-in helper utilities (available under helpers.*)

External libraries are not available in the default sandbox. Use API call steps for external data.

The default step timeout is 5 minutes. Keep transformations focused to avoid unnecessary execution time.

Error handling

By default, if your script throws, the step does not fail the Flow. It continues with the step’s configured default value (or null if none is set) for the output variable, and records the error on the step result. Set the step’s error handling to Stop on error if a failed transformation should halt the Flow instead.

To keep going with a meaningful value rather than null, wrap risky operations in try/catch and return a structured result:

1try {
2 const parsed = JSON.parse(raw_data);
3 return { success: true, data: parsed };
4} catch (error) {
5 return { success: false, error: error.message };
6}

A downstream Conditional Logic step can then branch on success.

Testing transformations

You can test a single step against the variables from the most recent run, but only after the Flow has run at least once:

  1. Run the Flow once
  2. Open the step’s action menu (the button on the step card)
  3. Choose Test with last run
  4. Review the output and iterate

Best practices

  • Keep it simple: Complex logic belongs in external services
  • Name clearly: Use descriptive step names that explain the transformation
  • Handle nulls: Check for undefined/null values before accessing properties
  • Return consistent types: Always return the same structure for predictability

Next steps