/django-ninja-apikey

Easy to use API key authentication for Django Ninja REST Framework

Primary LanguagePythonMIT LicenseMIT

Django Ninja APIKey

Easy to use API key authentication for Django Ninja REST Framework

build coverage pypi black


This is an unofficial Django app which makes it easy to manage API keys for the Django Ninja REST Framework.

Key Features:

  • Easy integration in your projects
  • Well integrated in the admin interface
  • Secure API keys due to hashing
  • Works with the standard user model

Installation

pip install django-ninja-apikey

Usage

Add ninja_apikey to your installed apps in your django project:

# settings.py

INSTALLED_APPS = [
    # ...
    "ninja_apikey",
]

Run the included migrations:

python manage.py migrate

Secure an api endpoint with the API keys:

# api.py

from ninja import NinjaAPI
from ninja_apikey.security import APIKeyAuth

#  ...

auth = APIKeyAuth()
api = NinjaAPI()

# ...

@api.get("/secure_endpoint", auth=auth)
def secure_endpoint(request):
    return f"Hello, {request.user}!" 

Or secure your whole api (or a specific router) with the API keys:

# api.py

from ninja import NinjaAPI
from ninja_apikey.security import APIKeyAuth

#  ...

api = NinjaAPI(auth=APIKeyAuth())

# ...

@api.get("/secure_endpoint")
def secure_endpoint(request):
    return f"Hello, {request.user}!" 

You can create now API keys from django's admin interface.

License

This project is licensed under the terms of the MIT license.