Skip to main content

Code

Code blocks let you write custom TypeScript to perform complex transformations, calculations, or validations that are not possible with standard blocks. Open any automation in Automation from the top navigation and click Add Condition, Record or Action to add a code block.

Using a code block

  1. Click Add Condition, Record or Action below any block.
  2. Select Code.
  3. Write your TypeScript in the code editor in the right panel.
  4. Optionally expand Advanced to add imports and dependencies.

Configuration

Main settings

FieldDescription
TypeScript CodeThe code to execute. Return a value to expose the result to later blocks.

Advanced settings

FieldDescription
ImportsImport statements required by your code (for example, import axios from "axios").
DependenciesNPM packages your code depends on (for example, axios, lodash, moment).
Dev DependenciesNPM dev packages required (for example, @types/lodash, typescript).

Accessing data from previous blocks

Data from earlier blocks is available as variables using the same alias names. If your trigger alias is order and a Record block alias is customer, both are accessible directly in code.

Example — accessing trigger data:

const total = order.total;
const status = order.status;

return total > 1000 ? 0.1 : 0;

Example — accessing a queried record:

const isPremium = customer.tier === 'premium';
const baseDiscount = isPremium ? 0.15 : 0.05;

return order.total * baseDiscount;

Examples

Calculate a discount

const subtotal = order.subtotal;
const isPremium = customer.tier === 'premium';

let discount = 0;
if (subtotal > 500 && isPremium) {
discount = subtotal * 0.15;
} else if (subtotal > 500) {
discount = subtotal * 0.05;
}

return discount;

Format a string

const firstName = user.firstName || 'there';
return `Hi ${firstName}, your order #${order.id} is ready.`;

Use an external library

Imports:

import { format } from "date-fns"

Dependencies:

date-fns

Code:

return format(new Date(order.createdAt), 'MMM d, yyyy');

Validate data

const phone = user.phone || '';
const isValid = /^\+?[1-9]\d{7,14}$/.test(phone.replace(/\s/g, ''));
return isValid;

Use with a Condition block: If codeOutput = true.

Best practices

  • Return a value — Code blocks only expose data if you return something. If you don't return, the result is undefined.
  • Keep code focused — Do one computation per code block. Use multiple blocks for multiple operations.
  • Use a Logger before relying on output — Log the code result to verify values during development.
  • Declare dependencies — Any NPM package you import must also be listed in the Dependencies field.
  • Variables — Store and combine values without custom code
  • Conditions — Branch on code block output
  • Logger — Verify code output during development