> For the complete documentation index, see [llms.txt](https://docs.baas.sh/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.baas.sh/baas-console/automations/actions/function.md).

# Function

Run your own JavaScript in a sandbox as a step of the pipeline.

The Function action runs the JavaScript you write; whatever it returns becomes `context.previous` for the next step. In this guide, it joins the `Transfer alerts` automation from the [Monitor](/baas-console/automations/triggers/monitor.md) page, shortening the transaction hash so the [notification](/baas-console/automations/actions/notification.md) can show it in one line.

## Write the function

{% stepper %}
{% step %}

### Pick the Function type

[Add a step](/baas-console/automations/actions.md) to the pipeline and pick **Function**.
{% endstep %}

{% step %}

### Write the code

**JavaScript code** takes a function of the form `async (trigger, context, plugins) => result`. Keep the three parameter names exactly as written. A synchronous function works too. The code can hold up to 10 240 characters.

In our example, it shortens the transaction hash:

```js
async (trigger, context, plugins) => {
  const hash = trigger.event.raw.transactionHash;
  return { shortHash: `${hash.slice(0, 10)}…${hash.slice(-8)}` };
}
```

The paths match the ones your templates use, without the braces: `{{trigger.event.raw.transactionHash}}` in a field is `trigger.event.raw.transactionHash` in code.
{% endstep %}

{% step %}

### Place it in the pipeline

Order matters, and a new action lands at the end of the pipeline: drag it to where its result is needed. In our example, the function goes above the notification, so the short hash is ready when the message goes out.
{% endstep %}

{% step %}

### Save

Click **Save**. The code now runs on every firing.
{% endstep %}
{% endstepper %}

## Advanced settings

The **Advanced** section holds two settings.

| Setting            | What it does                                                                                                         |
| ------------------ | -------------------------------------------------------------------------------------------------------------------- |
| **Key (optional)** | The key, then one of the networks it is set up on, that `plugins.vault.signMessage` signs with. Pick both or neither |
| **Timeout (ms)**   | The code's time budget, from 100 to 120000; when empty, uses the project's Function default                          |

## Limits of the sandbox

* The code runs isolated: no imports, no `require`, and no direct network access.
* The [`plugins` argument](/baas-console/automations/reference/plugins.md) lets your code call an API, send an email or a push, and sign messages with a key from your Vault.
* A `throw` fails the action.
* `console.log`, `console.warn`, and `console.error` appear in the run's [logs](/baas-console/automations/runs-and-logs.md), marked `console`. The budget is 1000 messages or 256 KiB of text per action attempt, shared across batch users; further messages are suppressed with a warning.
* In [batch mode](/baas-console/automations/batch.md), `context.batch` holds the current user and their position.

## What the next step receives

The encoded return value, subject to the [result limits](/baas-console/automations/reference/context.md); return nothing and the next step gets `null`. In our example, the next step sees:

```json
{ "shortHash": "0xabc12345…9f8e7d6c" }
```

and the notification reads `{{context.previous.shortHash}}` in place of the full hash.

## Next

* [Notification](/baas-console/automations/actions/notification.md): show the short hash in the alert.
* [Runs and logs](/baas-console/automations/runs-and-logs.md): follow each run's outcome.
* [Actions](/baas-console/automations/actions.md): back to the section overview.
