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.pyfiles - 📚 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.pyfile 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
- Log into Django admin
- Navigate to "API Tokens"
- Click "Add API Token"
- Give it a name (e.g., "Production Bot")
- 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
routerattribute (single router) - ✅ Looks for
routersattribute (list of routers) - ✅ Skips apps without
api.pyfiles - ✅ 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:
- APIToken Model - Database-backed authentication tokens
- APIKeyAuth - Bearer token authentication handler
- Auto-Discovery System - Scans apps for
api.pyfiles at startup - 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fb2c0981e2e9c445226acd6eb1fa1cd62107d3678cc3c4bde9c5ed5057369223
|
|
| MD5 |
a8ea1ad5f8fbde494b77282f33e77106
|
|
| BLAKE2b-256 |
daca275d62ba41b1e132db34386cc0b85c32f4865a694de84e49c428443559aa
|
Provenance
The following attestation bundles were made for django_directory_api-0.1.0.tar.gz:
Publisher:
publish.yml on heysamtexas/django-directory-api
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_directory_api-0.1.0.tar.gz -
Subject digest:
fb2c0981e2e9c445226acd6eb1fa1cd62107d3678cc3c4bde9c5ed5057369223 - Sigstore transparency entry: 604623485
- Sigstore integration time:
-
Permalink:
heysamtexas/django-directory-api@ff239091cd681d55123392790c00ffcf5c9b2bfd -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/heysamtexas
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ff239091cd681d55123392790c00ffcf5c9b2bfd -
Trigger Event:
release
-
Statement type:
File details
Details for the file django_directory_api-0.1.0-py3-none-any.whl.
File metadata
- Download URL: django_directory_api-0.1.0-py3-none-any.whl
- Upload date:
- Size: 12.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5434adcb34eadeeef9d390d0926753633caaf1014d254ef6314022949a6a8c73
|
|
| MD5 |
9d099d8b9c816e54cab77ef396e65409
|
|
| BLAKE2b-256 |
b80f5c645154bc4f7850de38b34cc827ae62d5bad2e89648fd23e8ddd9843049
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_directory_api-0.1.0-py3-none-any.whl -
Subject digest:
5434adcb34eadeeef9d390d0926753633caaf1014d254ef6314022949a6a8c73 - Sigstore transparency entry: 604623490
- Sigstore integration time:
-
Permalink:
heysamtexas/django-directory-api@ff239091cd681d55123392790c00ffcf5c9b2bfd -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/heysamtexas
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ff239091cd681d55123392790c00ffcf5c9b2bfd -
Trigger Event:
release
-
Statement type: