Skip to Content
FeaturesModulesNgrok TunnelsConfiguration

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

FieldTypeDefaultDescription
enabledboolFalseEnable ngrok integration (only works when DEBUG=True)
authtokenOptional[str]NoneNgrok auth token. Falls back to NGROK_AUTHTOKEN env var if None
basic_authOptional[List[str]]NoneBasic auth credentials in format ["user:pass"]
compressionboolTrueEnable 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:

VariableExampleDescription
NGROK_URLhttps://abc123.ngrok.ioFull tunnel URL
NGROK_HOSTabc123.ngrok.ioTunnel hostname
NGROK_SCHEMEhttpsProtocol 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

See Also

Ngrok Integration

Core Documentation:

Configuration & Setup

Configuration:

Getting Started:

Payment & Webhook Features

Payment Integration:

Related Integrations:

Tools & Development

CLI & Management:

Examples:

Last updated on