Skip to Content

Getting started

Write a program that makes a typed call, run it and see what each part does.

The quickest way to see how Effetch works is to run a small program. In this tutorial, you write one that fetches a todo from a public API, checks it against a schema and prints its title. Then you go through it part by part, so you know what each line does before you write your own calls.

Before you begin

To follow this tutorial, you need the following:

  • Effetch and Effect, installed in an ESM project, as Installation shows;
  • Node.js 24, to run the program as it is, since it runs TypeScript files without a build step. On another runtime, the run step names a command that works there.

Write and run the program

The program calls JSONPlaceholder, a free fake API, so it needs no account or key.

  1. In your project, create a file named main.ts, with the following program:
import * as  from "effect/Effect";
import * as  from "effect/http/FetchHttpClient";
import * as  from "effect/Layer";
import * as  from "effect/Schema";

import {  } from "@monarcode/effetch";

// 1. What a todo looks like
const  = .({
  : .,
  : .,
  : .,
});

// 2. The client, declared once for the whole app
class  extends .<>()("Api", {
  : "https://jsonplaceholder.typicode.com",
}) {}

// 3. A call
const  = .(function* () {
  const  = yield* ;
  const  = yield* .("/todos/:id", {
    : { : 1 },
    : ,
  });
  .(.);
});

// 4. Run it, with the client and the HttpClient that sends the requests
const  = ..(.(.));

await .(.(.()));
  1. Run the program with the following command:

    node main.ts

    On another version of Node.js, run npx tsx main.ts instead. With Bun, run bun main.ts.

  2. Check the output. The program prints the title of todo 1:

    delectus aut autem

If you see that line, the call worked: Effetch sent the request, checked the response against Todo and gave you a typed value.

What each part does

The numbered comments in the program mark its four parts:

  1. Todo describes the response. It's a schema (a description of a value's shape, from Effect's Schema module), and Effetch decodes the body with it. That's why todo is typed, and why it's checked when it arrives. If the API sends something else, the call fails with a DecodeError, rather than handing you bad data.
  2. Api is your client. Fetch.Service declares it as an Effect service: a value your program asks for by name, and gets from a layer when it runs. The client holds what all its calls have in common. Here that's the base URL. Later, it can also hold headers, error schemas and plugins.
  3. api.get makes the call. yield* Api gives you the client, and the :id in the path is filled in from params. The call is an Effect, so nothing is sent until the program runs.
  4. Api.layer builds the client. A layer is a recipe for building a service, and this one needs an HttpClient to send the requests. FetchHttpClient.layer sends them with the global fetch. Any other HttpClient layer works too, such as a fake backend in tests (see Test with a fake backend).

How the pieces fit

Effetch has four building blocks. The program used the first three, and the fourth is plugins. From the bottom up:

  • An HttpClient sends the requests. It comes from Effect, and you pick it when you run your program: the global fetch, another HttpClient layer, or a fake one in tests.
  • A client stands for one backend, with its base URL, default headers, error schemas and plugins. You declare it once, with Fetch.Service.
  • A call is one request: a method such as api.get, or an endpoint you saved. It returns an Effect, typed by your schemas, that fails with typed errors.
  • Plugins wrap every call a client makes, to add auth, retries, timeouts or logging.

The guides in the sidebar follow the same order. They start with Clients, Calls and Errors. Then come Endpoints, for calls you save once and reuse, and Plugins, including your own. After those, the sidebar goes further, with Streaming responses, Pagination, Testing, Structure an app and Platform notes.

The examples in these docs

Most examples in these docs leave out the setup you wrote in this tutorial, to stay short. They start from a client that's already there:

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

declare const : .;

export const  = .("/todos");

Fetch.Client is the type of a client with no plugins, defaultError or statusErrors, like the Api in this tutorial. In your app, get the client with yield* Api, as the program does. To type a client in a function signature, use Effect.Success<typeof Api>, which includes your plugins and error schemas.

Some examples also bind an endpoint inline, with api.bind, for the same reason. In your app, bind endpoints once, with Fetch.Endpoints (see Share endpoints as a service).

Every TypeScript example in these docs is compiled against the package when the site is built, so each one matches the version the docs describe.

What's next

  • Declare a client: see what the client class gives you, and how to type it.
  • Client configuration: add headers, error schemas and plugins to your client.
  • Make a call: pick a method, fill in path parameters and build a query string.
  • Call errors: see what can go wrong in a call, and how to catch it.
  • Examples: read complete programs that use Effetch, from one file to a whole app.

On this page