Skip to main content

API Keys for Django Ninja

Project description

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"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ninja_keys-1.0.1.tar.gz (36.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ninja_keys-1.0.1-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file ninja_keys-1.0.1.tar.gz.

File metadata

  • Download URL: ninja_keys-1.0.1.tar.gz
  • Upload date:
  • Size: 36.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.1

File hashes

Hashes for ninja_keys-1.0.1.tar.gz
Algorithm Hash digest
SHA256 ab371ff15f6a5ada4aef4c837042a0e280389bc803141ac00e0fadb430ac3753
MD5 b34e558205ee88d392fc07fd9f9ee09f
BLAKE2b-256 893481bb3101c89e21fd9bb3faad99caa9c52c849acb2b6fac2420ae39ef9449

See more details on using hashes here.

File details

Details for the file ninja_keys-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for ninja_keys-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6e81d868eed2f56744d414f0e3a827ef10f7edc7ed08ad5243d9a9fb692fe1c2
MD5 810b61e09d9669e00a8a917bf4874ce1
BLAKE2b-256 b1a4ee56716477460e8e4d3fa813410dffff69a77b81a00a11259dd253efb92b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page