ngrok-fastapi
Bring ngrok Traffic Policy, pooling, and binding config to FastAPI — via FastAPI's own lifespan hook, with one call:
from fastapi import FastAPI
import ngrok_fastapi
app = FastAPI()
ngrok_fastapi.attach(app, ngrok_fastapi.Config(port=8000))
What it is
A small FastAPI-native ngrok library. attach()/attach_many() wrap an app's existing
lifespan context manager so an ngrok tunnel opens when the app starts and closes when it stops.
Setup
- Sign up at ngrok.com and grab an authtoken from the dashboard.
- Set it as an env var:
NGROK_AUTHTOKEN=your_token_here pip install ngrok-fastapiand callattach()— seeexamples/basic.py.
Config
ngrok_fastapi.attach(app, ngrok_fastapi.Config(
port=8000,
url="your-reserved-domain.ngrok.app", # None = account's default dev domain
pooling=False,
traffic_policy="""
on_http_request:
- actions:
- type: basic-auth
config:
credentials:
- "user:password123"
""",
binding=None, # ngrok_fastapi.Binding.INTERNAL, etc.
))
See examples/with_config.py for a working example.
| Field | Type | Description |
|---|---|---|
port |
int |
Local port this endpoint forwards to. |
url |
Optional[str] |
Reserved domain for this endpoint. None falls back to the account's default dev domain. |
pooling |
bool |
Opt in to ngrok endpoint pooling — required if another endpoint on the same session would otherwise collide on the same domain. See Collisions. |
traffic_policy |
Optional[str] |
A raw ngrok Traffic Policy document (YAML or JSON) — the mechanism for auth, IP restrictions, header manipulation, webhook verification, and more. |
binding |
Optional[Binding] |
PUBLIC / INTERNAL / KUBERNETES ingress configuration. Not part of Traffic Policy (checked ngrok's actions reference directly — no equivalent exists), so it stays a standalone field. INTERNAL requires url to end in .internal, enforced by ngrok itself (ERR_NGROK_9029 if it doesn't). |
Multiple endpoints
attach_many(app, configs) opens one endpoint per Config, all on a single session, and
ties all of them to the same app's lifespan. attach(app, config) is just
attach_many(app, [config]).
ngrok_fastapi.attach_many(app, [
ngrok_fastapi.Config(port=8000, url="app.mycompany.ngrok.app"),
ngrok_fastapi.Config(port=8001, url="api.mycompany.ngrok.app"),
])
Development
python3 -m venv venv && source venv/bin/activate
pip install -e ".[dev]"
pytest
python examples/basic.py
See DESIGN.md for the design rationale and everything confirmed empirically
along the way.
License
MIT OR Apache-2.0
Metadata
Release files for ngrok-fastapi 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| ngrok_fastapi-0.1.0.tar.gz | 9.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ngrok_fastapi-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 20.6 kB
Release files / ngrok_fastapi-0.1.0.tar.gz
| Download URL | ngrok_fastapi-0.1.0.tar.gz |
|---|---|
| Size | 9.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9074a3ea7b5ea7d165fa647e6fb38e6674e22131585a5f348d1f8f9c93b02149
|
|
BLAKE2b-256 checksum How to use checksums |
bedac379d4320023fecd3cc38b9ed7555c0686e112f36dfa7c0cc4081da8dc74
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Oct 15, 2026.
Transparency logRelease files / ngrok_fastapi-0.1.0-py3-none-any.whl
| Download URL | ngrok_fastapi-0.1.0-py3-none-any.whl |
|---|---|
| Size | 10.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
365a338fc6c68c1ac0e748f8f5812407593e3a3438e6ccc9ddd696095aba4631
|
|
BLAKE2b-256 checksum How to use checksums |
fe0942cb3d84e1ed6e6a41ef1edd1eba49afbed5aea28aee8a79856a0f29a763
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Oct 15, 2026.
Transparency log