Registry / web-framework / django-ninja-jwt

django-ninja-jwt

JSON →
library5.4.4pypypiunverified

Django Ninja JWT provides JSON Web Token (JWT) authentication for Django Ninja, a fast API framework for Django. It handles token creation, refresh, and authentication seamlessly within Django Ninja API routes, building upon `djangorestframework-simplejwt`. The current version is 5.4.4, with a regular release cadence focused on bug fixes and compatibility updates.

pip install django-ninja-jwt
INSTALL
IMPORT
SIG · DJANGO-NINJA-JWT
D
django-ninja-jwt
web-frameworkpythonv5.4.4
Install
6.8s avg
Import
Disk
104MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v5.4.4 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 103.8MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 6.8s · import 0.000s · 103MB
104MB installed
● package 104MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

JWTAuth
from ninja_jwt.authentication import JWTAuth
from ninja_jwt.authentication import JWTAuth

This quickstart demonstrates how to set up `django-ninja-jwt` in a Django project. It covers adding the library to `INSTALLED_APPS`, configuring `SIMPLE_JWT` settings, specifying `AUTHENTICATION_BACKENDS`, and integrating the `AuthRouter` for token management endpoints. It also shows how to protect an API endpoint using `JWTAuth()`.

import os from datetime import timedelta # --- In your Django project's settings.py --- SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY', 'your-very-secret-key-for-development') # IMPORTANT: Use a strong key in production INSTALLED_APPS = [ # ... other apps "django.contrib.auth", "django.contrib.contenttypes", "ninja", # Add django-ninja "ninja_jwt", # Add this # ... ] # Configure JWT settings (mimics djangorestframework-simplejwt settings) SIMPLE_JWT = { "ACCESS_TOKEN_LIFETIME": timedelta(minutes=5), # Short lifetime for access tokens "REFRESH_TOKEN_LIFETIME": timedelta(days=1), # Longer lifetime for refresh tokens "ROTATE_REFRESH_TOKENS": False, "BLACKLIST_AFTER_ROTATION": False, "UPDATE_LAST_LOGIN": False, "ALGORITHM": "HS256", "SIGNING_KEY": SECRET_KEY, # Crucial: use a strong, unique secret key here! "VERIFYING_KEY": None, "AUDIENCE": None, "ISSUER": None, "AUTH_HEADER_TYPES": ("Bearer",), "AUTH_TOKEN_CLASSES": ("ninja_jwt.tokens.AccessToken",), "TOKEN_TYPE_CLAIM": "token_type", "JTI_CLAIM": "jti", } AUTHENTICATION_BACKENDS = [ 'django.contrib.auth.backends.ModelBackend', # For default Django user auth 'ninja_jwt.authentication.JWTAuthBackend', # Important for token authentication ] # --- In your_project/urls.py (main project urls) --- from django.contrib import admin from django.urls import path from ninja import NinjaAPI from ninja_jwt.views import AuthRouter from ninja_jwt.authentication import JWTAuth # Create your NinjaAPI instance api = NinjaAPI( version="1.0.0", title="My Django Ninja JWT API" ) # Add the JWT authentication routes (e.g., /api/auth/token, /api/auth/token/refresh) api.add_router("auth/", AuthRouter()) # Example protected endpoint @api.get("/hello", auth=JWTAuth()) # Use JWTAuth to protect this endpoint def protected_hello(request): return {"message": f"Hello, {request.user.username}! You are authenticated."} urlpatterns = [ path('admin/', admin.site.urls), path("api/", api.urls), # Mount your Ninja API ] # To run this example: # 1. Ensure Django and django-ninja-jwt are installed. # 2. Add 'ninja' and 'ninja_jwt' to INSTALLED_APPS in settings.py. # 3. Configure SIMPLE_JWT and AUTHENTICATION_BACKENDS as shown. # 4. Run `python manage.py makemigrations` and `python manage.py migrate`. # 5. Create a superuser: `python manage.py createsuperuser`. # 6. Run the development server: `python manage.py runserver`. # 7. Test: # - POST to /api/auth/token/ with {"username": "youruser", "password": "yourpassword"} to get tokens. # - GET to /api/hello/ with 'Authorization: Bearer <your_access_token>' header.
Debug
Known issues
breakingDjango Ninja JWT migrated its internal settings handling to Pydantic v2 in version `5.4.2`. If your project uses custom settings classes or older Pydantic versions that conflict, you may encounter validation errors related to the settings schema.
fix
Ensure Pydantic is updated to a compatible version (v2.0+) if using custom settings schemas. Review your `SIMPLE_JWT` configuration for any deprecated `Config` attributes, as older versions might have used them.
affects: 5.4.2+
gotchaFor security and proper token signing, the `SIGNING_KEY` within the `SIMPLE_JWT` settings must be set to a strong, unique secret key. By default, it often falls back to `settings.SECRET_KEY`, but explicitly setting it (or ensuring `SECRET_KEY` is robust) is critical. Leaving it unset or weak leads to severe security vulnerabilities.
fix
In `settings.py`, ensure `SIMPLE_JWT["SIGNING_KEY"] = settings.SECRET_KEY` (or a dedicated `JWT_SIGNING_KEY` environment variable) is configured with a robust, randomly generated key. Never hardcode sensitive keys in production.
affects: All versions
gotchaIf `django-ninja-jwt` fails to authenticate users even with correct tokens, a common cause is missing `ninja_jwt.authentication.JWTAuthBackend` from your `AUTHENTICATION_BACKENDS` list in `settings.py`. Django's default `ModelBackend` handles username/password, but the JWT backend is essential for token verification.
fix
Add `'ninja_jwt.authentication.JWTAuthBackend'` to your `AUTHENTICATION_BACKENDS` list in `settings.py`. Ensure `django.contrib.auth.backends.ModelBackend` is also present if you use Django's default user authentication methods.
affects: All versions
Upgrade
Version history
5.4.4latest on PyPI · released Jan 22, 2026
Audit
Dependencies
djangorequiredCore Django framework requirement.
django-ninjarequiredThe API framework this library extends.
python-joserequiredUsed for JWT encoding and decoding operations.
djangorestframework-simplejwtrequiredProvides the underlying JWT functionality and configuration patterns.
Agent activity
30 hits · last 30 days
node
28
OpenAI (training)
1
Resources
django-ninja-jwt — pip install django-ninja-jwt · libregistry