Skip to content

http

Fetch URLs from Agency code. Returns the response as text, JSON, or Markdown. Aborting tears down the in-flight request.

ts
import { fetch, fetchJSON } from "std::http"

node main() {
  const page = fetch("https://example.com")
  const data = fetchJSON("https://api.example.com/status")
  print(page)
}

Effects

std::http::fetch

ts
effect std::http::fetch {
  baseUrl: string;
  path: string
}

(source)

std::http::fetchJSON

ts
effect std::http::fetchJSON {
  baseUrl: string;
  path: string
}

(source)

std::http::fetchMarkdown

ts
effect std::http::fetchMarkdown {
  baseUrl: string;
  path: string
}

(source)

Functions

fetch

ts
fetch(baseUrl: string, path: string, headers: Record<string, any>, allowedDomains: string[]): Result

Fetch a URL and return the response as text.

@param baseUrl - The base URL to fetch @param path - Optional path appended to baseUrl @param headers - Custom request headers @param allowedDomains - Restrict fetches to these domains (empty allows all)

On abort, Agency tears down the in-flight HTTP request and body read. The abort surfaces as an AgencyCancelledError that propagates out of the surrounding try.

Parameters:

NameTypeDefault
baseUrlstring
pathstring""
headersRecord<string, any>{}
allowedDomainsstring[][]

Returns: Result

Throws: std::http::fetch

(source)

fetchJSON

ts
fetchJSON(baseUrl: string, path: string, headers: Record<string, any>, allowedDomains: string[]): Result

Fetch a URL and return the response parsed as JSON.

@param baseUrl - The base URL to fetch @param path - Optional path appended to baseUrl @param headers - Custom request headers @param allowedDomains - Restrict fetches to these domains (empty allows all)

On abort, Agency tears down the in-flight HTTP request and body read. The abort surfaces as an AgencyCancelledError that propagates out of the surrounding try.

Parameters:

NameTypeDefault
baseUrlstring
pathstring""
headersRecord<string, any>{}
allowedDomainsstring[][]

Returns: Result

Throws: std::http::fetchJSON

(source)

fetchMarkdown

ts
fetchMarkdown(baseUrl: string, path: string, headers: Record<string, any>, allowedDomains: string[]): Result

Fetch a URL and return the body as readable markdown when the response is HTML, or as plain text otherwise. Good for extracting page content for an LLM. Fails on network errors, domain violations, or if the response body exceeds 10 MB.

@param baseUrl - The base URL to fetch @param path - Optional path appended to baseUrl @param headers - Custom request headers @param allowedDomains - Restrict fetches to these domains (empty allows all)

On abort, Agency tears down the in-flight HTTP request and body read. The abort surfaces as an AgencyCancelledError that propagates out of the surrounding try.

Parameters:

NameTypeDefault
baseUrlstring
pathstring""
headersRecord<string, any>{}
allowedDomainsstring[][]

Returns: Result

Throws: std::http::fetchMarkdown

(source)