Skip to content

Tutorial - First Steps

This tutorial shows you how to use Django Ninja with most of its features.

This tutorial assumes that you know at least some basics of the Django Framework, like how to create a project and run it.


pip install django-ninja


It is not required, but you can also put ninja to INSTALLED_APPS. In that case the OpenAPI/Swagger UI (or Redoc) will be loaded (faster) from the included JavaScript bundle (otherwise the JavaScript bundle comes from a CDN).

Create a Django project

Start a new Django project (or if you already have an existing Django project, skip to the next step).

django-admin startproject myproject

Create the API

Let's create a module for our API. Create an file in the same directory location as your Django project's root

from ninja import NinjaAPI

api = NinjaAPI()

Now go to and add the following:

from django.contrib import admin
from django.urls import path
from .api import api

urlpatterns = [
    path("api/", api.urls),

Our first operation

Django Ninja comes with a decorator for each HTTP method (GET, POST, PUT, etc). In our file, let's add in a simple "hello world" operation.

from ninja import NinjaAPI

api = NinjaAPI()

def hello(request):
    return "Hello world"

Now browsing to localhost:8000/api/hello will return a simple JSON response:

"Hello world"


Continue on to Parsing input.