Skip to main content

Command Palette

Search for a command to run...

How to auto generate Rest API Docs in Django Rest Framework with Open API schema for your Rest API?

Auto Generate Rest API Documentation in Django

Updated
1 min read
How to auto generate Rest API Docs in Django Rest Framework with Open API schema for your Rest API?
G

Experienced Full Stack Software Engineer with 4+ Years of experience, proficient in Django and React, with expertise in Python, JavaScript and Typescript, also an author of Dev.Junction Blogs & YouTube Channel, expert in Python/Django, Rest Framework, ReactJS/NextJS and TypeScript, currently based in Pune, India.

Note: If you are looking for a Full Stack Developer (Django+React), then connect with me on LinkedIn or contact me through my portfolio.

Let's directly jump into the steps without wasting any time.

  • Open your root urls.py and paste this code at the end of your urls.py file -
from rest_framework.documentation import include_docs_urls

urlpatterns.extend([
    # For autogenerated API documentation
    path(
        "api/docs/",
        include_docs_urls(title="DevJunction API Docs"),
        name='drf-doc'
    ),
])
  • Next step is to allow Django Rest Framework to use Open API schema generator for all the Rest API created using DRF generics views, viewsets or view classes -

    Add this at the end of your settings.py file -

REST_FRAMEWORK = {
    'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema',
}
  • One final step is to install a pip package in your project's virtual environment -
pip install coreapi
  • Yuhuuu! You did it, just run your project in local and open your browser and go this location to see the auto generated API docs - http://localhost:8000/api/docs

For more such crispy blogs, follow DevJunction, subscribe to our newsletter and get notified.

N

Perfect ! This Something I was looking for :)