# API Helper

Source: https://docs.autessa.com/docs/autessascript/helpers/api

> The api helper: SSRF-protected HTTP requests from AutessaScript with get, post, put, patch, and delete.

# API Helper

The `api` helper is the only way to make HTTP requests from AutessaScript. Every method takes a JSON string describing the request, blocks until the response arrives, and returns a JSON string envelope containing the status code, body, and headers. All requests pass through the platform's SSRF protection before leaving the box.

## Methods

Requests and responses are JSON *strings* on both sides — build the request with `JSON.stringify(...)` and unpack the result with `JSON.parse(...)`.

  - `api.get` — string"> Sends a GET request. No body.
  - `api.post` — string"> Sends a POST request. The optional second argument is the raw request body; if omitted, a `body` field inside `requestJson` is used instead.
  - `api.put` — string"> Sends a PUT request. Body handling is identical to `post`.
  - `api.patch` — string"> Sends a PATCH request. Body handling is identical to `post`.
  - `api.delete` — string"> Sends a DELETE request. No body variant — only the `body` field inside `requestJson` is honored.

## The request JSON

  - `endpoint` (string, required, required) — Full URL including scheme. Only `http` and `https` are allowed.
  - `headers` (object, optional) — Header name to string value. Set your own `Authorization` header here for authenticated calls.
  - `body` (any, optional) — Request body, serialized as JSON and sent verbatim. Ignored when the separate `body` argument to `post`/`put`/`patch` is provided (the argument wins).
  - `timeoutSeconds` (number, optional) — How long to wait for the full response, in seconds. Defaults to 60; values are clamped to 1–300. Raise it only for endpoints that are known to be slow, such as long-running jobs.

Body rules worth knowing:

- Whenever a body is present (argument or field), `Content-Type: application/json` is set automatically.
- `POST`, `PUT`, and `PATCH` with no body at all send an empty body.
- Redirects are **never** followed — a `301`/`302` comes back to your script as-is, with the `Location` header intact.

```javascript
// GET with headers
const raw = api.get(JSON.stringify({
  endpoint: "https://api.example.com/v1/orders?status=open",
  headers: { "Authorization": "Bearer " + variables.get("apiToken") }
}));

// POST with a body field
const created = api.post(JSON.stringify({
  endpoint: "https://api.example.com/v1/orders",
  headers: { "Authorization": "Bearer " + variables.get("apiToken") },
  body: { sku: "A-100", quantity: 2 }
}));

// POST with a separate body argument (handy for pre-serialized payloads)
const alsoCreated = api.post(
  JSON.stringify({ endpoint: "https://api.example.com/v1/orders" }),
  JSON.stringify({ sku: "A-100", quantity: 2 })
);
```

## The response envelope

Every call returns a JSON string with the same shape — parse it and check `statusCode`:

```json
{
  "statusCode": 200,
  "body": "{\"orders\":[...]}",
  "headers": {
    "content-type": "application/json",
    "x-request-id": "abc123"
  }
}
```

  - `statusCode` (number) — The HTTP status code, or `-1` when the request never completed (see errors below).
  - `body` (string) — The raw response body. If the endpoint returns JSON, `JSON.parse` it yourself — it arrives as a string.
  - `headers` (object) — Response headers; multi-valued headers are joined with `", "`.

```javascript
const res = JSON.parse(raw);
if (res.statusCode >= 200 && res.statusCode < 300) {
  const data = JSON.parse(res.body);   // second parse: body is a string
  response.setResponse(JSON.stringify({ count: data.orders.length }));
} else {
  response.setResponse(JSON.stringify({ error: "upstream returned " + res.statusCode }));
}
```

> Two parses are always needed for JSON APIs: one for the envelope, one for `body`. Forgetting the second parse is the most common `api.*` mistake.

## Error shape

When the request fails before an HTTP response exists (blocked URL, network error, interrupt), the envelope carries error fields instead, with `statusCode: -1`:

```json
{
  "error": true,
  "errorCode": "SSRF_BLOCKED",
  "errorMessage": "Requests to private addresses are not allowed",
  "statusCode": -1,
  "body": "",
  "headers": {}
}
```

  - `SSRF_BLOCKED` (errorCode) — The URL failed SSRF validation (see below).
  - `SECURITY_ERROR` (errorCode) — The SSRF protection service wasn't available, so the request was refused outright.
  - `IO_ERROR` (errorCode) — Network-level failure: DNS, connection refused, timeout, TLS.
  - `INTERRUPTED` (errorCode) — The request thread was interrupted mid-flight.
  - `UNKNOWN_ERROR` (errorCode) — Anything else — malformed request JSON, unexpected runtime failure.

Check `res.error === true` (or `statusCode === -1`) before treating the envelope as an HTTP response.

## SSRF protection

Every `endpoint` is validated before the request is sent; requests to private networks, internal infrastructure, and non-HTTP(S) schemes are rejected with `SSRF_BLOCKED`.

Timeouts: 30 seconds to establish a connection, and 60 seconds for the full request unless the request sets `timeoutSeconds` (up to 300). A timeout surfaces as `IO_ERROR`.

```javascript
// A slow endpoint: wait up to 150 seconds for the response.
var res = JSON.parse(api.post(JSON.stringify({
  endpoint: "https://jobs.example.com/run",
  body: { input: "..." },
  timeoutSeconds: 150
})));
```

## Related

- [Connector helper](/docs/autessascript/helpers/connector.md) — get OAuth tokens to use in `Authorization` headers
- [The sandbox](/docs/autessascript/sandbox.md)
- [Examples](/docs/autessascript/examples.md)
- [AutessaScript overview](/docs/autessascript.md)

