Skip to Content

Introduction

Typed HTTP calls for Effect, without the boilerplate.

Calling a REST API with Effect's HttpClient means writing the same steps for every request: build the URL, send it, check the status, decode the body. Effetch does those steps for you. You give it a path and a schema, and you get back an Effect that's typed end to end, so a call's type says what it returns and every way it can fail.

This page shows why Effetch exists and where it fits next to Effect's own tools. To start using it, see Installation.

Why Effetch

Effect gives you two ways to call an HTTP API, and they sit at opposite ends:

  • HttpClient is low level. For each request, you build the URL, check the status and decode the body yourself.
  • HttpApi is contract first. You describe the whole API up front and share that description with the server. That's a good fit when you own both sides.

Most apps sit somewhere in between. They call a backend they don't control, such as their company's REST API or a third-party service, one call at a time. Effetch is made for that case. Each call gets the types and error handling a contract would give it, without you writing the contract.

At a glance

Here's the same call written with HttpClient alone, and with Effetch:

import * as  from "effect/Effect";
import * as  from "effect/http/HttpClient";
import * as  from "effect/http/HttpClientResponse";
import * as  from "effect/http/HttpIncomingMessage";
import * as  from "effect/Schema";

import type {  } from "@monarcode/effetch";

declare const : .;

const  = .({ : ., : . });

// With HttpClient alone
export const  = (: number) =>
  .(function* () {
    const  = yield* .;
    const  = yield* .(
      `https://api.example.com/todos/${()}`,
    );
    const  = yield* .();
    return yield* .()();
  });

// With Effetch
export const  = (: number) =>
  .("/todos/:id", { : {  }, :  });

Both call the same URL and decode the response with the same schema. The Effetch version is one line, and it also:

  • checks at compile time that the call passes an id;
  • fails with NetworkError, HttpError or DecodeError, rather than HttpClientError | SchemaError, so you can handle each case on its own;
  • decodes the API's error body onto HttpError, with a schema you give it;
  • sends accept: application/json, since it expects JSON back;
  • runs the client's plugins, such as auth, retries, timeouts and logging.

What you get

  • Paths that check themselves. Write "/users/:id", and TypeScript asks for params: { id }, nothing more and nothing less.
  • Queries from plain objects. { page: 2, tags: ["a", "b"] } becomes ?page=2&tags=a&tags=b, and undefined values are left out.
  • Responses you can trust. One schema decodes the response, and another decodes the API's error body. Their types become the call's types.
  • Errors you can catch by name. A call fails with one of three tagged errors, NetworkError, HttpError or DecodeError, plus any your plugins add, and all of them are in its type. With statusErrors, you can also turn statuses into errors of your own, such as a 404 into TodoNotFound (see Errors by status).
  • Plugins for the repetitive parts. Auth with token refresh, retries, timeouts and logging are built in, and a plugin of your own takes a few lines. Any call can turn a plugin off or adjust it, and TypeScript checks the names and the options.
  • Endpoints. Save a route such as "GET /users/:id" with its schemas once, then call it like a function wherever you need it. Share a module's endpoints as a service with Fetch.Endpoints.
  • Streams and pages. Read Server-Sent Events and newline-delimited JSON as an Effect Stream, and page through a list with Fetch.paginate.
  • Tests without a network. FetchTest turns your endpoints into a typed fake backend, so your tests run the real client, plugins included.
  • No platform-specific code. Effetch has no Node.js imports and no separate builds. It's built for browsers, React Native, Node.js, Bun and Deno. Compatibility lists which of them CI tests.

Where it fits

  • Use Effetch to call REST and JSON APIs that you don't own, or don't share code with.
  • Use Effect's HttpApi when you own both the server and the client, and want them to share one contract.
  • Use HttpClient directly for a request Effetch doesn't model. Every Effetch client gives you its own HttpClient, with the base URL and plugins already applied.

Neither Effetch nor HttpClient covers WebSockets, which Effect's Socket module handles, or reports upload progress.

If you've used axios or ky, see Coming from axios or ky to see how what you're used to maps to Effetch.

What's next

  • Installation: add Effetch and Effect to your project.
  • Getting started: write a first program that makes a typed call, from an empty file to a working run.

On this page