Open In App

Swagger Extensions

Last Updated : 11 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Swagger Extensions are a pivotal component in augmenting the capabilities of Swagger, a robust API development tool. This article delves into the significance of Swagger Extensions, providing insights into their functionality and how they contribute to crafting comprehensive and informative API documentation.

What are Swagger Extensions?

Swagger Extensions, often referred to as vendor extensions, introduce custom properties marked with `x-` in your Swagger documents. These extensions allow developers to describe additional functionality beyond the standard Swagger specification. In simpler terms, they provide a way to enrich your API documentation with custom details that may not be covered by default.

Swagger Extensions Examples

Below are some of the examples of swagger extensions in Python:

Swagger x-logo

This extension is used to specify the URL to the API’s logo, which can enhance the visual branding of your API documentation. Here’s an example of how you might use it in a Swagger document:

swagger.py: The below code defines a Swagger/OpenAPI specification for an API with a single endpoint (“/endpoint”) that supports a GET request. It includes information such as the API title, version, and a logo URL. The specification is then saved to a JSON file named ‘swagger.json’ using the json.dump() function.

Python3




import json
 
swagger_definition = {
    "swagger": "2.0",
    "info": {
        "title": "My API",
        "version": "1.0.0",
    },
    "x-logo": "https://example.com/logo.png",
    "paths": {
        "/endpoint": {
            "get": {
                "summary": "Get Data",
                "description": "Retrieves data from the API.",
                "responses": {
                    "200": {
                        "description": "Successful response."
                    }
                }
            }
        }
    }
}
 
with open('swagger.json', 'w') as f:
    json.dump(swagger_definition, f)


swagger.js : below Node.js code uses Express and ‘swagger-ui-express’ to create a server hosting Swagger UI for API documentation. The Swagger/OpenAPI specification is loaded from ‘swagger.json’, and the UI is accessible at ‘http://localhost:3000/api-docs’.

Javascript




const express = require('express');
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');
 
const app = express();
 
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
 
app.listen(3000, () => {
  console.log('Swagger UI is available at http://localhost:3000/api-docs');
});


Output:

{"swagger": "2.0", "info": {"title": "My API", "version": "1.0.0"},
"x-logo": "https://example.com/logo.png",
"paths": {"/endpoint": {"get": {"summary": "Get Data", "description": "Retrieves data from the API.",
"responses": {"200": {"description": "Successful response."}}}}}}

ezgifcom-video-to-gif-converter

x-auth-token

This extension can be used to define an authentication token property. This is useful for documenting authentication details within the Swagger document. Here’s an example:

swagger.py : This Python code generates a Swagger/OpenAPI specification for an API with a secure endpoint (“/secure-endpoint”) that requires an API key (“x-auth-token” in the header) for access. The specification is saved to a JSON file named ‘swagger.json’ using the `json.dump()` function.

Python3




import json
 
swagger_definition = {
    "swagger": "2.0",
    "info": {
        "title": "My API",
        "version": "1.0.0",
    },
    "paths": {
        "/secure-endpoint": {
            "get": {
                "summary": "Secure Endpoint",
                "responses": {
                    "200": {
                        "description": "Successful response."
                    }
                },
                "security": [{
                    "api_key": []
                }]
            }
        }
    },
    "securityDefinitions": {
        "api_key": {
            "type": "apiKey",
            "in": "header",
            "name": "x-auth-token"
        }
    }
}
 
 
with open('swagger.json', 'w') as f:
    json.dump(swagger_definition, f)


swagger.js : This Node.js code uses Express and ‘swagger-ui-express’ to create a server that serves Swagger UI for API documentation. The Swagger/OpenAPI specification is provided from the ‘swagger.json’ file, and the UI is accessible at ‘http://localhost:3000/api-docs’ after starting the server on port 3000.

Javascript




const express = require('express');
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');
 
const app = express();
 
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
 
app.listen(3000, () => {
  console.log('Swagger UI is available at http://localhost:3000/api-docs');
});


Output:

{"swagger": "2.0", "info": {"title": "My API", "version": "1.0.0"}, 
"paths": {"/secure-endpoint": {"get": {"summary": "Secure Endpoint",
"responses": {"200": {"description": "Successful response."}}, "security": [{"api_key": []}]}}}, "securityDefinitions": {"api_key": {"type": "apiKey",
"in": "header", "name": "x-auth-token"}}}

ezgifcom-video-to-gif-converter

In this example, `x-auth-token` is used to define an authentication token property.

x-description

This extension can be used to add a detailed description to an API endpoint, enabling the inclusion of comprehensive explanations for specific endpoints. Here’s an example:

swagger.py : This Python code generates a Swagger/OpenAPI specification for an API with a detailed endpoint (“/detailed-endpoint”) that includes a summary and a multiline description. The specification is saved to a JSON file named ‘swagger.json’ using the json.dump() function

Python3




import json
 
swagger_definition = {
    "swagger": "2.0",
    "info": {
        "title": "My API",
        "version": "1.0.0",
    },
    "paths": {
        "/detailed-endpoint": {
            "get": {
                "summary": "Detailed Endpoint",
                "x-description": """
                    This endpoint provides detailed information.
                    You can use it for various purposes.
                """,
                "responses": {
                    "200": {
                        "description": "Successful response."
                    }
                }
            }
        }
    }
}
 
with open('swagger.json', 'w') as f:
    json.dump(swagger_definition, f)


swagger.js : This Node.js code sets up an Express server to host Swagger UI for API documentation. It utilizes the ‘swagger-ui-express’ middleware to render the Swagger UI interface, and the Swagger/OpenAPI specification is loaded from the ‘swagger.json’ file.

Javascript




const express = require('express');
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');
 
const app = express();
 
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
 
app.listen(3000, () => {
  console.log('Swagger UI is available at http://localhost:3000/api-docs');
});


Output:

{"swagger": "2.0", "info": {"title": "My API", "version": "1.0.0"},
"paths": {"/detailed-endpoint": {"get": {"summary": "Detailed Endpoint",
"x-description": "\n
This endpoint provides detailed information.\n
You can use it for various purposes.\n
", "responses": {"200": {"description": "Successful response."}}}}}}

ezgifcom-video-to-gif-converter

Here, `x-description` adds a detailed description to the `/detailed-endpoint`.

Conclusion

In conclusion, Swagger Extensions stand out as a crucial asset for developers seeking to refine and augment their API documentation. Their utility extends beyond the standard Swagger specification, providing a flexible framework to tailor documentation according to specific needs. By judiciously exploring the capabilities of Swagger Extensions, developers can craft documentation that is not only well-defined but also easily comprehensible.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads