# Setting up Swagger UI in Next.js This guide explains how to set up Swagger UI in a Next.js application using route groups. ## Directory Structure The recommended directory structure for Swagger documentation: ``` app/ (swagger)/ # Route group for swagger-related pages swagger/ # Actual swagger route page.tsx # Swagger UI component ``` ## Installation 1. Add Swagger UI dependencies to your project: ```json { "dependencies": { "swagger-ui-react": "^5.12.0", "swagger-ui-dist": "^5.12.0" }, "devDependencies": { "@types/swagger-ui-react": "^4.18.3" } } ``` 2. Install webpack style loaders for handling Swagger UI CSS: ```bash pnpm add -D style-loader css-loader ``` ## Next.js Configuration Create or update `next.config.js` to handle Swagger UI CSS: ```javascript /** @type {import('next').NextConfig} */ const nextConfig = { transpilePackages: ['swagger-ui-react'], webpack: (config) => { config.module.rules.push({ test: /\.css$/, use: ['style-loader', 'css-loader'], }); return config; }, }; module.exports = nextConfig; ``` ## Swagger UI Component Create `app/(swagger)/swagger/page.tsx`: ```typescript "use client"; import { useEffect } from 'react'; import SwaggerUI from 'swagger-ui-react'; import 'swagger-ui-react/swagger-ui.css'; export default function SwaggerPage() { useEffect(() => { document.title = 'API Documentation - ShortURL Analytics'; }, []); const swaggerConfig = { openapi: '3.0.0', info: { title: 'Your API Title', version: '1.0.0', description: 'API documentation', contact: { name: 'API Support', email: 'support@example.com', }, license: { name: 'MIT', url: 'https://opensource.org/licenses/MIT', }, }, // ... your API configuration }; return (
Explore and test the API endpoints using the interactive documentation below.