Mock Config logoMock config

Path parameters

Match and type REST path parameters in Mock Config.

Path parameters let you capture dynamic parts of a REST path, like user ids, slugs, or nested resource identifiers. In Mock Config, path parameters are declared directly in the route path and become available on request.params inside your handler.

Declaring Parameters

Use :name to declare a parameter in the route path:

import { rest } from 'mock-config-server';

type User = {
  id: number;
  name: string;
};

const route = rest.get<{ params: { id: string }; response: User }>('/users/:id', ({ request }) => {
  const id = request.params.id;

  return {
    id: Number(id),
    name: 'John'
  };
});

The incoming GET /api/users/42 request matches /users/:id, and the handler receives { id: '42' } in request.params.

Multiple Parameters

You can declare more than one path parameter in the same route.

import { rest } from 'mock-config-server';

const route = rest.get<{
  params: {
    userId: string;
    postId: string;
  };
}>('/users/:userId/posts/:postId', ({ request }) => {
  const { userId, postId } = request.params;

  return {
    userId: Number(userId),
    postId: Number(postId)
  };
});

Each parameter is extracted from the matching path segment and passed to the handler as a string.

Parameters In Matchers

If you want different scenarios for the same dynamic route, use Matcher and match specific parameter values.

import { rest } from 'mock-config-server';

const route = rest.get<{ params: { id: string } }>(
  '/users/:id',
  {
    id: 1,
    name: 'John'
  },
  {
    match: {
      params: {
        id: '1'
      }
    }
  }
);

This lets you keep the real route shape while still returning different responses depending on the captured parameter values.

Why Parameters Stay Strings

Path parameters come from the URL, so they are received as strings by default. If your application expects numbers, convert them explicitly in the handler:

const userId = Number(request.params.id);

This keeps the mock behavior aligned with real HTTP routing and makes type conversions explicit.

On this page