Making Outgoing HTTP Requests from Effect Golem Agents
Use the Effect v4 HTTP client from effect/http. FetchHttpClient.layer supplies the
canonical HttpClient.HttpClient service through QuickJS's WASI-backed globalThis.fetch, while
keeping request construction, failures, response decoding, and dependency provision in Effect.
import { Effect, Schema } from "effect";
import {
FetchHttpClient,
HttpClient,
HttpClientRequest,
HttpClientResponse,
} from "effect/http";
Do not wrap globalThis.fetch in Effect.tryPromise for ordinary application HTTP. Do not use
node:http, node:https, @effect/platform-node, or another Node networking transport.
┌───────────────┐ HttpClient request ┌───────────────────────┐ WASI HTTP ┌────────────┐
│ Agent handler │─────────────────────────▶│ FetchHttpClient.layer │────────────────▶│ Remote API │
└───────────────┘ └───────────────────────┘ └────────────┘
The Http namespace from @golemcloud/effect-golem is separate: Http.mount, Http.get, and
Http.post declare incoming agent routes. They do not execute outbound requests.
Provide the Fetch Client
Provide FetchHttpClient.layer at the narrowest common application boundary. A one-off request can
provide it directly:
const readText = HttpClient.get("https://api.example.com/health").pipe(
Effect.flatMap(HttpClientResponse.filterStatusOk),
Effect.flatMap((response) => response.text),
Effect.provide(FetchHttpClient.layer),
);
For agent handlers, provide the layer to the composed HTTP effect returned by the handler. If a
larger application has several HTTP-dependent services, compose them into an application layer and
provide FetchHttpClient.layer once at that layer boundary.
GET and Decode JSON
Build immutable requests, execute them through the service, require a 2xx status, and decode the
JSON body with Effect Schema:
const ApiUser = Schema.Struct({
id: Schema.Number,
name: Schema.String,
});
const getUser = (id: number) =>
HttpClientRequest.get(`https://api.example.com/users/${id}`).pipe(
HttpClientRequest.setHeaders({
Accept: "application/json",
Authorization: "Bearer my-token",
}),
HttpClient.execute,
Effect.flatMap(HttpClientResponse.filterStatusOk),
Effect.flatMap(HttpClientResponse.schemaBodyJson(ApiUser)),
Effect.provide(FetchHttpClient.layer),
);
HttpClientResponse.schemaBodyJson both parses JSON and validates its shape. Transport, status,
body-decoding, and schema failures remain in the Effect error channel.
For several calls, filter the client once:
const program = Effect.gen(function* () {
const client = (yield* HttpClient.HttpClient).pipe(HttpClient.filterStatusOk);
const response = yield* client.get("https://api.example.com/users/1", {
headers: { Accept: "application/json" },
});
return yield* HttpClientResponse.schemaBodyJson(ApiUser)(response);
}).pipe(Effect.provide(FetchHttpClient.layer));
Choose either client-level HttpClient.filterStatusOk or response-level
HttpClientResponse.filterStatusOk; do not apply both to the same request.
POST JSON
Use HttpClientRequest.bodyJson so serialization and the JSON content type are handled by the
Effect HTTP request model:
const createUser = (name: string, email: string) =>
HttpClientRequest.post("https://api.example.com/users").pipe(
HttpClientRequest.setHeader("Accept", "application/json"),
HttpClientRequest.bodyJson({ name, email }),
Effect.flatMap(HttpClient.execute),
Effect.flatMap(HttpClientResponse.filterStatusOk),
Effect.flatMap((response) => response.text),
Effect.provide(FetchHttpClient.layer),
);
bodyJson is effectful because JSON encoding can fail. When a request body has an Effect Schema,
prefer HttpClientRequest.schemaBodyJson(InputSchema)(input). Other request helpers include
bodyText, bodyUint8Array, bodyUrlParams, bodyFormData, and bodyStream.
Use HttpClientRequest.put, patch, or delete for the corresponding HTTP method.
Read Responses
Status and headers are immediate response fields. Body readers are Effects:
const inspectResponse = (url: string) =>
Effect.gen(function* () {
const response = yield* HttpClient.get(url);
const body = yield* response.text;
return {
status: response.status,
contentType: response.headers["content-type"],
body,
};
}).pipe(Effect.provide(FetchHttpClient.layer));
Use:
response.textfor text;response.jsonfor an unvalidated parsed JSON value;HttpClientResponse.schemaBodyJson(Schema)for validated JSON;response.arrayBufferfor binary data; orresponse.streamfor streaming consumption.
Apply HttpClientResponse.filterStatusOk when non-2xx responses are failures. If the application
must inspect a non-2xx body before deciding, read the response and branch on response.status
instead.
Agent Handler Error Types
The HTTP client keeps transport, non-2xx, body-decoding, timeout, and schema failures typed in the
Effect error channel. An Effect-Golem method with no error schema cannot return those failures.
Either:
- declare an Effect Schema for caller-visible failures and map the HTTP errors to that type; or
- use
Effect.orDiefor genuinely unexpected failures that should become defects.
const infallibleHandlerEffect = getUser(1).pipe(Effect.orDie);
Do not use defects for expected business outcomes.
Timeouts and Cancellation
Put the timeout around request execution and body consumption, because receiving response
headers does not mean the body is complete:
const timed = HttpClient.get("https://api.example.com/slow").pipe(
Effect.flatMap(HttpClientResponse.filterStatusOk),
Effect.flatMap((response) => response.text),
Effect.timeout("5 seconds"),
Effect.provide(FetchHttpClient.layer),
);
FetchHttpClient passes Effect interruption to a fetch AbortSignal. Prompt cancellation of the
underlying WASI HTTP operation is currently blocked by the QuickJS runtime issue tracked in