Add ngrok to Next.js, FastAPI, Axum, and Gin
Your web framework already knows when your app starts and stops. Four packages in the ngrok-samples GitHub organization hook into that to give your app a public ngrok endpoint.
You don’t need a second terminal running ngrok http. Each package opens your app’s public URL when your server starts and closes it when the server stops, so you run one service: yours. The FastAPI, Axum, and Gin packages work the same way in production.
ngrok maintains all four as official packages. Each one is built on ngrok’s agent SDKs, so use a package as it is, or read the source and adapt the pattern to your own setup.
The four packages are:
@ngrok/nextjsfor Next.js. It runsnext devand the endpoint as one process, so Ctrl+C stops both. By default, it also writes the endpoint URL toNEXTAUTH_URLandNEXT_PUBLIC_APP_URLin.env.local, so NextAuth callbacks go to your public URL.ngrok-fastapifor FastAPI. It hooks into your app’slifespanwith one call tongrok_fastapi.attach, so the rest of your app stays the same.ngrok-axumfor Axum. It serves yourRouterthrough an endpoint with one call tongrok_axum::serve. The full API is on docs.rs.ngrok-ginfor Gin. It serves your router through an endpoint with one call tongrokgin.Serve.
All four read your ngrok authtoken from NGROK_AUTHTOKEN and use your account’s dev domain by default. Set a reserved domain, a Traffic Policy document, pooling, or binding in the package’s config.
To expose more than one service, list them under endpoints in ngrok.config.ts for Next.js. The FastAPI, Axum, and Gin packages open several endpoints from one call (attach_many, serve_many, and ServeMany).
Before you start
Sign up for ngrok or sign in, then copy an authtoken from the dashboard. Replace <YOUR_AUTHTOKEN> with it in the run commands below.
Next.js
Install the package:
Then run it from your project root in place of next dev:
FastAPI
Install the package:
Then attach it to your app in main.py:
Run your app:
Axum
Add the package with Axum and Tokio:
Then serve your router with ngrok_axum::serve in src/main.rs:
Run your app:
Gin
Install the package:
Then serve your router with ngrokgin.Serve in main.go:
Run your app:
Check your endpoint
Open your app’s public URL in a browser, then confirm the endpoint is online under Endpoints in the dashboard.
Each repository has an examples directory with working apps and a README that covers every config option. The same snippets are on the ngrok download pages for Next.js, FastAPI, Axum, and Gin.