# Build an endpoint's URL (https://effetch.monarcode.dev/docs/endpoints/urls)

Sometimes you need the URL of a call rather than its response: for a link,
an `<img>` source, a download or a cache key. Writing it by hand repeats the
path, and can drift from what the call sends. This page shows how to get
the URL from the endpoint itself, built the same way the call builds it.

## Get the URL

`endpoint.url(options)` returns the URL that a call with the same `params`
and `query` would go to. It works on a definition and on a bound endpoint:

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

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

const getFile = Fetch.endpoint("GET /files/:name", {
  query: Schema.Struct({ download: Schema.optional(Schema.Boolean) }),
});

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

// "/files/report.pdf?download=true"
export const path = getFile.url({
  params: { name: "report.pdf" },
  query: { download: true },
});

export const link = Effect.gen(function* () {
  const api = yield* Api;
  // "https://api.example.com/files/report.pdf?download=true"
  return api.bind(getFile).url({
    params: { name: "report.pdf" },
    query: { download: true },
  });
});
```

`url` takes `params` and `query` as a call does, and TypeScript checks them
the same way. When neither is required, you can call it with no arguments.

## What the URL contains

Where the URL starts depends on what you call `url` on:

- **On a bound endpoint,** the URL starts with the client's `baseUrl`. When
  the client has no `baseUrl`, it's relative, as in `/files/report.pdf`.
- **On a definition from `Fetch.endpoint`,** which has no client, it's the
  path alone, unless the route is a full URL. That makes it a stable cache
  key for one backend, whichever client calls it.
- **For a route written as a full URL,** such as
  `"GET https://cdn.example.com/files/:name"`, it's that URL in both cases.

The URL is built before plugins run, so anything a plugin adds, such as a
token in the query, isn't in it.

## When url throws

`url` runs synchronously and returns a string, so where a call would die,
`url` throws instead. It throws when:

- a path parameter is missing;
- the query doesn't match the endpoint's `query` schema, or a field encodes
  to an object;
- the URL can't be built from the base URL and the path.

An endpoint whose query schema needs a service to encode can't be encoded
synchronously, so calling its `url` doesn't compile.

`url` builds the URL with Effect's URL helpers, as each call does, and they
use the platform's `URL`. On React Native without Expo, that needs a
polyfill (see [Platform notes](https://effetch.monarcode.dev/docs/platforms#react-native)).

## What's next

- [Typed query parameters](https://effetch.monarcode.dev/docs/endpoints/typed-query): see how the query
  is encoded.
- [Write a plugin](https://effetch.monarcode.dev/docs/plugins/writing): read the route a call goes to
  from a plugin.
- [Call an endpoint](https://effetch.monarcode.dev/docs/endpoints/calling): send the request the URL
  describes.