# Fetch.Service or Fetch.make (https://effetch.monarcode.dev/docs/clients/service-or-make)

Most apps declare each client with `Fetch.Service`, and never need anything
else. Underneath it is `Fetch.make`, a function that builds a client
directly. It's the better fit for a script, for a config that only an
Effect can produce, or for a client made while the program runs. This page
shows how the two relate, what you give up with `Fetch.make`, and when to
use each.

## How they relate

`Fetch.make` takes the same config as `Fetch.Service` and returns an Effect
that builds the client. `Fetch.Service` calls `Fetch.make` in its layer, and
adds the pieces an app needs around it: a service key, a layer and the
config to read back. Both give you the same client, with the same calls and
the same types.

## What you give up with Fetch.make

With `Fetch.make`, you write by hand what the class gives you:

- **No service key.** To share the client as a service, you declare one
  with `Context.Service`, as in
  [Fetch.make with a config from an Effect](https://effetch.monarcode.dev/docs/clients/service-or-make#fetchmake-with-a-config-from-an-effect).
- **No layer.** You build one with `Layer.effect`.
- **No `Api.config`.** There's no frozen config to spread into a derived
  client (see [Derive a client](https://effetch.monarcode.dev/docs/clients/derived)).
- **No `Fetch.isService`.** It returns `false` for a key you wrote with
  `Context.Service`.

## Which one to use

Use the class by default, and use `Fetch.make` when the class doesn't fit.
The following table matches common situations to each:

| Situation                                                                          | Use             |
| ---------------------------------------------------------------------------------- | --------------- |
| An app, with the client shared as a service                                        | `Fetch.Service` |
| A config known when the module loads (a `baseUrl` Effect such as `Config` is fine) | `Fetch.Service` |
| Endpoints shared as a service with [`Fetch.Endpoints`](https://effetch.monarcode.dev/docs/endpoints/services)   | `Fetch.Service` |
| A script, a one-off program, or a quick test                                       | `Fetch.make`    |
| A config that comes from an Effect, such as headers read from a service            | `Fetch.make`    |
| A client made inside your own service's layer, or one per tenant at runtime        | `Fetch.make`    |

`Fetch.Endpoints` takes the client's service key, so it works with any key,
including one you wrote by hand. `Fetch.Service` is the shorter way there,
since the class is already that key.

## Fetch.make in a script

In a script, call `Fetch.make` inline and provide an `HttpClient` where you
run it:

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

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

const program = Effect.gen(function* () {
  const api = yield* Fetch.make({ baseUrl: "https://api.example.com" });
  return yield* api.get("/health");
});

await Effect.runPromise(program.pipe(Effect.provide(FetchHttpClient.layer)));
```

## Fetch.make with a config from an Effect

When the config itself comes from an Effect, such as a header read from
another service, call `Fetch.make` inside that Effect, then declare the
service and its layer by hand:

```ts
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";

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

class Tenant extends Context.Service<Tenant, { readonly id: string }>()(
  "Tenant",
) {}

const makeApi = Effect.gen(function* () {
  const tenant = yield* Tenant;
  return yield* Fetch.make({
    baseUrl: "https://api.example.com",
    headers: { "x-tenant": tenant.id },
  });
});

export class Api extends Context.Service<Api, Effect.Success<typeof makeApi>>()(
  "Api",
) {}

export const ApiLive = Layer.effect(Api)(makeApi);
```

`ApiLive` needs a `Tenant` and an `HttpClient`, and `Api` works as a client
key for `Fetch.Endpoints`.

## What's next

- [Declare a client](https://effetch.monarcode.dev/docs/clients): declare a client with `Fetch.Service`.
- [Client configuration](https://effetch.monarcode.dev/docs/clients/configuration): look up the options
  both take.
- [Share endpoints as a service](https://effetch.monarcode.dev/docs/endpoints/services): bind endpoints
  to a client's service key with `Fetch.Endpoints`.