Skip to main content

Route matching

It is possible to use Express.js style route matching with parameters within your handlers. To enable this functionality add the path-to-regexp package to your project by running:

npm install path-to-regexp

Then you can define your routes in your handler like:

// pages/api/user/[[...params]].ts
import { createHandler, Get, Param } from 'next-api-decorators';

class UserHandler {
public list() {
return DB.findAllUsers();

public details(@Param('id') id: string) {
return DB.findUserById(id);

public comments(@Param('userId') userId: string) {
return DB.findUserComments(userId);

public commentDetails(@Param('userId') userId: string, @Param('commentId') commentId: string) {
return DB.findUserCommentById(userId, commentId);

export default createHandler(UserHandler);

File names are important for route matching.


When path-to-regexp package is not installed and route matching is being used in handlers, the request will be handled by the method defined with the / path (keep in mind that using @Get() and @Get('/') do exactly the same thing).

For the above example, a request to api/user/123 will be handled by the list method if path-to-regexp package is not installed in your project.