Skip to main content
Version: v10.5.0

Parameters

URL parameters are the dynamic segments of an endpoint path, such as the 1 in /users/1. In HyperFetch you write them as :name in the endpoint, and TypeScript requires a value for each one before the request can be sent. For the ?key=value part of the URL, see query params.

What you'll learn
  1. How to declare params in the endpoint.
  2. How to set them with setParams or pass them to send().
  3. What happens when params are set twice.
  4. How param values are written into the URL.

Declare params in the endpoint​

Prefix a path segment with :. HyperFetch reads the names from the endpoint string, so no generic is needed. Values are string | number.

src/api/products.ts
export const getProduct = client.createRequest<{ response: Product }>()({
endpoint: "/categories/:categoryId/products/:productId",
});

A param name starts with a letter or _. The colon of a port (http://localhost:3000/users/:userId) or of a GraphQL variable like $id: ID! is not read as a param. Keep the endpoint a string literal: a widened string endpoint turns the checks off.


Set params with setParams​

setParams returns a new request with the values filled into the endpoint. Use it to prepare a request once and send it from several places.

src/app.ts
const request = getProduct.setParams({ categoryId: 12, productId: "Z24PBW443" });

console.log(request.endpoint); // "/categories/12/products/Z24PBW443"

await request.send();

TypeScript rejects a missing param, and send() needs params until every param is set:

src/app.ts
getProduct.setParams({ categoryId: 12 }); // productId is missing

await getProduct.send(); // Expected 1 arguments, but got 0

Pass params to send()​

Pass params to send() when the values come from user input or a route at call time. This is the usual choice for one request sent with many values.

src/app.ts
const { data } = await getProduct.send({
params: { categoryId: 12, productId: "Z24PBW443" },
});

Params set twice​

Once setParams has filled the params, the returned request no longer accepts params in send(), so TypeScript reports a second value. If you bypass the types, the value passed to send() wins for that call.

src/app.ts
const userOne = getUser.setParams({ userId: 1 });

userOne.send({ params: { userId: 2 } }); // params are already set

To send the same request with another value, start from the original request: getUser.setParams({ userId: 2 }).


How values reach the URL​

Each :name segment is replaced with String(value), as it is. Values are not URL-encoded, so encode those that may contain /, ?, # or spaces:

src/app.ts
const getFile = client.createRequest()({ endpoint: "/files/:path" });

await getFile.send({
params: { path: encodeURIComponent("docs/report 2024.pdf") },
});
// GET /files/docs%2Freport%202024.pdf

The filled endpoint is part of the request's cacheKey and queryKey, so GET /users/1 and GET /users/2 are cached and queued separately.


When to use which​

UseWhen
send({ params })Values known at call time, the common case
setParams(...)A prepared request sent from several places, or passed to a hook

Congratulations!

You've learned how to work with URL parameters.

  • You can declare params as :name segments and let TypeScript require them.
  • You can set them with setParams or pass them to send().
  • You can encode values that contain URL characters yourself.

Runnable examples: Basic data fetching and Dependent requests.

URL parameters types
How HyperFetch infers param types from the endpoint.
Query params
Type and serialize the query string.
setParams in the API reference
Every method of the Request class.