Ngrok Configuration Reference
Complete reference for configuring ngrok integration in Django-CFG.
Basic Configuration
from django_cfg import DjangoConfig, NgrokConfig
class MyConfig(DjangoConfig):
project_name: str = "My Project"
# Enable ngrok for webhook development
ngrok: NgrokConfig = NgrokConfig(
enabled=True, # Enable ngrok (only in DEBUG=True)
authtoken="${NGROK_AUTHTOKEN}", # Optional: from env var automatically if omitted
)
config = MyConfig()NgrokConfig Fields
| Field | Type | Default | Description |
|---|---|---|---|
enabled | bool | False | Enable ngrok integration (only works when DEBUG=True) |
authtoken | Optional[str] | None | Ngrok auth token. Falls back to NGROK_AUTHTOKEN env var if None |
basic_auth | Optional[List[str]] | None | Basic auth credentials in format ["user:pass"] |
compression | bool | True | Enable gzip compression |
Advanced Configuration
With Authentication
For advanced ngrok features like custom domains, provide an auth token:
from django_cfg import NgrokConfig
ngrok: NgrokConfig = NgrokConfig(
enabled=True,
authtoken="${NGROK_AUTHTOKEN}", # Load from NGROK_AUTHTOKEN env var
)Or provide token directly (not recommended for production):
ngrok: NgrokConfig = NgrokConfig(
enabled=True,
authtoken="your_ngrok_token_here",
)If authtoken is None, the service automatically checks the NGROK_AUTHTOKEN environment variable at tunnel start via get_authtoken().
With Basic Auth Protection
Password-protect the tunnel:
ngrok: NgrokConfig = NgrokConfig(
enabled=True,
basic_auth=["admin:secret"], # Format: ["user:pass"]
compression=True,
)Environment Variables
Set by Django-CFG
When you run python manage.py runserver_ngrok, Django-CFG automatically sets these environment variables:
| Variable | Example | Description |
|---|---|---|
NGROK_URL | https://abc123.ngrok.io | Full tunnel URL |
NGROK_HOST | abc123.ngrok.io | Tunnel hostname |
NGROK_SCHEME | https | Protocol scheme |
Usage in code:
import os
# Get tunnel URL
ngrok_url = os.environ.get('NGROK_URL') # "https://abc123.ngrok.io"
# Build webhook URL
webhook_url = f"{ngrok_url}/api/webhooks/stripe/"Required Environment Variables
For advanced ngrok features:
# Ngrok auth token (optional, for paid features)
export NGROK_AUTHTOKEN="your_ngrok_token"Configuration Examples
Example 1: Development Only
from django_cfg import DjangoConfig, NgrokConfig
from .environment import env
class MyConfig(DjangoConfig):
# Ngrok only in development
ngrok: NgrokConfig = NgrokConfig(
enabled=(env.environment == "development"),
)Example 2: With Auth Token from Env
from django_cfg import NgrokConfig
ngrok: NgrokConfig = NgrokConfig(
enabled=True,
# authtoken is None → automatically reads NGROK_AUTHTOKEN at tunnel start
compression=True,
)Example 3: With Password Protection
ngrok: NgrokConfig = NgrokConfig(
enabled=True,
basic_auth=["admin:supersecret"],
)Example 4: Environment-Specific
from .environment import env
class MyConfig(DjangoConfig):
ngrok: NgrokConfig = NgrokConfig(
enabled=(env.environment == "development"),
)
# Use real URL in prod
api_url: str = (
"https://api.myapp.com"
if env.environment == "production"
else "http://localhost:8000"
)Configuration Best Practices
1. Use Environment Variables for Auth Token
# ✅ CORRECT - omit authtoken; NGROK_AUTHTOKEN env var is read automatically
ngrok: NgrokConfig = NgrokConfig(
enabled=True,
)
# ❌ WRONG - hardcode token
ngrok: NgrokConfig = NgrokConfig(
enabled=True,
authtoken="hardcoded-token", # Security risk!
)2. Development Only
# ✅ CORRECT - automatically disabled in production (validator enforces DEBUG=True)
ngrok: NgrokConfig = NgrokConfig(
enabled=True,
)
# Even better - explicit environment check
from .environment import env
ngrok: NgrokConfig = NgrokConfig(
enabled=(env.environment == "development"),
)3. Type-Safe Configuration
# ✅ CORRECT - use Pydantic model
from django_cfg import NgrokConfig
ngrok: NgrokConfig = NgrokConfig(
enabled=True,
compression=True,
)
# ❌ WRONG - raw Django settings
# NGROK_ENABLED = True # No validation!Validations
Django-CFG automatically validates your ngrok configuration:
# ❌ Raises ValidationError — cannot enable ngrok in production (DEBUG=False)
ngrok: NgrokConfig = NgrokConfig(
enabled=True, # ValidationError if DEBUG is False
)
# ✅ Correct — enable only in development
ngrok: NgrokConfig = NgrokConfig(
enabled=(env.environment == "development"),
)Next Steps
- Implementation - Learn how to get tunnel URLs in code
- Webhook Examples - See practical webhook integration examples
- Troubleshooting - Common configuration issues
See Also
Ngrok Integration
Core Documentation:
- Ngrok Overview - Ngrok integration introduction
- Implementation Guide - Getting tunnel URLs in code
- Webhook Examples - Real-world webhook integrations
- Troubleshooting - Common configuration issues
Configuration & Setup
Configuration:
- Configuration Models - Complete DjangoConfig reference
- Type-Safe Configuration - Pydantic validation
- Environment Variables - Ngrok auth token and secrets
- Environment Detection - Dev-only ngrok
Getting Started:
- Installation - Install Django-CFG with ngrok
- Configuration Guide - YAML configuration setup
- First Project - Quick start tutorial
Payment & Webhook Features
Payment Integration:
Related Integrations:
- Django-RQ Integration - Async webhooks
- Integrations Overview - All integrations
Tools & Development
CLI & Management:
- CLI Tools - Ngrok CLI commands
- Development Commands - runserver_ngrok
- Troubleshooting - Debug issues
Examples:
- Sample Project - Production example
- Examples Guide - More patterns