Swagger for REST API :Django rest framework project

Ines Bouguerra Dev Tuto
3 min readAug 7, 2021

--

Develop APIs more comfortably

Swagger is an open source project launched by a startup in 2010. The goal is to implement a framework that will allow developers to document and design APIs, while maintaining synchronization with the code.

While developing the Framework, a Swagger specification was put in place to define the standard for designing and documenting its API. The project has attracted the attention of many developers, and has become the most popular technology used to design and describe Restful APIs.

In January 2016 the Swagger Specification project was renamed Open-API Specification (OAS) and came under the governance of the Linux Foundation.

Developing an API requires orderly and understandable documentation.

Currently, Swagger is the best solution for documenting a REST API. The documentation grows with the system and automatically records changes. Swagger is effective in achieving this because it logs the documentation of a REST API directly in its code.

Swagger: a practical example to get started Swagger with Django rest framework

In this article we will see how to document an API developed in Django rest framework.

Below is the git link of the project developed with Django which we will use to demonstrate the use of Swagger for the REST APIs created.

https://github.com/Ines-Bouguerra/Swagger-First-Blog.git

To document and design APIs with Django rest framework we will use drf-yasg.

drf-yasg generate real Swagger/Open-API 2.0 specifications from a Django Rest Framework API.

You can find the documentation here.

After cloning the Swagger-First-Blog project you can now configure Swagger by simply installing drf-yasg with the command

pip install drf-yasg

Once the drf-yasg package is installed you need to add it in settings.py .

The next step is to add this bot code in urls.py .

now run the server and test your APIs .

You can now try using Swagger with Django rest framework easily .

I would love to hear some feedback from others about this setup: how could I potentially improve it, anything that needs changing or could be done better, etc.

--

--