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
- Click Add Condition, Record or Action below any block.
- Select Code.
- Write your TypeScript in the code editor in the right panel.
- Optionally expand Advanced to add imports and dependencies.
Configuration
Main settings
| Field | Description |
|---|---|
| TypeScript Code | The code to execute. Return a value to expose the result to later blocks. |
Advanced settings
| Field | Description |
|---|---|
| Imports | Import statements required by your code (for example, import axios from "axios"). |
| Dependencies | NPM packages your code depends on (for example, axios, lodash, moment). |
| Dev Dependencies | NPM 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.
Related
- Variables — Store and combine values without custom code
- Conditions — Branch on code block output
- Logger — Verify code output during development