# Share endpoints as a service (https://effetch.monarcode.dev/docs/endpoints/services)

Your definitions can't make calls until they're bound to a client, and you
want that done once, not in every function that calls them. `Fetch.Endpoints`
binds a record of definitions when its layer is built, and shares the result
as an Effect service, so any code in your app can `yield*` it. This page
shows how to declare one, and what the class gives you.

## Declare the service

`Fetch.Endpoints` does for endpoints what `Fetch.Service` does for the
client. It takes the service's id, the client's service and the record of
definitions:

```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";
import { bearer } from "@monarcode/effetch/plugins";

declare const readToken: () => Promise<string | null>;

const User = Schema.Struct({ id: Schema.Finite, name: Schema.String });
const NewUser = Schema.Struct({ name: Schema.String });
const Credentials = Schema.Struct({
  user: Schema.String,
  password: Schema.String,
});
const Session = Schema.Struct({ token: Schema.String });

const getUser = Fetch.endpoint("GET /users/:id", { output: User });
const listUsers = Fetch.endpoint("GET /users", {
  query: Schema.Struct({ page: Schema.optional(Schema.Finite) }),
  output: Schema.Array(User),
});
const createUser = Fetch.endpoint("POST /users", {
  input: NewUser,
  output: User,
});
const login = Fetch.endpoint("POST /auth/login", {
  input: Credentials,
  output: Session,
});

export class Api extends Fetch.Service<Api>()("Api", {
  baseUrl: "https://api.example.com",
  plugins: [bearer({ token: () => readToken() })],
}) {}

export class UserApi extends Fetch.Endpoints<UserApi>()("UserApi", Api, {
  get: getUser,
  list: listUsers,
  create: createUser,
  // A [definition, options] pair: logging in sends no token.
  login: [login, { plugins: { bearer: false } }],
}) {}

export const program = Effect.gen(function* () {
  const users = yield* UserApi;
  const ada = yield* users.create({ body: { name: "Ada" } });
  const page = yield* users.list({ query: { page: 2 } });
  const user = yield* users.get({ params: { id: ada.id } });
  return { page, user };
});

export const AppLayer = UserApi.layer.pipe(
  Layer.provideMerge(Api.layer),
  Layer.provide(FetchHttpClient.layer),
);
```

Most entries are a definition. An entry can also be a
`[definition, options]` pair, with the options `api.bind` takes: `headers`,
and `plugins` overrides, which are checked against the client's plugins.
Here, `login` turns off the `bearer` plugin, so logging in sends no token
(see [Built-in plugins](https://effetch.monarcode.dev/docs/plugins)).

`Layer.provideMerge` keeps `Api` in the layer's output, so code can still
`yield* Api` for one-off calls.

## What the class gives you

Like `Fetch.Service`, the class gives you three things:

- **`UserApi` is the service.** `yield* UserApi` gives the endpoints, each
  typed with the client's plugins, `defaultError` and `statusErrors`.
- **`UserApi.layer` binds them** to the client once, when it's built. It
  needs the client's service, here `Api`, and never fails. The client can
  also be a service you wrote by hand.
- **`UserApi.definitions` is the record you passed**, frozen. You can read
  it when the module loads, without building anything.

Two things to keep in mind when you declare one:

- **Give each class its own id.** The id, `"UserApi"` here, is the
  service's identity at run time, as with any Effect service. Two classes
  with the same id are the same service.
- **Each entry is checked when the class is declared.** An entry that isn't
  a definition or a `[definition, options]` pair throws then, so the mistake
  shows up when the module is imported.

## How bound endpoints get the client's defaults

Each endpoint is bound to the client, so it picks up what the client
holds. A definition without an `error` schema decodes error bodies with the
client's `defaultError`, and the client's `statusErrors` are tried after
the endpoint's. Headers can come from the definition and from the pair,
and the pair's win. The rules for headers, plugin overrides and schemas are
in [How defaults combine](https://effetch.monarcode.dev/docs/endpoints/defining#how-defaults-combine).

## What's next

- [Call an endpoint](https://effetch.monarcode.dev/docs/endpoints/calling): see what each call takes,
  and how to bind endpoints without a service.
- [Structure an app](https://effetch.monarcode.dev/docs/structuring-an-app): decide where clients and
  endpoints live as your app grows.
- [Test with a fake backend](https://effetch.monarcode.dev/docs/testing): serve your endpoints from a
  fake backend in tests.