This project implements an agent that dynamically creates, lists, and deletes ngrok tunnels through a FastAPI interface. The agent ensures that tunnels are persisted and can be recreated upon restart, making it robust for dynamic IoT applications.
- Dynamically create ngrok tunnels
- List all active tunnels
- Delete specific tunnels
- Automatically recreate tunnels on agent restart
- Use reserved domains for consistent tunnel URLs (requires ngrok paid plan)
- Python
- ngrok account (free for dynamic URLs, paid for reserved domains)
- FastAPI
- Uvicorn
-
Clone the repository:
-
Create and activate a virtual environment:
-
Install dependencies:
-
Set your ngrok authtoken:
ngrok authtoken <YOUR_NGROK_AUTHTOKEN>
-
Run the agent:
python agent.py
Request: GET /tunnels
Description: List all active tunnels.
Response:
[
{
"url": "https://2F334e095e9685.ngrok.app",
"protocol": "http",
"forwards_to": "localhost:8001",
"domain": null
}
]
Request: POST /tunnels
Description: Create a new ngrok tunnel.
Request Body:
{
"protocol": "http",
"forwards_to": "localhost:8080",
"domain": "your-reserved-domain.ngrok.io"
}
Response:
{
"url": "https://your-reserved-domain.ngrok.io",
"protocol": "http",
"forwards_to": "localhost:8080",
"domain": "your-reserved-domain.ngrok.io"
}
Request: POST /tunnels
Description: Create a new ngrok tunnel.
Request Body:
{
"protocol": "http",
"forwards_to": "localhost:8080",
"domain": "your-reserved-domain.ngrok.io" ,
"policy": {
"inbound": [],
"outbound": [
{
"expressions": [],
"name": "Add headers to requests",
"actions": [
{
"type": "add-headers",
"config": {
"headers": {
"is-ngrok": "1"
}
}
}
]
}
]
}
}
Request: DELETE /tunnels/{url_part}
Description: Delete a specified tunnel by the main part of the URL.
Request Body :
curl -X DELETE https://82916d83f41a.ngrok.app/tunnels/2F334e095e9685
Response:
{
"detail": "Tunnel deleted"
}