liveBook · Manning

NinjaAPI

As explained in Django in Action

NinjaAPI is Django Ninja's primary API class for registering endpoints and configuring API behavior such as authentication.

NinjaAPI is Django Ninja’s central API object: create it once for an API, attach endpoint groups through Router objects, and expose the result through its urls property in Django’s URL configuration. Think of it as the API’s switchboard rather than an individual endpoint. Routers collect related routes, while HTTP-method decorators turn ordinary view functions into API endpoints. section 11.2 section 11.5

A typical registration flow looks like this:

  • Create a Router in an app’s api.py file and decorate a view function with an HTTP method such as .get(). section 11.2
  • Instantiate NinjaAPI, normally once per API, optionally supplying a version such as "1.0". section 11.2
  • Register each router with api.add_router(), giving it a URL prefix such as /home/. section 11.2
  • Mount api.urls beneath a Django path, producing a versioned base route such as /api/v1/. section 11.5

RiffMates API wiring

A small RiffMates API can place its home endpoint in home/api.py, where home_router handles GET requests. The project URL configuration then creates api = NinjaAPI(version="1.0"), registers home_router at /home/, and mounts api.urls at /api/v1/. A request to the combined route reaches the decorated function and returns its string response.

How Django in Action covers it

How deep it goes
Explains construction, router registration, URL mounting, and versioning, while leaving schemas and endpoint parameter details to related treatment.
Examples
Demonstrates a RiffMates home endpoint, a Router, a versioned NinjaAPI instance, and Django URL patterns.
What it assumes
Assumes Django URL configuration and Python view functions, while showing Ninja installation and endpoint wiring from the beginning.
What you can do after
Build it from scratch by declaring routers, registering them with NinjaAPI, and mounting versioned API URLs in Django.
How it connects
Connects Django URL routing with Router objects, HTTP methods, decorators, REST API versioning, schemas, and authentication.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.