Next.js › API Routes

How do you read query parameters and the request body inside an API route?

Beginner

Answer

In the Pages Router, the request object passed to your handler function gives you access to query parameters through req.query and to the submitted data through req.body, letting you build endpoints that respond differently based on what the client sent.
export default function handler(req, res) {
  const { id } = req.query;
  const { name } = req.body;
  res.status(200).json({ id, name });
}
Real-world example A product search API route reads a search term from the query string and returns matching products, letting the frontend build a live search feature.

Common follow-ups: Do you need to parse the body manually or does Next.js do it automatically?;How do you handle different HTTP methods like GET and POST in the same route?