@SetMetadata()

Attaches custom metadata to a route or controller for later retrieval via Reflector.

Since NestJS 10/11 Spec ↗

Syntax

@SetMetadata(key: string, value: any)

Parameters

NameTypeRequiredDescription
key string No Metadata key used to read the value back with Reflector.
value any No Arbitrary metadata payload to associate with the handler or class.

Returns

ClassDecorator | MethodDecorator — A decorator applied at controller or handler level.

Examples

import { SetMetadata } from '@nestjs/common';

export const Roles = (...roles: string[]) => SetMetadata('roles', roles);
import { Controller, Get } from '@nestjs/common';
import { Roles } from './roles.decorator';

@Controller('admin')
export class AdminController {
  @Get()
  @Roles('admin')
  dashboard() {
    return 'secret';
  }
}

Notes

@SetMetadata() is typically wrapped in a named custom decorator for readability. A guard then reads it via Reflector.getAllAndOverride() to implement authorization rules.