Skip to main content

Ninja API Key

PyPI

codecov

API Key authentication for Django Ninja.

This is a fork from django-ninja-apikey.

Key Features:

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

Installation

pip install ninja-api-key

How to use

  1. Add ninja_apikey to your installed apps in your Django project:
# settings.py

INSTALLED_APPS = [
    # ...
    "ninja_apikey",
]
  1. Apply migrations
python manage.py migrate
  1. Secure

    a. the whole API

    # 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}!"
    

    b. an specific endpoint

    # 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}!"
    
  2. ninja-api-key uses settings.PASSWORD_HASHERS to hash the API keys. Django's default PBKDF2PasswordHasher may be slow depending on the number of iterations. You can change the settings.PASSWORD_HASHERS to use a faster (but less secure) one. ninja-api-key provides a SHA256PasswordHasher.

     # settings.py
    
     PASSWORD_HASHERS = [
         "ninja_apikey.hashers.SHA256PasswordHasher",
         # others
     ]
    

    ⚠️ Keep in mind that this will affect Django authentication as a whole, not just ninja-api-key.

Changelog

See CHANGELOG.md for a detailed history of changes and releases.

Contributing

Contributions are welcome; feel free to open an Issue or Pull Request.

git clone https://github.com/lucasrcezimbra/ninja-api-key
cd ninja-api-key
python -m venv .venv
source .venv/bin/activate
pip install .[test]
pre-commit install
make test

Metadata

Release files for ninja-api-key 2.0.0

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-api-key 2.0.0
File Size Uploaded
ninja_api_key-2.0.0.tar.gz 8.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ninja-api-key 2.0.0
File Interpreter ABI Platform
ninja_api_key-2.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 19.1 kB

Release files / ninja_api_key-2.0.0.tar.gz

Download URL ninja_api_key-2.0.0.tar.gz
Size 8.2 kB
Tags Source
SHA-256 checksum
How to use checksums
3dbc2390d13671ff336377068e83504fa1819a2fe6acbc58ce8afe00a290527c
BLAKE2b-256 checksum
How to use checksums
b5752b453e2055beef8bd1e3b9a66513b4487325736db4f5d85b03e474cdc9a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 6, 2026.

Transparency log

Release files / ninja_api_key-2.0.0-py3-none-any.whl

Download URL ninja_api_key-2.0.0-py3-none-any.whl
Size 11.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6da7d4900953747d2b8f7cca7de1d005eb3996cd2d9a2032d5eaaddd5eed267b
BLAKE2b-256 checksum
How to use checksums
65268a9982dc656e2c905bda8438299ccfd75c7f1957c78758f7f602f0a7e23a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 6, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.0

2 release files

0.2.2

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