File

src/auth/api-keys.controller.ts

Prefix

api-keys

Index

Methods

Methods

Async create
create(orgId: string, userId: string, dto: CreateApiKeyDto)
Decorators :
@Post()
@ApiOperation({summary: 'Create a new API key', description: 'Generates a new API key for external integrations. The raw key is returned ONCE in the response and cannot be retrieved again.'})
@ApiResponse({status: 201, description: 'API key created. Raw key returned once.'})
Parameters :
Name Type Optional
orgId string No
userId string No
dto CreateApiKeyDto No
Returns : unknown
Async list
list(orgId: string)
Decorators :
@Get()
@ApiOperation({summary: 'List all API keys', description: 'Returns all API keys for the organization. Only the prefix is shown, not the full key.'})
Parameters :
Name Type Optional
orgId string No
Returns : unknown
Async revoke
revoke(id: string, orgId: string)
Decorators :
@Delete(':id')
@HttpCode(HttpStatus.OK)
@ApiOperation({summary: 'Revoke an API key'})
@ApiResponse({status: 200, description: 'API key revoked.'})
Parameters :
Name Type Optional
id string No
orgId string No
Returns : unknown
import {
  Controller,
  Post,
  Get,
  Delete,
  Body,
  Param,
  UseGuards,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import {
  ApiTags,
  ApiBearerAuth,
  ApiOperation,
  ApiResponse,
} from '@nestjs/swagger';
import { JwtAuthGuard } from './guards/jwt-auth.guard';
import { CurrentUser } from './decorators/current-user.decorator';
import { ApiKeysService } from './api-keys.service';
import { CreateApiKeyDto } from './dto/create-api-key.dto';

@ApiTags('API Keys')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Controller('api-keys')
export class ApiKeysController {
  constructor(private readonly apiKeysService: ApiKeysService) {}

  @Post()
  @ApiOperation({
    summary: 'Create a new API key',
    description:
      'Generates a new API key for external integrations. The raw key is returned ONCE in the response and cannot be retrieved again.',
  })
  @ApiResponse({ status: 201, description: 'API key created. Raw key returned once.' })
  async create(
    @CurrentUser('organizationId') orgId: string,
    @CurrentUser('id') userId: string,
    @Body() dto: CreateApiKeyDto,
  ) {
    const expiresAt = dto.expiresAt ? new Date(dto.expiresAt) : undefined;
    const result = await this.apiKeysService.createKey(
      orgId,
      userId,
      dto.name,
      dto.permissions,
      expiresAt,
    );
    return {
      message:
        'API key created. Save the key now — it will not be shown again.',
      key: result.key,
      id: result.record.id,
      prefix: result.record.prefix,
      name: result.record.name,
      permissions: result.record.permissions,
    };
  }

  @Get()
  @ApiOperation({
    summary: 'List all API keys',
    description:
      'Returns all API keys for the organization. Only the prefix is shown, not the full key.',
  })
  async list(@CurrentUser('organizationId') orgId: string) {
    return this.apiKeysService.listKeys(orgId);
  }

  @Delete(':id')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: 'Revoke an API key' })
  @ApiResponse({ status: 200, description: 'API key revoked.' })
  async revoke(
    @Param('id') id: string,
    @CurrentUser('organizationId') orgId: string,
  ) {
    await this.apiKeysService.revokeKey(id, orgId);
    return { message: 'API key revoked successfully' };
  }
}

results matching ""

    No results matching ""