Security schemes — OpenAPI · NestJS

Tell callers how to authenticate: declare the staff's bearer JWT and the partners' API key as security schemes, mark each operation with the scheme it needs, document the sign-in exchange, and declare the 401 every operation can answer.

What you will learn

Read the theory for Security schemes

All OpenAPI lessons

All NestJS courses

loading types…

What you'll learn

  • Explain what a security scheme is in OpenAPI (a named way to authenticate, declared once) and what a security requirement on an operation says
  • Declare the built-in schemes with addBearerAuth, addBasicAuth, addCookieAuth and addOAuth2, and any other with addSecurity
  • Mark an operation with @ApiBearerAuth() or @ApiSecurity(name), so the Swagger UI's Authorize button sends the credential and generated clients know to
  • Document the sign-in exchange itself: the credentials as a DTO with examples, the token as a response model, 401 for the wrong password