Skip to Content
Endpoints

Typed query parameters

Give an endpoint a schema that types each call's query and encodes it before it's sent.

This page shows you how to give an endpoint a query schema, how each field is encoded into the URL, and what happens when a query doesn't match.

Add a query schema

Give an endpoint a query schema, usually a Schema.Struct, and each call's query is typed by it and encoded with it before it's sent. A Schema.Class works too, called with an instance:

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

import {  } from "@monarcode/effetch";

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

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

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

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

export const  = .(function* () {
  const  = yield* ;
  const  = .();
  // GET /todos?done=false&since=2026-01-01T00%3A00%3A00.000Z&page_size=20
  return yield* ({
    : { : false, : new ("2026-01-01"), : 20 },
  });
});

The schema is the endpoint's query property. client.bind, client.bindAll and Fetch.Endpoints keep it, and api.endpoint takes one too.

How each field is encoded

The schema encodes the query as follows:

  • Each field is encoded to a string: a Date to its ISO string, a boolean to "true" or "false", a number to its digits. An array repeats the key.
  • Rename keys with Schema.encodeKeys, as in the example.
  • A field you leave out isn't sent, but a null is sent as the string "null", so prefer optional fields to nullable ones.
  • A Schema.Redacted field is sent as its value and, with Effect 4.1 or later, stays redacted in Effect's logs and traces.

Queries that don't match

A query or a schema that can't be encoded fails early:

  • A query that doesn't match the schema, or a field that encodes to an object, is a bug, not an error to handle: the call dies before anything is sent. See Logging and defects.
  • A schema with a field that has no string encoding, such as Schema.instanceOf(Uint8Array), throws when the endpoint is defined.

What's next

On this page