Autorun AI Docs

15. HTTP Requests


http(options)

async · returns { ok, status, statusText, headers, body }

Sends an outbound HTTP request. One function covers every method.

Prop

Type

const response = await context.functions.http({
  method: "POST",
  url: "https://api.example.com/webhook",
  headers: { "Authorization": "Bearer " + context.env.API_TOKEN },
  body: { event: "invoice_created", id: context.record.ID }
});

if (!response.ok) throw new Error("Webhook failed with " + response.status);
log(response.body.messageId);

Returned value

Prop

Type

{
  "ok": true,
  "status": 200,
  "statusText": "OK",
  "headers": { "content-type": "application/json" },
  "body": { "status": "received", "messageId": "msg_8f3a2b" }
}

The body is parsed for you when the response content type contains json — no JSON.parse needed. Anything else arrives as a string.


Errors do not throw

Always check response.ok

Timeouts, DNS failures, refused connections, and 4xx/5xx responses all resolve rather than reject. A failed call returns status: 0 with statusText set to "Timeout", "Request Failed", or "Error", and the reason on an error property. Without an ok check, your process carries on with body set to undefined.

const response = await context.functions.http({ url: context.env.CRM_URL + "/contacts" });

if (!response.ok) {
  throw new Error("CRM unreachable: " + response.statusText + " " + (response.error || ""));
}

Sending a body

An object is serialized to JSON automatically and sent as application/json:

body: { event: "created", id: 84 }
Object keys are camelCased on the way out

Automatic serialization converts property names to camelCase, so { InvoiceNumber: "INV-1" } is sent as {"invoiceNumber":"INV-1"}. Entity fields are PascalCase, so forwarding a record as-is quietly renames every field. When the receiver needs exact names, stringify it yourself — a string body is transmitted untouched.

// Exact control over the payload
await context.functions.http({
  method: "POST",
  url: endpoint,
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ InvoiceNumber: record.InvoiceNumber, Amount: record.Amount })
});

For other content types, set Content-Type in headers and pass a string body:

await context.functions.http({
  method: "POST",
  url: endpoint,
  headers: { "Content-Type": "application/x-www-form-urlencoded" },
  body: "grant_type=client_credentials&client_id=" + context.env.CLIENT_ID
});

Timeouts

timeout is in milliseconds, defaults to 30,000, and is capped at 60,000 — a larger value is silently reduced. Remember the process itself also has a timeout (30 seconds by default), so raise @timeout on the process when you allow a long request.

/** @trigger Sync with CRM @timeout 120 */
const response = await context.functions.http({ url: slowEndpoint, timeout: 60000 });

Basic authentication

There is no helper for encoding credentials — use btoa:

const encoded = btoa(context.env.API_USER + ":" + context.env.API_PASSWORD);

const response = await context.functions.http({
  url: "https://api.example.com/data",
  headers: { "Authorization": "Basic " + encoded }
});

Retrying

Nothing is retried for you. For flaky endpoints, back off and try again:

async function fetchWithRetry(options, attempts = 3) {
  for (let i = 0; i < attempts; i++) {
    const response = await context.functions.http(options);
    if (response.ok) return response;
    if (response.status >= 400 && response.status < 500) return response;  // client error — retrying won't help
    log("Attempt " + (i + 1) + " failed (" + response.status + "), retrying");
  }
  throw new Error("Gave up after " + attempts + " attempts");
}

const response = await fetchWithRetry({ url: context.env.CRM_URL + "/contacts" });