dev-resources.site
for different kinds of informations.
Seamlessly Integrate Swagger with JWT Authentication in NestJS
Published at
11/3/2024
Categories
nestjs
jwt
swagger
webdev
Author
imzihad21
Author
9 person written this
imzihad21
open
Enhancing your NestJS API with robust documentation and JWT authentication is essential for a smooth developer experience. In this article, we'll walk you through integrating Swagger UI into your NestJS application, utilizing dummy data, and customizing the UI with external assets.
Step 1: Install Required Packages
Start by installing the necessary packages for Swagger support:
npm install @nestjs/swagger
Step 2: Create Swagger Configuration
Create a file (swagger.config.ts
), configure Swagger using the following code snippet with dummy data for demonstration:
import { INestApplication } from "@nestjs/common";
import {
DocumentBuilder,
SwaggerCustomOptions,
SwaggerModule,
} from "@nestjs/swagger";
const documentConfig = new DocumentBuilder()
.setTitle("Dummy API") // API Title
.setVersion("1.0.0") // API Version
.addBearerAuth( // Adds JWT Bearer authentication
{
type: "http",
scheme: "bearer",
bearerFormat: "JWT",
name: "Authorization",
description: "Enter JWT token",
in: "header",
},
"Bearer",
)
.addSecurityRequirements("Bearer") // Security requirement
.build();
const swaggerUiOptions: SwaggerCustomOptions = {
swaggerOptions: {
persistAuthorization: true, // Retain authorization
},
customSiteTitle: "Dummy API Documentation", // Swagger UI title
customJs: [ // External JS files
"https://unpkg.com/[email protected]/swagger-ui-bundle.js",
"https://unpkg.com/[email protected]/swagger-ui-standalone-preset.js",
],
customCssUrl: [ // External CSS files
"https://unpkg.com/[email protected]/swagger-ui.css",
],
};
// Function to configure Swagger UI
export const configureSwaggerUI = (app: INestApplication<any>) => {
const document = SwaggerModule.createDocument(app, documentConfig); // Create Swagger document
SwaggerModule.setup("swagger", app, document, swaggerUiOptions); // Setup Swagger UI
};
Explanation
- Document Configuration: Set up a Swagger document with a title, version, and authentication.
- Swagger UI Options: Configure options, including a custom title and external assets.
Step 3: Integrate Swagger in Your NestJS Application
Add the Swagger configuration in your main.ts
file:
import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
import { configureSwaggerUI } from "./swagger.config"; // Import Swagger configuration
async function bootstrap() {
const app = await NestFactory.create(AppModule);
configureSwaggerUI(app); // Configure Swagger
await app.listen(4000); // Start the application
}
bootstrap();
Step 4: Run Your Application
Now, run your NestJS application:
npm run start
Step 5: Access Swagger UI
Open your browser and navigate to:
http://localhost:4000/swagger
You should see the Swagger UI with your Dummy API documentation!
swagger Article's
30 articles in total
Crudify: Automate Your Mongoose CRUD Operations in NestJS
read article
🚀 API Maker : Release Notes for v1.9.0
read article
What Does Swagger x-nullable Mean?
read article
Two Reasons on Why We Ship LiveAPI with An ROI Calculator
read article
.NET 9 Revolutionizing documentation of APIs : From Swashbuckle to Scalar 🚀
read article
What is the Best Way to Group REST API methods in Swagger UI
read article
How Scale Changes Everything - The LiveAPI Perspective
read article
What is SwaggerHub?
read article
Ever wished to maintain API Docs with ease? Introducing LiveAPI: Super Convenient API Docs
read article
Integrate Swagger UI with Codeigniter4
read article
How to Use Swagger UI Locally: A Step-by-Step Guide
read article
Musings Over What Makes LiveAPI Different (from Swagger Et Cetera)
read article
Missing Required Key in Body of PUT /odata/Assets({Key}) Edit an asset on UiPath.WebApi 18.0
read article
An Online Free API AutoTesting Tool That Completes 160 Hours of Testing Work for 20 APIs in Just 3 Minutes
read article
How to Improve Development Efficiency Through Automated API Testing
read article
Top 8 Swagger Codegen Alternatives
read article
JavaFX In Action #8 with Ulas Ergin: How JavaFX helps to migrate from Swing to React UIs, all combined in one Java app
read article
Exploring AutoAPI: An Automation Tool to Simplify Frontend Development
read article
Django Rest framework with Swagger
read article
Seamlessly Integrate Swagger with JWT Authentication in NestJS
currently reading
New Swagger-UI embedding Cloud TypeScript Editor with RPC SDK
read article
Laravel API Documentation Made Easy: Step-by-Step Swagger Integration
read article
Introducing Swama: A CLI Tool for Swagger/OpenAPI Interactions
read article
StudySpy: Building the new PublicApi v2
read article
Swagger UI + Docker: Initial Setup
read article
apigen-ts – Simple TypeScript API Client Generator
read article
Automating Swagger Documentation with Joi in Node.js: Simplify Your API Documentation Workflow
read article
Merge and bundle open api yaml files for swagger
read article
Gerando Documentação de API Automática com Fastify, @fastify/swagger e Zod
read article
Hosting Swagger-UI using GitHub Pages
read article
Featured ones: