Skip to Content

Getting started

Declare a client, make a typed call and run it, from an empty file to a working program.

In this tutorial, you write a complete program that fetches a todo from an API, typed and checked by a schema. Along the way, you meet the four ideas the rest of Effetch builds on.

Before you begin

Install Effetch and Effect, and check that your project meets the requirements. For the commands, see Installation.

Write your first client

The following program fetches a todo from JSONPlaceholder, a free fake API. It's complete, so you can run it as it is:

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 .(.(.()));

What each part does

The numbered comments in the program mark its four parts:

  1. Todo describes the response. Effetch decodes the body with it, so todo is typed, and checked when it arrives. If the API sends something else, the call fails with a DecodeError instead of handing you bad data.
  2. Api is your client. Fetch.Service declares it as an Effect service, with everything its calls have in common: here the base URL, later the 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.
  4. Api.layer builds the client. It needs an HttpClient to send the requests. FetchHttpClient.layer uses the global fetch. Node's works too, and in tests you'd use a fake one (see Test with a fake backend).

How the pieces fit

Everything in Effetch builds on four ideas, from the bottom up:

  • An HttpClient sends requests. It's Effect's, and you choose it when you run your program: the global fetch, Node's, or a fake in tests.
  • A client is one backend: 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, for auth, retries, timeouts and logging.

The sidebar follows the same order: clients, calls and errors, then plugins, including your own, and endpoints. After those come streams, pagination, how to structure an app and testing.

The examples in these docs

To stay short, many examples start from declare const api: Fetch.Client: a client without plugins, like the one yield* Api gives you when Api has none. In your app, get the client with yield* Api. 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, to stay short. In your app, bind endpoints once, with Fetch.Endpoints (see Share endpoints as a service).

Every TypeScript example in these docs is compiled with the package, so they all match the API.

What's next

On this page