djangorestframework-api-key (DRF API Key) is a Django REST Framework library providing API key permissions for server-side clients. It allows secure interaction for machines or third-party services that do not have user accounts, focusing on authorization rather than user authentication. The current version is 3.1.0, and it maintains a regular release cadence with several updates annually, supporting recent Django and Python versions.
pip install djangorestframework-api-keyVerified import paths — ran on the pinned version, not inferred.
To get started, add `rest_framework_api_key` to your `INSTALLED_APPS` in `settings.py` and run migrations. Then, configure `HasAPIKey` as a default permission class in `REST_FRAMEWORK` settings or apply it to individual views. Clients will then need to provide the generated API key in the `Authorization: Api-Key <API_KEY>` header. API keys can be created via the Django admin panel or programmatically using `APIKey.objects.create_key()`. The full key is only visible upon creation; subsequently, only its prefix is shown for security.
No direct code change is required for existing keys, as the upgrade is automatic. Ensure your environment supports Python >=3.8. Review performance implications and test thoroughly after upgrading.
Ensure your project runs on Python 3.7 or newer. Python 3.8+ is recommended for recent versions.
For custom models, verify your `hashed_key` field's `max_length`. If necessary, create a data migration to alter the field or adjust your custom model's definition to `max_length=150`.
Always specify the major version in your `requirements.txt` or `pyproject.toml` (e.g., `djangorestframework-api-key~=3.1`).
Understand the use case for API keys: they are best for machine-to-machine interactions or public API access control. Do not use them to identify or log in individual users.
Always securely store the generated API key immediately after creation and provide it to the client. Do not rely on being able to retrieve it later from the database.