Dynamic API Routes in NextJS

Sayantan Sarkar
2 min readJun 17, 2023

--

Photo from freepik.com

Dynamic API routes are a powerful tool that can be used to improve the functionality and performance of your applications. They allow you to serve different content based on the URL path, which gives you a lot of flexibility in how you architect your application. Dynamic API routes can also be cached by the browser, which can improve performance for frequently accessed pages. This makes them a good choice for applications that expect a lot of traffic.

Here are some examples of how dynamic API routes can be used:

  • A blog application could use dynamic routes to serve a list of posts for a specific user.
  • An e-commerce application could use dynamic routes to return the details for a specific product.
  • A social media application could use dynamic routes to handle user requests, such as liking a post or following a user.

Overall, dynamic API routes are a valuable tool for improving the functionality, performance, and security of your applications.

Next.js is a React framework that makes it easy to build server-rendered web applications.

Syntax:

Dynamic API routes in Next.js follow a file-based routing system. To create a dynamic API route, you need to create a file with a specific naming pattern. For example, a dynamic route for fetching user data based on their ID would be created in a file named [id].js. The square brackets indicate that id is a dynamic parameter that can be accessed within the route handler like this —

- pages/
- api/
- users/
- [id].js

Here's an example of a dynamic API route in Next.js -

// pages/api/users/[id].js
export default function handler(req, res) {
const { id } = req.query;
// Fetch user data based on the ID from a database or external API
// ...
// Return the response
res.status(200).json({ id, name: 'John Doe' });
}

In this example, a dynamic API route is defined under the /pages/api/users directory. The [id] indicates that the id parameter will be dynamic and can be accessed via req.query.id within the route handler function. You can perform database queries or call external APIs to fetch the user data based on the provided ID and return the response as JSON.

When a request is made to /api/users/123, where 123 is the user ID, Next.js will automatically route the request to this dynamic API route and execute the handler function, providing access to the ID as a query parameter.

Dynamic API routes in Next.js can follow various patterns depending on your application’s requirements. Some common patterns include:

  • Fetching individual resources: /api/users/[id].js
  • Filtering or searching resources: /api/products/[category].js
  • Pagination: /api/posts/[page].js
  • Custom endpoints: /api/discount/[code].js

I hope this article helps!

To get more Technology related content-

Follow me on Medium.

Let’s connect on Twitter.

--

--

Sayantan Sarkar

Software Engineer 2 at Lexmark (India), Passionate about Technology, Startup and Love to travel