ConfigService
Provides typed access to configuration values loaded by ConfigModule.
Syntax
configService.get<T>(key, default?) | configService.getOrThrow<T>(key) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
key | string | No | Dotted path of the configuration value. |
default | T | No | Optional fallback when the key is missing. |
Returns
T — The resolved configuration value.
Throws
Error— getOrThrow throws when the key is undefined.
Examples
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
@Injectable()
export class AppService {
constructor(private config: ConfigService) {}
get port(): number {
return this.config.get<number>('PORT', 3000);
}
get dbUrl(): string {
return this.config.getOrThrow<string>('DATABASE_URL');
}
}
Notes
Use the generic type parameter for type-safe reads and getOrThrow() for
required values. With `infer: true` and typed config factories, the
service can return precisely typed nested values.