DEV Community

Sarwar Hossain
Sarwar Hossain

Posted on

đź§­ How to Exclude a Route from Global Prefix & Versioning in NestJS

In NestJS apps, it's common to use global route prefixes and versioning like /api/v1. But sometimes you need a public-facing endpoint — such as a payment gateway callback — that must live outside those constraints.

Here’s how to expose routes like /payment/callback without the global /api/v1 prefix.

đź”§ Problem
You're using global prefixing and versioning like this:

main.ts:

app.setGlobalPrefix('api');
app.enableVersioning({
  type: VersioningType.URI,
  defaultVersion: '1',
});

Enter fullscreen mode Exit fullscreen mode

âś… Resulting in routes like:

/api/v1/auth/login

/api/v1/user/profile

❌ But now you need /payment/callback to be exposed at:

http://localhost:3000/payment/callback

Instead of:

http://localhost:3000/api/v1/payment/callback

âś… Solution Overview
You need to do two things:

â›” Exclude the route from the global prefix

â›” Mark the route as version-neutral

  1. Exclude the Route from Global Prefix Update main.ts:
import { RequestMethod } from '@nestjs/common';

app.setGlobalPrefix('api', {
  exclude: [{ path: 'payment/callback', method: RequestMethod.GET }],
});

Enter fullscreen mode Exit fullscreen mode

This tells NestJS to ignore prefixing for GET /payment/callback

đźš« No leading slash in path (correct: payment/callback)

  1. Mark Route as Version-Neutral Update your controller:
import { Controller, Get, Version, VERSION_NEUTRAL } from '@nestjs/common';

@Controller('payment')
export class PaymentController {
  @Get('callback')
  @Version(VERSION_NEUTRAL)
  callback() {
    return 'Payment callback hit!';
  }
}

Enter fullscreen mode Exit fullscreen mode

✅ VERSION_NEUTRAL tells NestJS: “do not apply any version prefix”

đź§Ş Final Result
http://localhost:3000/payment/callback âś…

http://localhost:3000/api/v1/payment/callback ❌

All other routes still work normally under /api/v1.

đź§µ Conclusion
NestJS makes it easy to maintain clean API versioning — but when you need an exception like a public payment callback, it only takes

two steps:

  • Exclude the path in setGlobalPrefix()
  • Add @version(VERSION_NEUTRAL) to the controller

Self-Service Infrastructure, No Chaos

Self-Service Infrastructure, No Chaos

Learn to deliver user-friendly self-service infrastructure with Spacelift. This webinar covers Blueprints, automation, and API-first design. Perfect for developers and automation enthusiasts.

Learn More

Top comments (0)

Gen AI apps are built with MongoDB Atlas

Gen AI apps are built with MongoDB Atlas

MongoDB Atlas is the developer-friendly database for building, scaling, and running gen AI & LLM apps—no separate vector DB needed. Enjoy native vector search, 115+ regions, and flexible document modeling. Build AI faster, all in one place.

Start Free

đź‘‹ Kindness is contagious

Explore this practical breakdown on DEV’s open platform, where developers from every background come together to push boundaries. No matter your experience, your viewpoint enriches the conversation.

Dropping a simple “thank you” or question in the comments goes a long way in supporting authors—your feedback helps ideas evolve.

At DEV, shared discovery drives progress and builds lasting bonds. If this post resonated, a quick nod of appreciation can make all the difference.

Okay