# Scripts

#### Features

* Access input: customer phone number, session vars.
* Produce output `data` JSON and `outcome` string.
* Save `data` (optional with JSON Path to access any fields) to session variables.
* Fetch HTTP API and parse its response.

**Example script code:**

*UPPERCASE, `userName` are placeholders*

```
// name=SCRIPT_NODE1
export async function onExecute(input, config) {
    console.log('hello from script node!');
    const resp = await fetch('https://jsonplaceholder.typicode.com/users/1');
    const body = await resp.json();

    // 👉 return [data, outcome]
    return [{FIELD1: 'one', FIELD2: 42, userName: body.name}, 'OUTCOME2'];
}
```

**Parameter: `input`**

*UPPERCASE are placeholders*

```
{
    "sessionContext": {
        "business": { "id": "..." },         // only business.id
        "customer": { "phoneNumber": "..." } // only customer.phoneNumber
        "variables": { "VAR1": "1", ... }    // session variables
    },
    "SCRIPT_NODE1": {
        "outcome": "...",                    // SCRIPT_NODE1.outcome
        "data": { "FIELD1": "one", ...}      // SCRIPT_NODE1.data
    }
}
```

**Parameter: `config`**

*UPPERCASE are placeholders*

```
{
    "secrets": { "SECRET1": "one", ... }
    // other session data, but won't be supported in v1
}
```

**Return: `[data, outcome]`**

```
return [{FIELD1: 'one', FIELD2: 42}, 'OUTCOME2']
```

* must return in format `[data, outcome]`
  * other formats may be partially supported: only data, only outcome, etc.

**`outcome`**: must be a string

* each possible outcome will be mapped to an edge
* there is **no default outcome** nor **fallback outcome** for v1

**`output`**: must be a json object

* no other requirement

#### Mapping `data` and `outcome`

*UPPERCASE are placeholders*

| `raw_param`  | session variable |
| ------------ | ---------------- |
| `outcome`    | → `VARX`         |
| `data`       | → `VAR1`         |
| `data.FOO.1` | → `VAR_FOO1`     |

\ <br>
