Skip to content

A custom ngrok agent built with the ngrok-python sdk for IOT devices

Notifications You must be signed in to change notification settings

ngrok-samples/ngrok-iot-agent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 

Repository files navigation

IoT-Agent

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.

Features

  • 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)

Prerequisites

  • Python
  • ngrok account (free for dynamic URLs, paid for reserved domains)
  • FastAPI
  • Uvicorn

Setup

  1. Clone the repository:

  2. Create and activate a virtual environment:

  3. Install dependencies:

  4. Set your ngrok authtoken:

    ngrok authtoken <YOUR_NGROK_AUTHTOKEN>
  5. Run the agent:

    python agent.py

API Endpoints

List Tunnels

Request: GET /tunnels

Description: List all active tunnels.

Response:

[
    {
        "url": "https://2F334e095e9685.ngrok.app",
        "protocol": "http",
        "forwards_to": "localhost:8001",
        "domain": null
    }
]

Create Tunnel

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"
}

Create Tunnel With Policy

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"
              }
            }
          }
        ]
      }
    ]
  }
}

Delete Tunnel

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"
}

About

A custom ngrok agent built with the ngrok-python sdk for IOT devices

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages