ExecutionContext

Provides details about the current request pipeline to guards and interceptors.

Since NestJS 10/11 Spec ↗

Syntax

context.switchToHttp() | context.getHandler() | context.getClass()

Returns

ExecutionContext — An object extending ArgumentsHost with handler and class accessors.

Examples

import {
  Injectable, CanActivate, ExecutionContext,
} from '@nestjs/common';

@Injectable()
export class AuthGuard implements CanActivate {
  canActivate(context: ExecutionContext): boolean {
    const req = context.switchToHttp().getRequest();
    return Boolean(req.headers.authorization);
  }
}

Notes

ExecutionContext extends ArgumentsHost and adds getHandler() and getClass(), which Reflector uses to read route metadata. switchToHttp(), switchToRpc(), and switchToWs() expose the transport-specific objects.