# Run code

> Runs a short JavaScript you write on the data so far: reshape it, calculate, or call one of your API connections. Runs in an isolated sandbox.

Source: https://docs.go-lumen.ai/de/reference/steps/run-code

## Auf einen Blick

| Eigenschaft | Wert |
| --- | --- |
| **Schrittname** | `run-code` |
| **Ressource** | ClusterStep (`lumen.ai/v1alpha1`) |
| **Palettengruppe, Kategorie** | control, logic |
| **Credits** | **1** Credit bei jedem Erfolg |
| **Externe Wirkung** | ja: Er ändert etwas außerhalb von Lumen, deshalb fragt ein Wiederholungsversuch vorher nach, und ein Test zeigt, was er tun würde, statt es zu tun. |
| **Die Installation muss bereitstellen** | nichts |

## Eingaben

| Eigenschaft | Wert |
| --- | --- |
| **Braucht** | nichts |
| **Liefert** | `fields` |
| **Arbeitet auf** | item: einem Dokument nach dem anderen |

Braucht und Liefert sind Datentypen (file, text, fields, email). Der Editor bietet einen Schritt nur dort an, wo das, was er braucht, schon auf dem Pfad liegt. Siehe [Dokumente und Daten](https://docs.go-lumen.ai/de/concepts/how-flows-run.md#documents-and-data).

## Ausgaben

Ein späterer Knoten verweist auf eine Ausgabe als `{nodeId.field}` in einem Text oder als `nodeId.field` in einer Bedingung.

| Ausgabe | Typ |
| --- | --- |
| `result` | string |

More outputs are declared in the setting `outputs`.

## Einstellungen

Wird unter `config:` im Knoten des Flows gesetzt. Texteinstellungen dürfen Platzhalter wie `{nodeId.field}` enthalten.

| Einstellung | Typ | Standardwert | Beschreibung |
| --- | --- | --- | --- |
| `code` (erforderlich) | string |  | Code is the body of an async JavaScript function of (input, lumen, console); what it returns is the result. (minLength 1; maxLength 65536) |
| `inputs` | map of string |  | Inputs are the input's properties: a name, and text with `{<nodeId>.<field>}` placeholders. A value that is just one placeholder keeps the field's type. None passes every field so far. |
| `outputs` | list of object |  | Outputs are the fields later steps can pick. (maxItems 50) |
| `outputs[].name` (erforderlich) | string |  | Name (minLength 1; maxLength 64; pattern \`^\[A-Za-z\]\[A-Za-z0-9\_\]\*$\`) |
| `outputs[].type` (erforderlich) | string |  | Type (one of string, number, boolean, date, money) |
| `connections` | list of string |  | Connections are the HTTP connections lumen.fetch may call. (maxItems 10) |
| `methods` | list of string |  | Methods are the methods lumen.fetch may use; none is GET and HEAD. |
| `timeoutSeconds` | integer |  | TimeoutSeconds caps one run; zero is 5. (minimum 1; maximum 30) |

Das vollständige JSON Schema dieser Einstellungen: [run-code.schema.json](https://docs.go-lumen.ai/schemas/steps/run-code.schema.json).

## Beispiel

```yaml title="config"
code: |-
  const lines = JSON.parse(input.lines)
  const total = lines.reduce((sum, l) => sum + l.qty * l.price, 0)
  console.log('lines', lines.length)
  return { total, withVat: Math.round(total * 1.18 * 100) / 100 }
inputs:
  lines: '{read.lines}'
outputs:
- name: total
  type: number
- name: withVat
  type: number
```

## Im Flow verwenden

Ein Knoten, der `run-code` ausführt, steht unter `spec.nodes` eines [Flow](https://docs.go-lumen.ai/de/concepts/how-flows-run.md#the-flow-as-yaml). Geben Sie ihm eine beliebige id und nennen Sie in next die ids der nächsten Knoten.

```yaml title="flow.yaml"
apiVersion: lumen.ai/v1alpha1
kind: Flow
metadata:
  name: run-code-example
  namespace: tenant-acme
spec:
  displayName: "Run code"
  triggers:
    - id: start
      ref: {name: manual-run}
      next: [run-code]
  nodes:
    - id: run-code
      stepRef: {name: run-code}
      config:
        code: |-
          const lines = JSON.parse(input.lines)
          const total = lines.reduce((sum, l) => sum + l.qty * l.price, 0)
          console.log('lines', lines.length)
          return { total, withVat: Math.round(total * 1.18 * 100) / 100 }
        inputs:
          lines: '{read.lines}'
        outputs:
        - name: total
          type: number
        - name: withVat
          type: number
```
