nestrs supports three versioning strategies that map directly to the NestJS VersioningType options: URI prefix, custom request header, and Accept media-type negotiation. You pick one strategy per application and optionally annotate individual controllers or routes to restrict them to specific versions.
URI versioning
enable_uri_versioning nests all your routes under a version prefix. Requests to /v1/users are routed exactly like requests to /users in an unversioned app:
With this setup a GET /v1/users request matches your UsersController handler for GET /users. The version prefix is applied globally — individual controllers can narrow to a specific version with #[version] (see below).
enable_header_versioning reads the version from a request header (default X-API-Version) and rewrites the internal path before routing:
The second argument is the default version to use when the header is absent. Pass None to treat unversioned requests as unversioned.
You can also pass a full ApiVersioningPolicy through enable_api_versioning for more control:
enable_media_type_versioning parses the version from the Accept header using the version= parameter, for example Accept: application/vnd.api+json;version=2:
Per-controller versioning with #[version]
Annotate a controller struct with #[version("v2")] to bind all its routes to that version. When URI versioning is active, only /v2/... requests are matched. When header or media-type versioning is active, only requests carrying that version are matched:
Per-route versioning with #[ver]
Use #[ver("v2")] on a single handler to make that route available only for the given version, while the rest of the controller remains at the default version:
Reading the resolved version in a handler
The NestApiVersion extractor gives you the resolved version string for the current request. Use it in guards, interceptors, or handler logic:
NestApiVersion is inserted into request extensions by the versioning middleware for header and media-type strategies. For URI versioning the version is part of the path and is not separately injected.
Strategy comparison
URI versioning is the most cache-friendly strategy since each version has a distinct URL. Header and media-type versioning keep URLs stable but require clients to set the right header on every request.