Swagger UI — Interactive API Documentation from OpenAPI Specs
In this tutorial, you will learn about Swagger UI. We cover key concepts, practical examples, and best practices to help you master this topic.
Swagger UI is a tool that generates interactive HTML documentation from OpenAPI specifications, providing a visual interface for exploring and testing API endpoints.
What You'll Learn
- Setting up Swagger UI for your API
- Customizing the Swagger UI theme and branding
- Hosting Swagger UI with various deployment options
Why It Matters
Swagger UI is the most popular API documentation tool. It turns your OpenAPI spec into an interactive playground that developers can use immediately.
Code Examples
<!-- Self-hosted Swagger UI -->
<!DOCTYPE html>
<html>
<head>
<title>API Documentation</title>
<link rel="stylesheet"
href="https://unpkg.com/swagger-ui-dist@5/swagger-ui.css">
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://unpkg.com/swagger-ui-dist@5/swagger-ui-bundle.js">
</script>
<script>
SwaggerUIBundle({
url: "/openapi.yaml",
dom_id: "#swagger-ui",
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIBundle.SwaggerUIStandalonePreset
],
layout: "StandaloneLayout",
supportedSubmitMethods: ["get", "post"]
});
</script>
</body>
</html>
// Swagger UI with custom configuration
const ui = SwaggerUIBundle({
url: "/openapi.yaml",
dom_id: "#swagger-ui",
deepLinking: true,
displayOperationId: true,
defaultModelsExpandDepth: 1,
defaultModelExpandDepth: 1,
docExpansion: "list",
filter: true,
showExtensions: true,
showCommonExtensions: true,
tryItOutEnabled: true
});
# Flask serving Swagger UI
from flask import Flask, send_from_directory
app = Flask(__name__)
@app.route('/docs')
def swagger_ui():
return send_from_directory('static', 'swagger.html')
@app.route('/openapi.yaml')
def openapi_spec():
return send_from_directory('static', 'openapi.yaml')
Common Mistakes
1. Using Outdated Swagger UI Version
Always use the latest Swagger UI for security fixes and new features.
2. Not Enabling Try-It-Out
The interactive console is Swagger UI's killer feature. Enable it.
3. Missing CORS Configuration
Swagger UI needs CORS headers to make test requests to your API.
4. No Authentication Support
Configure Swagger UI to support your API's auth methods.
5. Serving Spec from Different Origin
OpenAPI spec and Swagger UI must be served from the same origin or with CORS.
Practice Questions
- What format does Swagger UI read for documentation?
- How do you enable the Try-It-Out feature?
- What is required for Swagger UI to make test API calls?
- How do you customize Swagger UI branding?
- What is the difference between Swagger UI and Redoc?
Answers:
- OpenAPI (YAML or JSON) specification.
- It's enabled by default in modern versions.
- The API must support CORS or be on the same origin.
- By passing configuration options or using CSS overrides.
- Swagger UI is interactive; Redoc is a static, clean, non-interactive viewer.
Challenge: Set up Swagger UI for a sample API. Customize the theme colors, enable deep linking, and add authentication support.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro