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.
