Django Ninja TS Generator
Automatically builds your TypeScript client whenever your Django Ninja schema changes.
Installation
-
Install the package:
pip install django-ninja-ts
-
Add to
INSTALLED_APPSinsettings.py:INSTALLED_APPS = [ # ... 'django.contrib.staticfiles', 'django_ninja_ts', # Add this # ... ]
Migrating from v1.x
v2.0 switched from openapi-generator-cli (Node.js/Java) to openapi-ts-client (pure Python).
Breaking changes:
- Remove
NINJA_TS_CMD_ARGSfrom your settings (no longer supported) - Add
NINJA_TS_FORMATif you need axios or angular (fetch is default) - Node.js and Java are no longer required
Configuration
Add these settings to your settings.py:
import os
# Path to your NinjaAPI instance (dot notation)
NINJA_TS_API = 'myproject.api.api'
# Where to output the generated client
NINJA_TS_OUTPUT_DIR = os.path.join(BASE_DIR, '../frontend/src/app/shared/api')
# Optional: Client format - 'fetch' (default), 'axios', or 'angular'
NINJA_TS_FORMAT = 'fetch'
# Optional: Debounce time in seconds (prevents rapid rebuilds on "Save All")
# Default: 1.0
# NINJA_TS_DEBOUNCE_SECONDS = 0.5
# Optional: Clear output directory before generation
# Default: True
# NINJA_TS_CLEAN = True
# Optional: Enable/disable auto-generation on runserver
# Default: True
# NINJA_TS_AUTO_GENERATE = True
How It Works
- When you run
python manage.py runserver, the package intercepts the command - It loads your Django Ninja API and extracts the OpenAPI schema
- It calculates a hash of the schema and compares it to the previous build
- If the schema has changed, it runs
openapi-ts-clientto generate the TypeScript client - The hash is stored in
.schema.hashin the output directory to avoid unnecessary rebuilds
Configuration Options
| Setting | Required | Default | Description |
|---|---|---|---|
NINJA_TS_API |
Yes | - | Dot-notation path to your NinjaAPI instance |
NINJA_TS_OUTPUT_DIR |
Yes | - | Directory where the TypeScript client will be generated |
NINJA_TS_FORMAT |
No | fetch |
Client format: fetch, axios, or angular |
NINJA_TS_DEBOUNCE_SECONDS |
No | 1.0 |
Delay before generation to handle rapid file saves |
NINJA_TS_CLEAN |
No | True |
Clear output directory before generation |
NINJA_TS_AUTO_GENERATE |
No | True |
Enable auto-generation on runserver |
Example: Using Axios
NINJA_TS_FORMAT = 'axios'
Example: Using Angular
NINJA_TS_FORMAT = 'angular'
Example: Disabling Auto-Generation
If you prefer to generate the client manually instead of on every server restart:
NINJA_TS_AUTO_GENERATE = False
Manual Generation
You can manually generate the TypeScript client using the generate_ts_client command:
# Generate client (only if schema changed)
python manage.py generate_ts_client
# Force regeneration even if schema hasn't changed
python manage.py generate_ts_client --force
This is useful when:
- You've disabled auto-generation with
NINJA_TS_AUTO_GENERATE = False - You want to generate the client in CI/CD pipelines
- You need to regenerate without restarting the server
Logging
The package uses Python's standard logging module. To see debug output, configure logging in your settings:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
},
},
'loggers': {
'django_ninja_ts': {
'handlers': ['console'],
'level': 'DEBUG',
},
},
}
Troubleshooting
Common Issues
"Module not found" error
Problem: You see an error like Generation Error: Module not found: No module named 'myapp'
Solution: Ensure NINJA_TS_API contains a valid import path to your NinjaAPI instance:
# Correct - full import path
NINJA_TS_API = 'myapp.api.api'
# Incorrect - missing module path
NINJA_TS_API = 'api'
"does not have 'get_openapi_schema' method" error
Problem: The object at your NINJA_TS_API path is not a NinjaAPI instance.
Solution: Ensure you're pointing to the actual NinjaAPI instance, not a module or router:
# In myapp/api.py
from ninja import NinjaAPI
api = NinjaAPI() # This is what NINJA_TS_API should point to
# In settings.py
NINJA_TS_API = 'myapp.api.api' # Points to the 'api' variable in myapp/api.py
"Invalid OpenAPI schema" error
Problem: The schema returned by your API is missing required OpenAPI fields.
Solution: This usually indicates a configuration issue with your NinjaAPI. Ensure your API has:
- A title (set in NinjaAPI constructor or via
titleparameter) - At least one endpoint registered
api = NinjaAPI(title="My API", version="1.0.0")
@api.get("/health")
def health(request):
return {"status": "ok"}
"0.0 is not of type 'string'" / generation fails on Decimal fields
Problem: A model field typed as Decimal (e.g. Django's DecimalField with default=0.00) is rendered by Pydantic v2 as {"type": "string", "pattern": ...} — because decimals are serialized as strings to preserve precision — while its default/example is emitted as a JSON number. Strict OpenAPI validators reject this internally inconsistent node.
Solution: Upgrade to openapi-ts-client>=1.4.1, which sanitizes such literals before generation (and honors skip_validation on the Angular $ref-resolution path). This is the minimum version this package depends on, so a fresh install already includes the fix; if you pinned an older version, run pip install -U openapi-ts-client.
"Output directory parent is not writable" error
Problem: The package cannot create files in the specified output directory.
Solution: Ensure the parent directory of NINJA_TS_OUTPUT_DIR exists and has write permissions:
# Check permissions
ls -la /path/to/parent/directory
# Fix permissions if needed
chmod 755 /path/to/parent/directory
Schema not regenerating after changes
Problem: You've made API changes but the TypeScript client isn't updating.
Solution:
- Delete the
.schema.hashfile in your output directory - Restart the development server
- If using
NINJA_TS_DEBOUNCE_SECONDS, wait for the debounce period
Configuration Validation
The package validates your configuration at startup using Django's system checks. Run checks manually with:
python manage.py check
This will report any configuration errors like:
- Missing required settings
- Invalid setting types
- Unwritable output directories
Debug Mode
Enable debug logging to see detailed information about the generation process:
LOGGING = {
'version': 1,
'loggers': {
'django_ninja_ts': {
'handlers': ['console'],
'level': 'DEBUG',
},
},
}
Contributing
Commit Messages
This project uses Conventional Commits for commit messages.
Format: <type>(<scope>): <description>
Types:
feat- New featuresfix- Bug fixesdocs- Documentation changesstyle- Code style changes (formatting, whitespace)refactor- Code refactoring without feature changestest- Adding or updating testschore- Maintenance tasks, dependencies, configs
Examples:
feat(generator): add support for axios client
fix(runserver): handle missing Java dependency gracefully
docs(readme): add troubleshooting section
License
MIT License - see LICENSE for details.
Metadata
Release files for django-ninja-ts 2.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django_ninja_ts-2.1.1.tar.gz | 18.4 kB | With provenance | Details for django_ninja_ts-2.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_ninja_ts-2.1.1-py3-none-any.whl | Python 3 | none | any | Details for django_ninja_ts-2.1.1-py3-none-any.whl |
Total release size: 31.7 kB
Release files / django_ninja_ts-2.1.1.tar.gz
| Download URL | django_ninja_ts-2.1.1.tar.gz |
|---|---|
| Size | 18.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bfea0b8017aa7107c6cb1e2dce8f7e507dbca143d1b402b9ef8474a71f8f486e
|
|
BLAKE2b-256 checksum How to use checksums |
327a6b27d476acb0774dfcba6a32ddf3696168f7281a62b8e7e056cd2d2c8630
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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 Jun 11, 2026.
Transparency logRelease files / django_ninja_ts-2.1.1-py3-none-any.whl
| Download URL | django_ninja_ts-2.1.1-py3-none-any.whl |
|---|---|
| Size | 13.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2ffceb7fbec15b4242a52498bb5be875710a42ad946846d69c47655fb12cc31c
|
|
BLAKE2b-256 checksum How to use checksums |
1d3560ab70e5e587c9fd0ad68b8cffe7f60f08265b4b452897066c17dd63aa4a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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 Jun 11, 2026.
Transparency logSecurity for django-ninja-ts 2.1.1
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.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for django_ninja_ts-2.1.1.tar.gz
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 Jun 11, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.