# Script steps

> Run a short piece of JavaScript in a playbook to compute, transform or format data, and use what it returns in later steps.

A **Script** step runs a short piece of JavaScript when a signal reaches it, for what templates and conditions can't express: a score, a total, a date thirty days out, a value cleaned up, a message put together from several fields. What it returns is available to later steps.

Add one by picking **Script** when adding a step to a monitor's playbook.

## Writing a script

The code is the body of an `async` function. It gets `input` and returns what later steps need:

```js
const amount = Number(input.signal.amount_eur);
console.log("amount", amount);
return { refund: amount, priority: amount > 500 ? "high" : "low" };
```

| `input.` | Holds |
|---|---|
| `signal` | The signal's data: the columns of the row, or the fields of the webhook, that raised it. |
| `steps` | What earlier steps with a **System Name** exposed, such as `steps.approval.response` or `steps.score.priority`. |
| `vars` | The account's [variables](https://pulso.run/docs/concepts/variables-and-address-book.md#variables). |
| `meta` | The signal's `id`, `title`, `priority`, `status`, `created_at` and `monitor` (`id`, `name`). |

Values from a database or a webhook often arrive as text: turn them into numbers with `Number(…)` before doing arithmetic, and compare dates as `new Date(…)`.

`console.log`, `info`, `warn` and `error` lines are kept with the run and shown on the signal. `await` works, for example on a `Promise` you build.

## Using what it returns

Give the step a **System Name**, such as `score`. Then:

- Each key of the returned object is available as `{{score.key}}` in later steps: a comment, a Slack message, an email, a question's text.
- A **Branch on data** step can test it, as the column `score.key`.
- A later script gets it as `input.steps.score`.
- Any other value, like a number or a string, is available as `{{score.result}}`.

To post what a script worked out as a comment, return the text ready to post and add a **Comment** step after it:

```js
const amount = Number(input.signal.amount_eur);
const level = amount > 500 ? "high" : "low";
return {
  level,
  comment: `Refund of ${amount.toFixed(2)} € for ${input.meta.title}: ${level} priority.`
};
```

```
{{score.comment}}
```

Return text, numbers or booleans for what goes into templates. An object or a list put whole into a template, like `{{score}}`, doesn't come out as JSON.

## Examples

A due date 30 days after the signal:

```js
const due = new Date(input.meta.created_at);
due.setUTCDate(due.getUTCDate() + 30);
return { due_on: due.toISOString().slice(0, 10) };
```

An order number out of a free-text reference:

```js
const match = String(input.signal.reference ?? "").match(/ORD-(\d+)/);
return { order_id: match ? match[1] : null };
```

A total over a list in the signal's data:

```js
const lines = input.signal.lines ?? [];
const total = lines.reduce((sum, line) => sum + Number(line.amount), 0);
return { total: total.toFixed(2), count: lines.length };
```

Who to page, from an answer and a variable:

```js
const urgent = input.steps.triage?.response === "Yes";
return { notify: urgent ? input.vars.oncall_email : input.vars.team_email };
```

## Trying it

**Run with a sample signal** runs the code as written against the monitor's latest signal (or its fields, before it has any), and shows what it returned, its logs and how long it took. **Edit the input** lets you change that input and run it again. Earlier steps' values are empty in a trial run, so fill `steps` in by hand to try code that reads them. Nothing is stored, and the returned keys are offered as tags in later steps.

Through the [API](https://pulso.run/docs/api/monitors.md) or [MCP](https://pulso.run/docs/api/mcp.md), `run_script` does the same, so an assistant can draft a script and try it before someone adds it to a playbook.

## When it fails

A script fails when it throws, runs too long, uses too much memory or returns something that can't be turned into JSON. The step's **If it fails** setting decides what happens:

- **Stop the playbook here**: the step stays open with the error. **Run again** runs it once more, with the code the signal got; **Continue without it** goes on. A run that hasn't finished after 10 minutes, because its worker was stopped, offers the same. Through the API, `retry_task` and `complete_task` do the same.
- **Note the error and go on**: later steps run, and get the error as `{{score.error}}`.

On the signal, the step shows the error, the logs up to it and how long it ran. A syntax error names its line.

## Limits

Scripts run in an isolated sandbox on Pulso's servers:

| | |
|---|---|
| Time | About a second of CPU. Waiting doesn't count, so there are no timers. |
| Memory | 64 MB. |
| Code | 20 KB. |
| Returned value | 64 KB, as JSON. |
| Logs | 100 lines, 16 KB. |

The script has no network, files, environment or clock to sleep on: `fetch`, `require`, `setTimeout` and Node's modules aren't there. It is plain JavaScript (ES2023), with `JSON`, `Math`, `Date` and regular expressions, but without `Intl`, so `toLocaleString` doesn't format numbers or dates for a language: use `toFixed` and build the text yourself. To call an API, add an [HTTP request step](https://pulso.run/docs/concepts/playbooks.md#http-requests) after the script.

Each save of a monitor's playbook is in the monitor's **Audits** tab with the whole playbook, so every version of a script's code, and who saved it, is kept.
