Introduction
In the last lesson, you took the first crucial step in documenting your API by installing the necessary Swagger packages and creating a basic configuration. You also defined a reusable schema for your data model, which will save a lot of time later.
This lesson focuses on refining the high-level information that describes your API. According to the OpenAPI standard, this is handled by the info and servers objects in your configuration. While we added a basic version of these in the last lesson, we'll now elevate them to a professional, production-ready standard. For your exam, having clean, configurable, and accurate API metadata is a sign of a well-architected project.
Specifically, you will learn to:
- Define your API's title, version, and description using environment variables.
- Configure multiple server URLs in your documentation, preparing for both local development and final deployment.
By the end, your Swagger configuration will be more robust, maintainable, and aligned with best practices for real-world applications.
1. The Role of info and servers
Before we modify the code, let's be crystal clear on what these two objects do.
info: This object is the business card of your API. It holds the essential metadata:title: The name of your API.version: The current version of your API. This is critical for version management.description: A brief explanation of what your API does.
servers: This is an array that lists the base URLs where your API can be accessed. This is incredibly useful because it allows anyone using your documentation (including an examiner) to switch between different environments, like your local machine and the live production server, with a single click.
The configuration you write directly generates the header section of your interactive documentation page, as shown below.

2. Professionalizing Configuration with Environment Variables
In the previous lesson, we hardcoded the title and description directly into the swaggerOptions object. This is fine for a quick start, but a much better practice is to manage this information as part of your environment configuration. It centralizes your project's key settings and makes them easy to change without digging through code.
This pattern is a common professional standard and is demonstrated well in the following article.
Building a RESTful API from scratch with Node.js: Part 5
This article from Bronson Dunbar's blog shows an excellent pattern for making your Swagger configuration dynamic by sourcing metadata from your .env file.
Read the sections 'Environment Setup' and 'Swagger Configuration'. Pay close attention to how API_VERSION, API_TITLE, and API_DESCRIPTION are defined in the .env file and then referenced using process.env in the Swagger definition object. You will implement this exact technique.
3. Action: Move API Metadata to .env
Let's apply this pattern to your project.
-
Update your
.envfile:
Open your.envfile and add the following variables. Feel free to customize the values.# .env # ... your existing MONGO_URI and PORT # Swagger API Info API_TITLE=CRUD API for Certification API_VERSION=1.0.0 API_DESCRIPTION=A RESTful API for managing items, built for the backend certification exam. -
Update your Swagger Configuration:
Now, open your main server file (e.g.,src/server.js) and modify theinfoobject withinswaggerOptionsto read fromprocess.env.// src/server.js // ... other code const swaggerOptions = { definition: { openapi: '3.0.0', info: { title: process.env.API_TITLE, // Use environment variable version: process.env.API_VERSION, // Use environment variable description: process.env.API_DESCRIPTION, // Use environment variable }, // ... servers and components will be here }, apis: ['./src/routes/*.js'], }; // ... rest of the file
This change makes your documentation's metadata easily configurable and separates it from your application logic.
4. Configuring for Development and Production Servers
Your API will exist in at least two places: on your local machine (localhost) for development, and on a live server (like Render or Railway) for production. Your documentation should reflect this.
The servers property is an array, meaning you can list multiple server URLs. Let's configure it for both environments. We'll use a placeholder for the production URL for now, which you will replace with your actual live URL during the deployment lesson.
// Inside your swaggerOptions.definition object
servers: [
{
url: `http://localhost:${process.env.PORT || 5001}`,
description: 'Development server',
},
{
url: 'https://your-app-name.onrender.com', // << REPLACE THIS during deployment
description: 'Production server',
},
],
5. Finalizing Your Base Configuration
Let's put everything together. Your complete swaggerOptions object should now look like this. Make sure it is placed in your main server file (src/server.js or app.js) before your routes and error handlers.
Action: Verify Your Final Configuration
Replace your existing swaggerOptions with the code below to ensure everything is correct.
// src/server.js or app.js
// ... other imports
const swaggerUi = require('swagger-ui-express');
const swaggerJSDoc = require('swagger-jsdoc');
// ...
const app = express();
// ...
// --- Swagger Configuration ---
const swaggerOptions = {
definition: {
openapi: '3.0.0',
info: {
title: process.env.API_TITLE,
version: process.env.API_VERSION,
description: process.env.API_DESCRIPTION,
},
servers: [
{
url: `http://localhost:${process.env.PORT || 5001}`,
description: 'Development server',
},
{
url: 'https://your-app-name.onrender.com', // Placeholder for production
description: 'Production server',
},
],
// The components/schemas part you added in the last lesson goes here
components: {
/* Your schemas from previous lesson */
}
},
apis: ['./src/routes/*.js'], // Path to the files containing API definitions
};
const swaggerSpec = swaggerJSDoc(swaggerOptions);
// --- Serve Swagger Docs ---
// This should be placed *before* your API routes
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
// ... your API routes, 404 handler, and error handler
Now, restart your server (npm run dev) and refresh the http://localhost:5001/api-docs page in your browser. You should see:
- The title, version, and description now reflect the values from your
.envfile. - A new "Servers" dropdown menu is visible, allowing you to select between your "Development server" and the "Production server" placeholder.
Conclusion
You have now successfully defined a professional and configurable base for your API documentation. This attention to detail is exactly what examiners look for, as it demonstrates an understanding of how real-world applications are managed.
Key Takeaways:
- The
infoandserversobjects are the foundation of your OpenAPI specification, providing essential metadata and API locations. - Storing metadata like title and version in
.envfiles is a best practice that separates configuration from code. - Defining multiple server URLs makes your documentation immediately useful for different environments and is a required step for preparing your project for deployment.
Next Up:
With the base specification fully configured and served at the /api-docs endpoint, you are ready to document your actual API operations. In the next lesson, you will write the documentation for all your CRUD endpoints, bringing your interactive API documentation to life.