
# Code Actions

The `code` family runs custom TypeScript as an automation step for logic that the declarative actions don't cover. One operator, `runTypescript`. The source is a named `execute(context)` function whose body is **type-checked at server startup**.

| Prop        | Description                                                                                                                                                                      |
| ----------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `code`      | TypeScript source for a named `execute(context)` function. In TS configs, wrap with `String(function execute(context: CodeContext) { … })` for IDE autocompletion. **Required.** |
| `inputData` | Template-resolved key-value pairs passed as `context.inputData`.                                                                                                                 |
| `input`     | Template-resolved key-value pairs passed as `context.input` (same resolution rules as `inputData`).                                                                              |
| `packages`  | npm package specifiers with optional version pins (e.g. `lodash`, `date-fns@3.6.0`, `@scope/pkg@1.2.3`). Resolved/installed at app start or via `sovrium install`.               |
| `timeout`   | Execution timeout in ms (1000–300000, default 30000).                                                                                                                            |

## Code Context

The `execute` function receives a `context` object:

| Member              | Description                                                       |
| ------------------- | ----------------------------------------------------------------- |
| `context.trigger`   | The trigger payload.                                              |
| `context.steps`     | Outputs of prior steps, keyed by step name.                       |
| `context.inputData` | Template-resolved inputs from the `inputData` prop.               |
| `context.input`     | Template-resolved inputs from the `input` prop.                   |
| `context.env`       | Environment variables (sensitive values redacted in logs).        |
| `context.actions`   | Invoke native actions or reusable templates from within the code. |
| `context.packages`  | The declared npm packages, accessed by name.                      |
| `context.log`       | Structured logging: `info`, `warn`, `error`.                      |

```yaml
- name: enrich-lead
  type: code
  operator: runTypescript
  props:
    packages: [date-fns]
    inputData: { url: '{{trigger.data.profile_url}}' }
    code: |
      async function execute(context) {
        const res = await context.actions.http.get({ url: context.inputData.url })
        context.log.info('fetched', res.status)
        return { score: res.body.score, fetchedAt: new Date().toISOString() }
      }
```

:::callout
**Type-checked at startup, sandboxed at runtime.** The `execute` body is validated when the app boots, so a malformed function fails fast rather than mid-run. Declared `packages` are resolved ahead of time and exposed via `context.packages` — they are not fetched on demand inside the sandbox.
:::

## Related Pages

- [Actions Overview](/en/docs/automation-actions-overview) — invoking native actions (also available as `context.actions`) and reusable `ref` templates.
- [Environment Variables](/en/docs/automation-env-vars) — `context.env` and secret redaction.
- [HTTP & Webhooks](/en/docs/automation-http-actions) — the HTTP actions reachable from code.
