# Typed query parameters (https://effetch.monarcode.dev/docs/endpoints/typed-query)

A plain `query` object isn't checked, so a misspelled filter compiles, and
a value such as a `Date` has to be converted by hand. Give the
endpoint a `query` schema instead, and each call's query is typed by the
schema and encoded with it before it's sent. This page shows how to add
one, how each field is encoded, and what happens when a query doesn't
match.

## Add a query schema

The following endpoint types its filters, sends a `Date` as an ISO string,
and renames `pageSize` to `page_size` in the URL:

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

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

const Todo = Schema.Struct({ id: Schema.Finite, title: Schema.String });

const TodoFilters = Schema.Struct({
  done: Schema.optional(Schema.Boolean),
  since: Schema.optional(Schema.Date),
  pageSize: Schema.optional(Schema.Finite),
}).pipe(Schema.encodeKeys({ pageSize: "page_size" }));

export const listTodos = Fetch.endpoint("GET /todos", {
  query: TodoFilters,
  output: Schema.Array(Todo),
});

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

export const program = Effect.gen(function* () {
  const api = yield* Api;
  const list = api.bind(listTodos);
  // GET /todos?done=false&since=2026-01-01T00%3A00%3A00.000Z&page_size=20
  return yield* list({
    query: { done: false, since: new Date("2026-01-01"), pageSize: 20 },
  });
});
```

A query schema is usually a `Schema.Struct`, as here. A `Schema.Class`
works too, and then each call's `query` is an instance of the class.

Every field in `TodoFilters` is optional, so `query` is optional too, and
`list()` sends `GET /todos`. When the schema has a required field, every
call has to pass `query`, and TypeScript says so.

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

## How each field is encoded

The schema encodes each field to a string, as Effect's
`Schema.toCodecStringTree` does:

- A `Date` becomes its ISO string, and a boolean `"true"` or `"false"`. A
  number is written as `String()` writes it, such as `20` or `0.5`.
- An array repeats the key, as in `tag=work&tag=home`.
- Rename keys with `Schema.encodeKeys`, as in the example.
- A field you leave out isn't sent. A `null` is sent as the string `"null"`,
  unlike in a plain query, where it's left out. So prefer optional fields
  to nullable ones.
- A `Schema.Redacted` field is sent as its value. With Effect 4.1 or later,
  it stays redacted in Effect's logs and traces.

A schema whose encoding needs a service adds that service to the call's
requirements, the way an `input` schema's does. Provide it where you run
the call. Such an endpoint's `url` can't be called, since `url` encodes
synchronously (see [Build an endpoint's URL](https://effetch.monarcode.dev/docs/endpoints/urls)).

## Queries that don't match

A schema or a query that can't be encoded fails early, at one of two
points:

- **When the endpoint is defined.** A schema with a field that has no
  string encoding, such as `Schema.instanceOf(Uint8Array)`, throws there,
  so the mistake shows up when the module is imported.
- **When the call runs.** 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, with an `Error` whose message starts with
  `Invalid query:`. See
  [Logging and defects](https://effetch.monarcode.dev/docs/errors/logging-and-defects).

## What's next

- [Build an endpoint's URL](https://effetch.monarcode.dev/docs/endpoints/urls): get the URL, query
  included, without sending a request.
- [Call an endpoint](https://effetch.monarcode.dev/docs/endpoints/calling): see everything a call takes.
- [Make a call](https://effetch.monarcode.dev/docs/calls#build-a-query-string): send a plain query from
  a method call.