Skip to main content

PyPI - Python Version Django versions Status GitHub Actions Workflow Status PyPI - Version

django-api-usage

Lightweight, privacy-first usage metering for Django APIs, with an optional deprecation lifecycle layer on top.

It answers two questions that decide whether an endpoint can be changed or removed:

  1. How much is this endpoint (or this whole Django app) used?
  2. Who is calling it, so they can be contacted before it changes?

The package is deliberately generic: the core is plain Django middleware (works with or without Django REST Framework), it never breaks a request because metering failed, and it stores no raw personal data by default.

Why not just drf-api-tracking?

drf-api-tracking stores one database row per request (including bodies), which is heavy and privacy-sensitive. django-api-usage accumulates counters and keeps consumer attribution hashed and opt-in. That makes it suitable both for continuous usage analytics and for the concrete decision of deprecating an endpoint.

Install

pip install django-api-usage
INSTALLED_APPS = [
    # ...
    "django_api_usage",
]

MIDDLEWARE = [
    # ...
    "django_api_usage.middleware.ApiUsageMiddleware",
]

Metering starts immediately. Counters are buffered in the cache and written to the database in batches:

python manage.py api_usage_flush      # from cron, or a Celery beat task
python manage.py api_usage_report --days 30
python manage.py api_usage_report --days 90 --sunset-candidates

With Celery (and the celery extra) two tasks are provided: django_api_usage.tasks.flush_api_usage and django_api_usage.tasks.prune_api_usage.

Configuration

API_USAGE = {
    "ENABLED": True,
    "FAIL_OPEN": True,
    "BUFFER_BACKEND": "cache",       # "cache" (batched) or "db" (write per request)
    "RETENTION_DAYS": 90,
    "TRACK_CONSUMERS": False,        # opt-in; stores hashed callers only
    "CONSUMER_SALT": "a-rotatable-secret",
}

All resolvers are overridable, so no project-specific knowledge leaks into the package:

API_USAGE = {
    "APP_LABEL_RESOLVER": "myproject.api.resolvers.app_label",
    "SITE_RESOLVER": "myproject.api.resolvers.site_id",
    "ROLE_SCOPES_RESOLVER": "myproject.api.resolvers.role_scopes",
}

Deprecation lifecycle

Endpoint carries deprecated, sunset_date, replacement and owner, so the same data that measures usage also drives a deprecation plan. See the consuming project's migration guide for the full workflow (measure → notify → enforce).

DRF shadow mode

django_api_usage.drf.HasAPIScope enforces scopes declared on a view:

class AllUserViewSet(viewsets.ReadOnlyModelViewSet):
    permission_classes = [HasAPIScope]
    required_scopes = ["user:read_all"]

With API_ENFORCEMENT = "report" (the default) nothing is blocked yet: a X-Api-Would-Deny header is returned instead, so you can deploy a permission and measure what it would have denied before enforcing it.

Status

Alpha. The core is implemented: metering middleware, models and migrations, management commands, the deprecation layer and a DRF shadow-mode permission. Tested on Python 3.10-3.12 and Django 4.2/5.2. MIT licensed.

Development

python -m django test tests --settings=tests.settings
ruff check .
black --check .

Metadata

Release files for django-api-usage 0.1.2

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

Source distribution (sdist)

Source distribution for django-api-usage 0.1.2
File Size Uploaded
django_api_usage-0.1.2.tar.gz 21.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-api-usage 0.1.2
File Interpreter ABI Platform
django_api_usage-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 44.6 kB

Release files / django_api_usage-0.1.2.tar.gz

Download URL django_api_usage-0.1.2.tar.gz
Size 21.9 kB
Tags Source
SHA-256 checksum
How to use checksums
df5ab80c68286cac67d12874e4d20f24238a292a89cd0533a91b3e86df14534b
BLAKE2b-256 checksum
How to use checksums
f22bc16d0062a4c7a4bfbb49bf1836a07cf81b3fd45a022319c548d10e8fcfb6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release files / django_api_usage-0.1.2-py3-none-any.whl

Download URL django_api_usage-0.1.2-py3-none-any.whl
Size 22.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bee2cd0499cc189cea3ba739e0c13a94df6c6dd11b7839d14df357fc643e25f8
BLAKE2b-256 checksum
How to use checksums
2fbe52edc759eadb3f3e9b253c8881ccef7656b53b135f495ceb6eaf8684af56
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release history Release notifications | RSS feed

0.1.10

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.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