Skip to main content

Reusable Django REST API framework with auto-discovery and bearer token authentication

Project description

django-directory-api

Reusable Django REST API framework with auto-discovery and bearer token authentication.

Features

  • 🔐 Bearer Token Authentication - Secure API access with per-user tokens
  • 🔌 Auto-Discovery - Automatically discovers and registers API routers from api.py files
  • 📚 Django Shinobi - Built on Django Shinobi (Django Ninja fork) for type-safe APIs
  • 🤖 LLM-Optimized - Rich OpenAPI documentation designed for AI agent consumption
  • 🎯 Zero Config - Just create an api.py file and start building

Installation

pip install django-directory-api

Quick Start

1. Add to INSTALLED_APPS

# settings.py
INSTALLED_APPS = [
    # ...
    "django_directory_api",  # Must come before apps that define API endpoints
    # ...
]

2. Include API URLs

# urls.py
from django_directory_api import api

urlpatterns = [
    path("api/", api.urls),
    # ...
]

3. Create API Endpoints

Create an api.py file in any Django app:

# myapp/api.py
from ninja import Router
from .models import MyModel

router = Router(tags=["My App"])

@router.get("/items/")
def list_items(request):
    return {"items": list(MyModel.objects.values())}

That's it! The router is automatically discovered and registered.

Authentication

Creating API Tokens

  1. Log into Django admin
  2. Navigate to "API Tokens"
  3. Click "Add API Token"
  4. Give it a name (e.g., "Production Bot")
  5. Copy the token value (shown only once)

Using Tokens

curl -H "Authorization: Bearer <your-token>" \
     https://example.com/api/items/
import requests

headers = {"Authorization": "Bearer <your-token>"}
response = requests.get("https://example.com/api/items/", headers=headers)

Auto-Discovery

The package automatically discovers api.py files in all installed Django apps:

  • ✅ Looks for router attribute (single router)
  • ✅ Looks for routers attribute (list of routers)
  • ✅ Skips apps without api.py files
  • ✅ No explicit registration required

Example with Multiple Routers

# myapp/api.py
from ninja import Router

public_router = Router(tags=["Public"])
admin_router = Router(tags=["Admin"])

@public_router.get("/public/")
def public_endpoint(request):
    return {"message": "Hello world"}

@admin_router.get("/admin/")
def admin_endpoint(request):
    return {"message": "Admin only"}

# Export multiple routers
routers = [public_router, admin_router]

API Documentation

Once installed, automatic documentation is available at:

  • Swagger UI: /api/docs
  • OpenAPI Schema: /api/openapi.json
  • ReDoc: /api/redoc

Architecture

The package provides:

  1. APIToken Model - Database-backed authentication tokens
  2. APIKeyAuth - Bearer token authentication handler
  3. Auto-Discovery System - Scans apps for api.py files at startup
  4. Common Schemas - Shared Pydantic schemas (e.g., PaginatedResponse)

Development

# Install dependencies
uv sync --extra dev

# Run tests
python tests/manage.py test

# Format code
ruff format .

# Lint
ruff check .

License

MIT License - see LICENSE file for details.

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

django_directory_api-0.1.0.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

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

django_directory_api-0.1.0-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file django_directory_api-0.1.0.tar.gz.

File metadata

  • Download URL: django_directory_api-0.1.0.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_directory_api-0.1.0.tar.gz
Algorithm Hash digest
SHA256 fb2c0981e2e9c445226acd6eb1fa1cd62107d3678cc3c4bde9c5ed5057369223
MD5 a8ea1ad5f8fbde494b77282f33e77106
BLAKE2b-256 daca275d62ba41b1e132db34386cc0b85c32f4865a694de84e49c428443559aa

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_directory_api-0.1.0.tar.gz:

Publisher: publish.yml on heysamtexas/django-directory-api

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_directory_api-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_directory_api-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5434adcb34eadeeef9d390d0926753633caaf1014d254ef6314022949a6a8c73
MD5 9d099d8b9c816e54cab77ef396e65409
BLAKE2b-256 b80f5c645154bc4f7850de38b34cc827ae62d5bad2e89648fd23e8ddd9843049

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_directory_api-0.1.0-py3-none-any.whl:

Publisher: publish.yml on heysamtexas/django-directory-api

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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