Skip to Content
Endpoints

Endpoints or methods

Learn what endpoints add over methods, and when to use each.

When the same call shows up in a few places, its path and schemas start to repeat, and a typo in one copy still compiles. An endpoint saves the call once, so every place that uses it stays in step. This page shows what that buys you, and when a plain method is still the better choice.

What an endpoint is

An endpoint is a call you've saved under a name. Here's the same request as a method call and as an endpoint:

import * as  from "effect/Effect";
import * as  from "effect/Schema";

import {  } from "@monarcode/effetch";

const  = .({ : ., : . });

class  extends .<>()("Api", {
  : "https://api.example.com",
}) {}

// Saved once, with its route and schema, and bound to the client
const  = .("GET /users/:id", { :  });

class  extends .<>()("Users", , {
  : ,
}) {}

export const  = .(function* () {
  const  = yield* ;
  const  = yield* ;

  // A method call spells out the path and the schema
  const  = yield* .("/users/:id", {
    : { : 1 },
    : ,
  });

  // An endpoint call passes only what changes
  const  = yield* .({ : { : 2 } });

  return [, ];
});

Both calls send GET /users/:id, and decode the response with User. The difference is in what you write at the call site.

There are two ways to define an endpoint:

  • Fetch.endpoint(route, definition) gives a definition: plain data, with no client. You can export it from a file next to its schemas, then bind it to a client with api.bind, or share a group of them as a service with Fetch.Endpoints.
  • api.endpoint(route, definition) defines and binds in one step, on a client you already have.

Both give the same kind of bound endpoint, which is the function you call. Call an endpoint compares them.

What endpoints add

An HTTP call mixes two kinds of information. Some of it is fixed by the API: the route, the schemas for the response, the error body and the request body, and rules for that route, such as "logging in sends no token". The rest changes on every call: the path's parameters, the query values, the body, and the odd one-off header.

With methods, you write both kinds at every call site. Three calls to GET /users/:id repeat the path and the User schema three times. A mistake in one of them, such as the wrong schema, or a forgotten bearer: false (which turns off the bearer auth plugin for that call), still compiles. An endpoint takes the fixed part once, when it's defined, and only the changing part when it's called. That gives you:

  • One place per route. A path or a schema changes in one line, and the service that holds your endpoints lists the API you use.
  • Stricter types than a method can have. A method gets its HTTP method and its input schema only at the call, so its types have to allow every case. An endpoint has both from its definition. body is required when there's an input (it's optional with api.post). A GET endpoint rejects a body at compile time, while api.request("GET", …) only dies at runtime. The route is checked as you type.
  • Defaults that can't be forgotten. An endpoint's headers and plugin overrides apply to every call.
  • A name instead of a URL. Call sites read users.get(…), and the endpoint's route, method and path are there for query keys and log labels.

Endpoints add no new behavior. A call goes through the same plugins, decoding and errors as the method it stands for, so everything in Declare a client, Make a call, Call errors and Built-in plugins applies to both. An endpoint is a saved call, not a contract shared with a server. For that, use Effect's HttpApi.

When to use an endpoint

Use an endpoint when a call:

  • is made from more than one place;
  • has rules every call must follow, such as an auth exception or a content type;
  • sends a body with an input schema;
  • belongs to a backend whose API you want listed in one place.

When to use a method

Use a method for one-off calls, and for calls whose method or path is only known at runtime. Mixing the two is normal. Keep the client available next to its endpoints, for the calls that aren't worth one.

Patterns to avoid

The following patterns compile, but cost you what endpoints are for:

  • api.endpoint(…)(…) at a call site. That's a method call written twice. Use the method.
  • Making or binding endpoints in code that runs on every request. Do it once, when the layer that holds them is built, as Fetch.Endpoints does.
  • Passing an endpoint's plugin overrides at every call. Give them once instead. A Fetch.endpoint definition can't hold them, since overrides are checked against a client's plugins, so give them when binding, as in api.bind(login, { plugins: { bearer: false } }). An api.endpoint definition can hold them directly.
  • Spreading several groups of endpoints into one object. When two use the same name, the later one silently wins. Nest them instead, as in { users, todos }, or give each group its own Fetch.Endpoints class.

How an endpoint's headers, overrides and schemas combine with the client's and the call's is in Define endpoints.

What's next

On this page