A comprehensive, interactive API documentation site built with Swagger UI that showcases a complete RESTful bookstore management system. Features dynamic YAML loading, modern responsive design, and easy deployment to any static hosting platform.
- GitHub Repository:
https://github.com/alokbash/swagger-openapi-sample
The Bookstore API provides a complete solution for managing an online bookstore.
swagger-openapi-sample/
├── index.html # Main Swagger UI page with dynamic YAML loading
├── openapi.yaml # Your OpenAPI 3.x specification
├── package.json # Project metadata and scripts
├── .gitignore # Git ignore patterns
└── README.md # This file
- Node.js 16+ (for development tools)
- A modern web browser
- Git
-
Clone the repository:
git clone https://github.com/alokbash/swagger-openapi-sample.git cd swagger-openapi-sample -
Install development dependencies (optional):
npm install
-
Start local development server:
npm run dev
Or serve manually:
# Using Python python -m http.server 3000 # Using Node.js http-server npx http-server . -p 3000 -c-1 --cors # Using PHP php -S localhost:3000
-
Open your browser to
http://localhost:3000
npm run start # Start development server on port 3000
npm run dev # Start development server and open browser
npm run validate # Validate OpenAPI specification syntax and structure
npm run lint # Lint YAML syntax and formatting-
Start the development server:
npm run dev
-
Open your browser to
http://localhost:3000 -
Explore the API:
- Browse the different endpoint categories (Books, Orders, Users)
- Try out the interactive examples
- Test authentication flows
- Examine request/response schemas
GET /books- List books with filtering, sorting, and paginationGET /books/{bookId}- Get detailed book informationPOST /books- Create new book (admin only)
GET /orders- List user's orders with status filteringPOST /orders- Create new order with items and shippingGET /orders/{orderId}- Get specific order details
POST /users/register- Register new user accountPOST /users/login- Authenticate and get JWT tokenGET /users/profile- Get user profile information
- Edit the OpenAPI spec: Modify
openapi.yamlwith your API endpoints, schemas, and documentation - Validate your changes: Run
npm run validateto ensure valid OpenAPI format - Test locally: The page will automatically reload and display your changes
- Use Swagger Editor: Visit editor.swagger.io for visual editing
This is a static site that can be deployed to any hosting platform:
- GitHub Pages: Enable Pages in repository settings
- Vercel: Connect your GitHub repository
- Netlify: Drag and drop or connect via Git
- Any web server: Upload files to your server
-
Push to GitHub:
git add . git commit -m "Ready for deployment" git push origin main
-
Deploy to your preferred platform:
- Most platforms will automatically detect this as a static site
- No build step required - just serve the files
- Ensure your hosting platform serves YAML files with correct MIME type
Edit the <style> section in index.html to customize:
/* Brand colors */
:root {
--primary-color: #3498db;
--secondary-color: #2ecc71;
--accent-color: #e74c3c;
}
/* Custom styling examples */
.swagger-ui .info .title {
color: var(--primary-color);
font-family: 'Your Brand Font', sans-serif;
}Modify the SwaggerUIBundle configuration in index.html:
SwaggerUIBundle({
spec: spec,
dom_id: '#swagger-ui',
deepLinking: true,
tryItOutEnabled: true,
// Customization options
defaultModelsExpandDepth: 2, // How deep to expand models
docExpansion: 'list', // 'list', 'full', or 'none'
filter: true, // Enable search/filter
showExtensions: true, // Show vendor extensions
showCommonExtensions: true, // Show common extensions
// Theme and layout
layout: "BaseLayout",
// Request interceptor for authentication
requestInterceptor: (request) => {
const token = localStorage.getItem('authToken');
if (token) {
request.headers.Authorization = `Bearer ${token}`;
}
return request;
}
});To support multiple API versions or services:
-
Create additional YAML files:
openapi-v1.yaml # Version 1 API openapi-v2.yaml # Version 2 API admin-api.yaml # Admin API -
Add version switching:
// Get version from URL parameter const urlParams = new URLSearchParams(window.location.search); const version = urlParams.get('version') || 'v1'; const specUrl = `./openapi-${version}.yaml`;
-
Update navigation:
<div class="api-version-selector"> <a href="?version=v1">API v1</a> <a href="?version=v2">API v2</a> <a href="?version=admin">Admin API</a> </div>
Add your company branding:
<!-- Add to index.html head -->
<link rel="icon" href="./favicon.ico">
<meta property="og:title" content="Your Company API Documentation">
<meta property="og:description" content="Comprehensive API documentation">
<meta property="og:image" content="./api-preview.png">Symptoms: Error page appears instead of API documentation
Solutions:
- ✅ Ensure
openapi.yamlexists in the project root - ✅ Validate YAML syntax:
npm run lint - ✅ Check browser console for detailed errors (F12)
- ✅ Verify file is accessible via HTTP (not file:// protocol)
- ✅ Test with a simple HTTP server:
npm run start
Symptoms: "Access blocked by CORS policy" errors
Solutions:
- ✅ Always use an HTTP server (not file:// protocol)
- ✅ Use provided npm scripts:
npm run dev - ✅ For custom servers, enable CORS headers
- ✅ Check that YAML file is served with correct MIME type
Symptoms: Site doesn't deploy or shows errors
Solutions:
- ✅ Check deployment logs in your hosting platform's dashboard
- ✅ Ensure all files are committed to Git:
git status - ✅ Verify YAML files are served with correct MIME type
- ✅ Check routing configuration for your hosting platform
- ✅ Validate OpenAPI spec before deployment:
npm run validate
Symptoms: "No layout defined" errors
Solutions:
- ✅ Use correct layout names:
"BaseLayout"or"StandaloneLayout" - ✅ Check Swagger UI version compatibility
- ✅ Verify CDN links are accessible
Symptoms: Poor mobile experience
Solutions:
- ✅ Ensure viewport meta tag is present
- ✅ Test responsive design with browser dev tools
- ✅ Check CSS media queries in custom styles
-
Check Browser Console (F12):
Look for JavaScript errors, network failures, or CORS issues -
Validate OpenAPI Spec:
npm run validate npm run lint
-
Test Local Server:
npm run start # Then visit http://localhost:3000 -
Check Network Tab:
Verify openapi.yaml loads successfully (200 status) Check response headers and content type