# Getting started (https://effetch.pages.dev/docs/getting-started)

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](https://effetch.pages.dev/docs/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](https://jsonplaceholder.typicode.com), 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:

```ts
import * as Effect from "effect/Effect";
import * as FetchHttpClient from "effect/http/FetchHttpClient";
import * as Layer from "effect/Layer";
import * as Schema from "effect/Schema";

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

// 1. What a todo looks like
const Todo = Schema.Struct({
  id: Schema.Finite,
  title: Schema.String,
  completed: Schema.Boolean,
});

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

// 3. A call
const program = Effect.gen(function* () {
  const api = yield* Api;
  const todo = yield* api.get("/todos/:id", {
    params: { id: 1 },
    output: Todo,
  });
  console.log(todo.title);
});

// 4. Run it, with the client and the HttpClient that sends the requests
const ApiLive = Api.layer.pipe(Layer.provide(FetchHttpClient.layer));

await Effect.runPromise(program.pipe(Effect.provide(ApiLive)));
```

2. Run the program with the following command:

   ```bash
   node main.ts
   ```

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

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

   ```text
   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](https://effetch.pages.dev/docs/testing)).

> If your program runs in a web page and calls an API on another origin, read
> [Browsers](https://effetch.pages.dev/docs/platforms#browsers) first. Effect adds tracing headers to
> each request, and the browser blocks the call when the API doesn't allow them.

## 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](https://effetch.pages.dev/docs/endpoints) 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](https://effetch.pages.dev/docs/clients), [Calls](https://effetch.pages.dev/docs/calls) and [Errors](https://effetch.pages.dev/docs/errors).
Then come [Endpoints](https://effetch.pages.dev/docs/endpoints), for calls you save once and reuse,
and [Plugins](https://effetch.pages.dev/docs/plugins), including [your own](https://effetch.pages.dev/docs/plugins/writing).
After those, the sidebar goes further, with
[Streaming responses](https://effetch.pages.dev/docs/streaming), [Pagination](https://effetch.pages.dev/docs/pagination),
[Testing](https://effetch.pages.dev/docs/testing), [Structure an app](https://effetch.pages.dev/docs/structuring-an-app) and
[Platform notes](https://effetch.pages.dev/docs/platforms).

## 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:

```ts
import type { Fetch } from "@monarcode/effetch";

declare const api: Fetch.Client;

export const todos = api.get("/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](https://effetch.pages.dev/docs/endpoints/services)).

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](https://effetch.pages.dev/docs/clients): see what the client class gives you,
  and how to type it.
- [Client configuration](https://effetch.pages.dev/docs/clients/configuration): add headers, error
  schemas and plugins to your client.
- [Make a call](https://effetch.pages.dev/docs/calls): pick a method, fill in path parameters and
  build a query string.
- [Call errors](https://effetch.pages.dev/docs/errors): see what can go wrong in a call, and how to
  catch it.
- [Examples](https://effetch.pages.dev/docs/examples): read complete programs that use Effetch, from
  one file to a whole app.