Lesson 8 / 30

Route Parameters & Query

Capture dynamic values from URL paths and query strings.

Path parameters

Colon syntax (:param) captures URL segments. Access them via request.params.

Params and query together

A single route can extract both path parameters and query strings.

fastify.get('/users/:userId/posts/:postId', async (req) => {
  const { userId, postId } = req.params;
  const { page = '1', limit = '10' } = req.query;
  
  return {
    userId,
    postId,
    pagination: { page: Number(page), limit: Number(limit) }
  };
});

Query strings are always strings

Remember to cast query params to their intended types (Number, Boolean, etc.) before using them.