Skip to content

Version

reference
1 min readUpdated

Kind: Function

Source: packages/common/decorators/core/version.decorator.ts

Part of: Common

Sets the version of the endpoint to the passed version

Version() sets version metadata on an individual endpoint, allowing the routing layer to match requests against a specific API version. Use it with application-level versioning configuration to support multiple versions of the same route without duplicating an entire controller.

Signature

ts
function Version(version: VersionValue): MethodDecorator

Parameters

NameType
versionVersionValue

Returns: MethodDecorator

Diagram

mermaid
graph LR
  A[Client Request<br/>/v1/users] --> B[Versioning Strategy]
  B --> C[Controller Route]
  C --> D["@Version('1') metadata"]
  D --> E[Matching Endpoint Handler]

Usage

ts
import { Controller, Get, Version } from '@nestjs/common';

@Controller('users')
export class UsersController {
  @Get()
  @Version('1')
  findAllV1() {
    return { version: '1', users: [] };
  }

  @Get()
  @Version('2')
  findAllV2() {
    return { version: '2', users: [] };
  }
}

AI Coding Instructions

  • Apply @Version() to route handler methods when only specific endpoints need a different API version.
  • Ensure versioning is enabled during application bootstrap, such as with app.enableVersioning(...).
  • Use version values consistently across controllers, routes, and client-facing API documentation.
  • Avoid defining conflicting handlers with the same HTTP method, route path, and version value.
  • Prefer controller-level versioning when every endpoint in a controller belongs to the same API version.

Used by

4 references from 4 files. Each is a place in this repository where the symbol is actually used — go read one rather than trusting an example.

Imported by (4)

  • AppV2Controllerintegration/inspector/src/app-v2.controller.ts:3
  • MiddlewareControllerintegration/versioning/src/middleware.controller.ts:3
  • OverridePartialControllerintegration/versioning/src/override-partial.controller.ts:3
  • OverrideControllerintegration/versioning/src/override.controller.ts:3

Was this page helpful?

Download as PDF
Version — NestJS head-to-head