Skip to main content

ninja-keys

PyPI version PyPI Supported Python Versions tests codecov

API Keys for Django Ninja. 🔐

Based on djangorestframework-api-key.

Introduction

This package provides a simple and secure way to add API keys to your Django Ninja APIs.

You can easily customize API keys to your needs, such as tying them to specific users or organizations, setting expiration dates, and more.

Quickstart

Install the package using pip:

pip install ninja-keys

Add ninja_keys to your INSTALLED_APPS in your Django project's settings file:

INSTALLED_APPS = [
    ...
    "ninja_keys",
]

Run the migrations:

python manage.py migrate

Usage

API Key creation

API keys can be created via the Django admin interface (/admin) or programmatically via the .create_key() method on APIKey objects. By doing this, you'll have only one-time access to the key. Once it's hashed and stored, you cannot access the raw key anymore.

from ninja_keys.models import APIKey

api_key, key = APIKey.objects.create_key(name="my-remote-service")
# Proceed with `api_key` and `key`... The `key` value is the raw key that should be provided to the client.

API Key authentication

API keys can be used to authenticate requests to your API. To do this, you'll need to add the ApiKeyAuth class to your API's auth attribute. It can be added globally or per view/router.

from ninja_keys.auth import ApiKeyAuth

# global
api = NinjaAPI(auth=ApiKeyAuth())
from ninja_keys.auth import ApiKeyAuth


# Per view/router
@api.get("/protected", auth=ApiKeyAuth())
def protected(request):
    return "Hello, protected!"

Requests with API keys

To make requests with an API key, you'll need to send the key in the X-API-Key header.

# Using curl
curl -X GET -H "X-API-Key: my-api-key" http://localhost:8000/api/protected

# Using HTTPie
http http://localhost:8000/api/protected X-API-Key:"my-api-key"

By default, the API key is required to be sent in the X-API-Key header. You can change this behavior by overriding the param_name attribute on the ApiKeyAuth class.

class MyApiKeyAuth(ApiKeyAuth):
    param_name = "Api-Key"

Release files for ninja-keys 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ninja-keys 1.0.1
File Size Uploaded
ninja_keys-1.0.1.tar.gz 36.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ninja-keys 1.0.1
File Interpreter ABI Platform
ninja_keys-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 45.9 kB

Release files / ninja_keys-1.0.1.tar.gz

Download URL ninja_keys-1.0.1.tar.gz
Size 36.9 kB
Tags Source
SHA-256 checksum
How to use checksums
ab371ff15f6a5ada4aef4c837042a0e280389bc803141ac00e0fadb430ac3753
BLAKE2b-256 checksum
How to use checksums
893481bb3101c89e21fd9bb3faad99caa9c52c849acb2b6fac2420ae39ef9449
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.1

Release files / ninja_keys-1.0.1-py3-none-any.whl

Download URL ninja_keys-1.0.1-py3-none-any.whl
Size 9.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6e81d868eed2f56744d414f0e3a827ef10f7edc7ed08ad5243d9a9fb692fe1c2
BLAKE2b-256 checksum
How to use checksums
b1a4ee56716477460e8e4d3fa813410dffff69a77b81a00a11259dd253efb92b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.1

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page