ngrok Python SDK

1.7.0 · active · verified Tue Apr 14

The ngrok Python SDK is an official, open-source library that enables developers to integrate secure ingress directly into their Python applications. It acts as a Python-packaged version of the ngrok Agent CLI, allowing applications to expose local services to the internet without complex network configurations. The library is actively maintained, with a current version of 1.7.0, and typically sees updates in line with major ngrok platform developments.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to expose a local service (assumed to be running on port 9000) to the internet using `ngrok.forward()`. It requires an ngrok authtoken, ideally set as an environment variable `NGROK_AUTHTOKEN`. The `while True: time.sleep(1)` loop keeps the tunnel active until a keyboard interrupt.

import ngrok
import os
import time

# Ensure NGROK_AUTHTOKEN is set in your environment variables
# or pass it directly: ngrok.forward(9000, authtoken="YOUR_AUTHTOKEN")

if os.environ.get('NGROK_AUTHTOKEN') == None:
    print("WARNING: NGROK_AUTHTOKEN environment variable not set. Please set it for ngrok to function correctly.")

# Establish connectivity to a local service running on port 9000
# For this example, imagine a simple HTTP server is running on localhost:9000
listener = ngrok.forward(9000, authtoken_from_env=True)

print(f"ngrok Ingress established at {listener.url()}")

# Keep the listener alive
try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    print("\nClosing ngrok listener")
    ngrok.disconnect(listener.url())
    ngrok.kill()

view raw JSON →