# Outbound HTTP from scripts

A script can call an HTTPS endpoint outside Vectros with `vectros.http.request`. The call goes to a
**destination**, an address an account owner declares, and it is made only by a script version that
declares the operation and that an account owner has approved for that destination.

## The pieces

| Piece | What it is |
|---|---|
| Destination | A named address (`alias`) an account owner declares in the developer portal: its `https` base address, the methods it accepts, the app contexts whose scripts may call it, and the credential it sends. |
| Secret | A named credential value an account owner stores; a destination refers to it by name. |
| Approval | An account owner's approval of one script version, by the hashes of its source and of its declared operations, to call one destination. Changing the destination's base address or methods needs a new approval. |
| Declaration | The script version's `optInOperations` list, which must contain `http.request`. |

Vectros adds the credential to the call; the script does not receive it.

A secret's value is entered once. The portal, the CLI and the API do not return it after it is stored.

The real controls are your approval of the destination and the approval of each script version.

## How to call a destination

1. **Sign the waiver and declare the destination.** In the developer portal, an account owner opens
   **Script access** (the Script outbound access page), signs the outbound call waiver for the environment, and adds the
   destination, bound to the app context the script runs in.
2. **Push the script version** declaring the operation:

   ```sh
   vectros scripts push <name> --context <context> --source-file ./script.js --opt-in-operation http.request
   ```

   Declaring it needs the `script-egress-declare` capability (see the
   [identity and access reference](../identity-access/reference.md)).
3. **Approve that version for the destination.** An account owner runs:

   ```sh
   vectros scripts approve <name> --context <context> --destination <alias>
   ```

   The command shows the version, the SHA-256 of its source, the operations it declares and what
   changed since the version last approved for that destination, then asks before it writes. Without
   `--script-version` it approves the newest version.
4. **Call the destination** from the script:

   ```js
   const reply = vectros.http.request('billing-api', {
     method: 'POST',
     path: '/v1/invoices',
     headers: { 'x-request-source': 'vectros-trigger' },
     body: { customerId: input.record.customerId },
   });
   if (reply.status >= 300) throw new Error('billing-api answered ' + reply.status);
   const invoice = JSON.parse(reply.body);
   ```

A trigger rule that lists its own operations must list `http.request` too.

### Blueprints

`blueprint apply` declares and checks only: it never creates a destination or an approval, and a
blueprint never carries a credential. When no approval exists it prints `needs approval` with the
exact `vectros scripts approve ...` command (see [Blueprints](../clients/blueprints.md)).

## `vectros.http.request(alias, options)`

| Option | Type | Default | Notes |
|---|---|---|---|
| `method` | string | `GET` | One of `GET`, `POST`, `PUT`, `PATCH`, `DELETE`, and allowed by the destination. |
| `path` | string | required | Starts with `/`; appended to the destination's base address. No `?`, `#`, `//`, `.` or `..` segments, whitespace or non-ASCII characters: put parameters in `query`. |
| `query` | object | `{}` | Values are strings, numbers or booleans. |
| `headers` | object | `{}` | Values are strings, numbers or booleans. Some header names are reserved by the platform: a call that sets one fails with 400, and the message names the header (for example `header not allowed: <name>`); Set-Cookie headers in a reply are dropped. |
| `body` | string, object or array | none | An object or array is sent as JSON, with `Content-Type: application/json` unless you set one. Only `POST`, `PUT` and `PATCH` may carry a body. |
| `allowTruncated` | boolean | `false` | Accept the first part of a response longer than the platform reads. |

Any other option is refused. The call returns `{ status, headers, body, truncated }` for every
completed exchange, whatever its status: a `404` or a `500` from the destination is returned to the
script, not thrown. `body` is a string (parse JSON yourself), and header names are lowercase.

Calls go over https on port 443 only, to the address you declared, and are not redirected.

## Limits

| Limit | Value |
|---|---|
| Calls per execution | Each execution of a script may make at most 20 `http.request` calls. |
| Request bytes per execution | The requests of one execution may carry at most 256 KiB of request bytes in total. |
| Response bytes per execution | The responses of one execution may total at most 4 MiB. |
| Request body per call | 65,536 bytes |
| Response read per call | 1,048,576 bytes |

Only request bodies count toward the request bytes, and a call's body is counted once the call is attempted, whether or not it completes.
Each call also counts as one of the execution's host calls.

## Errors

A refused or failed call throws an error the script can catch. Its message is the text below; on the
rows marked *prefixed*, it reads `Request failed with status <status>: <text>`.

| Status | Text | When |
|---|---|---|
| `429` *prefixed* | `This execution has made its 20 http.request calls.` | A 21st call. |
| `429` *prefixed* | `This execution has used its budget of 262144 request bytes.` | The call's body would pass the request budget. |
| `429` *prefixed* | `This execution has read its budget of 4194304 response bytes.` | The responses read so far pass the response budget. |
| `400` *prefixed* | `A destination alias is required.`, `Unknown member '<name>'. Expected: method, path, query, headers, body, allowTruncated.`, `path is required.`, `body must be a string, an object or an array.` and the other option checks | The alias is missing, or an option is unknown, missing or of the wrong type. |
| `400` | The reason, for example `path must start with /`, `this method carries no body`, `request body exceeds 65536 bytes` | A request the platform will not send. |
| `400` | `destination is unreachable` | The destination's stored address can no longer be used, or its host name does not resolve to an address the platform will call. |
| `403` *prefixed* | `'http.request' is not permitted for script <name> version <n> and this destination.` | The alias names no active destination, or the waiver, the destination or the approval does not permit the call. |
| `403` *prefixed* | `The destination's authentication failed.` | The destination's credential cannot be added to the call. |
| `400` | `The destination's authentication failed.` | The query is too long once the destination's credential is added to it. |
| `408` | `the destination did not answer in time` | The execution's time ran out during the call. |
| `413` | `the response is longer than 1048576 bytes; pass allowTruncated: true to accept the first part` | A longer response without `allowTruncated`. |
| `502` | `destination is unreachable` | The connection failed. |
| `500` *prefixed* | `The call could not be completed.` | A platform fault. |
| `503` *prefixed* | `The destination's credential could not be used.` | A platform fault. |

## Credentials in responses

Before your script sees a response, Vectros replaces every exact copy of a credential it sent in
that call with `[redacted]`, in the response headers and body. This is a safeguard with stated
limits, not a guarantee.

- It matches exact copies only: a partial, re-encoded or hashed echo, or a value split across chunks, may not be caught.
- A credential form shorter than 8 characters is not matched. A shorter secret can still be stored and used.
- The forms matched are the raw value, the full header value (such as `Bearer ...`, and the base64 form for Basic), and the URL-encoded query value.

## Repeated calls

A trigger can run a script more than once for one event, so a destination can receive the same call more than once. For example, when the script's writes meet another write to the same item, the trigger runs the script again a few seconds later, up to twice, and a trigger that still fails is retried later. Each call carries an Idempotency-Key made from the event, the call's position in the run, and the call's destination name (the alias the script calls, not the address it points to), method, path, query and body. Headers are not part of it. A repeat of the same call carries the same key, so a destination that honours it can recognise the repeat; a call whose destination name, method, path, query or body changed carries a new key. A body that differs on every run, such as one holding the current time or a random id, therefore carries a new key every time and cannot be recognised as a repeat: keep such values out of the body if the destination relies on the key. Design the destination to accept a repeated call.
