Skip to main content


next-api-decorators is technically compatible with all Express.js middlewares. However, keep in mind that some middlewares may not be compatible with Next.js API routes. When using a 3rd party middleware in a Next.js API handler, it's advised to test the middleware thoroughly.

We provide the @UseMiddleware decorator to run a middleware before the handler. You can use the decorator either for a class or a class method

Applying a middleware

const rateLimiter = rateLimit();

class ArticleHandler {
public articles() {
return 'My articles';

Custom middleware decorators

In some cases, it may be beneficial to create a middleware decorator and use it throughout your app.

We provide the createMiddlewareDecorator function for you to create a decorator that fulfills your needs.

const JwtAuthGuard = createMiddlewareDecorator(
(req: NextApiRequest, res: NextApiResponse, next: NextFunction) => {
if (!validateJwt(req)) {
throw new UnauthorizedException();
// or
return next(new UnauthorizedException());


class SecureHandler {
public securedData(): string {
return 'Secret data';

NextFunction type is exported from next-api-decorators.