Django Ninja

1.6.2 · active · verified Sat Apr 11

Django Ninja is a fast, modern web framework for building APIs with Django using Python type hints, Pydantic, and automatic OpenAPI documentation. It aims for high performance and developer friendliness, similar to FastAPI, while leveraging Django's ecosystem. The library maintains a regular release cadence with frequent updates and bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates creating a basic API endpoint. Define a `NinjaAPI` instance, then use decorator functions (e.g., `@api.get`) to define API routes. Parameters with type hints are automatically validated and converted. The API instance's `.urls` should be included in your Django project's `urlpatterns`.

from django.contrib import admin
from django.urls import path
from ninja import NinjaAPI

# In your Django project's main urls.py:
# from .api import api # Assuming api.py is next to urls.py
# urlpatterns = [
#     path('admin/', admin.site.urls),
#     path('api/', api.urls) # Mount the NinjaAPI
# ]

# Create an api.py file in your Django project's root directory (next to urls.py)
# api.py
api = NinjaAPI(title="My Awesome API", version="1.0.0")

@api.get("/hello")
def hello(request):
    return "Hello from Django Ninja!"

@api.get("/add")
def add(request, a: int, b: int):
    return {"result": a + b}

view raw JSON →