Skip to Content
Calls

Make a call

Pick a method, fill in the path's parameters and build the query string.

With a client in hand, every request is a single method call. You pass a path and an object of options, and you get back an Effect that sends the request when it runs. This page covers what every call has in common: the method, the path and its parameters, and the query string.

Pick a method

The client has a method for each common HTTP method, named in lowercase:

  • get, head and options send no body.
  • post, put, patch and del can send one. It's del, not delete, because delete is a reserved word in JavaScript.

Each one takes the path first, then the options:

import * as  from "effect/Schema";

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

declare const : .;

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

export const  = .("/todos/1", { :  });

export const  = .("/todos/1", {
  : { : "Write the docs" },
  : ,
});

Three other methods cover the rest:

  • api.request takes the HTTP method as its first argument. Use it when the method is only known at runtime, or for one with no shorthand, such as TRACE or QUERY (see When the methods aren't enough).
  • api.sse and api.ndjson read a response that arrives a piece at a time (see Streaming responses).

Fill in path parameters

To put a value in the path, write that segment as a name that starts with :, and pass the value in params:

import * as  from "effect/Schema";

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

declare const : .;

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

// GET /todos/42
export const  = .("/todos/:id", { : { : 42 }, :  });

TypeScript reads the parameter names from the path, so it asks for exactly params: { id }, with no names missing and none extra. A path without parameters doesn't take params at all.

A value can be a string or a number. Effetch converts it with String() and encodes it with encodeURIComponent, so "a/b" stays one segment (a%2Fb) and can't add a level to the path.

A parameter is always a whole segment, from the : to the next /. In /files/:name.json, the parameter is called name.json, not name. To send a file name with its extension, make the whole segment the parameter, as in /files/:file, and pass "report.json".

Build a query string

Pass the query as a plain object, and Effetch builds the string for you:

import * as  from "effect/Schema";

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

declare const : .;

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

// GET /todos?page=1&tags=work&tags=home
export const  = .("/todos", {
  : { : 1, : ["work", "home"], :  },
  : .(),
});

The rules are made so that you can pass optional filters as they are:

  • An array repeats the key, as in tags=work&tags=home.
  • null and undefined values are left out, so search sends nothing.
  • Numbers, booleans and bigints are converted with String().

With Effect 4.1 or later, a value can also be Redacted, such as Redacted.make(apiKey). It's sent as its value, and Effect keeps it out of its own logs and traces.

The query object isn't checked against anything, so a typo in a key compiles. To type a query, or to send values that need encoding, such as dates, give an endpoint a query schema. See Typed query parameters.

What's next

On this page