Skip to main content
ngrok logo

Download ngrok

ngrok is your app’s front door—and the fastest way to put anything on the internet.

SDK

Rust

Installation

Setup a project by making a directory:

mkdir hello-ngrok

Then, change to that directory:

cd hello-ngrok

Install rust on your system here or use Homebrew:

brew install rust

Initialize your rust project:

cargo init

Install the ngrok-rust package:

cargo add ngrok -F axum

Install required dependencies:

cargo add axum
cargo add tokio -F rt-multi-thread -F macros

Edit your Cargo.toml file so that it has the correct dependency versions:

[package]
name = "rust-sdk"
version = "0.1.0"
edition = "2021"

[dependencies]
axum = "0.6"
ngrok = { version = "0.13.1", features = ["axum"] }
tokio = { version = "1.41.0", features = ["full", "macros", "rt-multi-thread"] }
hyper = "0.14"
anyhow = "1.0"
}

Put your app online

hello-ngrok/src/main.rs

use axum::{routing::get, Router};
use ngrok::prelude::*;
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box> {
    // build our application with a route
    let app = Router::new().route("/", get(|| async { "Hello from ngrok-rs!" }));

    // listen on ngrok ingress (i.e. https://myapp.ngrok.io)
    let listener = ngrok::Session::builder()
        .authtoken_from_env()
        .connect()
        .await?
        .http_endpoint()
        .listen()
        .await?;
    println!("Ingress URL: {:?}", listener.url());
    axum::Server::builder(listener)
        .serve(app.into_make_service())
        .await?;
    Ok(())
}

Run your Rust app with your ngrok authtoken as an environment variable. for a free account to get your authtoken.

NGROK_AUTHTOKEN= cargo r