# 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/es/reference/steps/run-code

## De un vistazo

| Propiedad | Valor |
| --- | --- |
| **Nombre del paso** | `run-code` |
| **Recurso** | ClusterStep (`lumen.ai/v1alpha1`) |
| **Grupo de la paleta, categoría** | control, logic |
| **Créditos** | **1** crédito cada vez que tiene éxito |
| **Efectos externos** | sí: Cambia algo fuera de Lumen, así que un reintento pregunta antes y una prueba muestra lo que haría en lugar de hacerlo. |
| **La instalación debe aportar** | nada |

## Entradas

| Propiedad | Valor |
| --- | --- |
| **Necesita** | nada |
| **Produce** | `fields` |
| **Trabaja sobre** | item: un documento cada vez |

Necesita y Produce son tipos de datos (file, text, fields, email). El editor ofrece un paso solo donde lo que necesita ya está en el camino. Consulta [Documentos y datos](https://docs.go-lumen.ai/es/concepts/how-flows-run.md#documents-and-data).

## Salidas

Un nodo posterior se refiere a una salida como `{nodeId.field}` en un texto, o `nodeId.field` en una condición.

| Salida | Tipo |
| --- | --- |
| `result` | string |

More outputs are declared in the setting `outputs`.

## Ajustes

Se definen bajo `config:` en el nodo del flujo. Los ajustes de texto pueden contener marcadores de posición como `{nodeId.field}`.

| Ajuste | Tipo | Valor por defecto | Descripción |
| --- | --- | --- | --- |
| `code` (obligatorio) | 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` (obligatorio) | string |  | Name (minLength 1; maxLength 64; pattern \`^\[A-Za-z\]\[A-Za-z0-9\_\]\*$\`) |
| `outputs[].type` (obligatorio) | 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) |

El JSON Schema completo de estos ajustes: [run-code.schema.json](https://docs.go-lumen.ai/schemas/steps/run-code.schema.json).

## Ejemplo

```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
```

## Úsalo en un flujo

Un nodo que ejecuta `run-code` va bajo `spec.nodes` de un [Flow](https://docs.go-lumen.ai/es/concepts/how-flows-run.md#the-flow-as-yaml). Dale cualquier id y enumera en next los ids de los nodos siguientes.

```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
```
