Skip to main content

Download & Install

The fastest way to put anything on the internet.

RustSDK

1

Get started with Rust

Clone the ngrok Rust SDK example:

git clone git@github.com:ngrok/rust-sdk-example.git && cd rust-sdk-example

Install the ngrok Rust SDK:

cargo add ngrok url

Add ngrok to your app:

src/main.rs

1use ngrok::config::ForwarderBuilder;2use ngrok::prelude::*;3use url::Url;4 5async fn connect_ngrok() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {6    let sess = ngrok::Session::builder()7        .authtoken_from_env()8        .connect()9        .await?;10 11    let mut listener = sess12        .http_endpoint()13        .listen_and_forward(Url::parse("http://localhost:8085")?)14        .await?;15 16    println!("Available at: {:?}", listener.url());17 18    let _ = listener.join().await;19    Ok(())20}
2

Run your app

NGROK_AUTHTOKEN="<YOUR_AUTHTOKEN>" cargo run

Don’t have an authtoken? for a free account.

Open your ngrok URL in a browser to see it working!

You’re all set. What’s next?

1let tp = r#"2on_http_request:3  # redirect users to Google to log in4  - actions:5    - type: oauth6      config:7        provider: google8 9  # allow logins *only* from acme.com10  - expressions:11    - "!actions.ngrok.oauth.identity.email.endsWith('@acme.com')"12    actions:13    - type: deny14"#;15 16let mut listener = sess.http_endpoint().traffic_policy(tp)17  .listen_and_forward(Url::parse("http://localhost:8085")?)18  .await?;
1let tp = r#"2on_http_request:3  # verify webhook signatures from your provider4  - actions:5    - type: verify-webhook6      config:7        provider: github8        # access your signing key from a secure ngrok vault9        # no cleartext secrets in your policy, friend10        secret: ${secrets.get('webhook-vault', 'github-secret')}11"#;12 13let mut listener = sess.http_endpoint().traffic_policy(tp)14  .listen_and_forward(Url::parse("http://localhost:8085")?)15  .await?;
1let tp = r#"2on_http_request:3  # redirect users to your OpenID provider to log in4  - actions:5    - type: openid-connect6      config:7        issuer_url: https://accounts.google.com8        # access your OIDC details from a secure ngrok vault9        client_id: ${secrets.get('auth-vault', 'oidc-client-id')}10        client_secret: ${secrets.get('auth-vault', 'oidc-client-secret')}11        scopes:12          - openid13          - email14"#;15 16let mut listener = sess.http_endpoint().traffic_policy(tp)17  .listen_and_forward(Url::parse("http://localhost:8085")?)18  .await?;
1let tp = r#"2on_http_request:3  - actions:4    - type: basic-auth5      config:6        credentials:7          # add up to 10 username:password pairs, all stored safely in a vault8          - user01:${secrets.get('basic-auth-vault', 'password01')}9          - user02:${secrets.get('basic-auth-vault', 'password02')}10"#;11 12let mut listener = sess.http_endpoint().traffic_policy(tp)13  .listen_and_forward(Url::parse("http://localhost:8085")?)14  .await?;

Inspect every detail of your traffic

Watch the flow in real time, then dig into the headers, body, latency, response, and more for every request.

Peruse the Rust SDK quickstart

Follow along to embed ngrok into your app and use Traffic Policy for auth and traffic transformation.

Bring your own domain

Paid feature

Create a DNS CNAME record to use your own domain name for your endpoint URL.

1async fn connect_ngrok() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {2  let mut listener = sess3    .http_endpoint()4    .domain("hello-world.your-domain.com")5    .listen_and_forward(url)6    .await?;7}

Listen, don't forward

Serve traffic directly on an ngrok listener without forwarding. Your handler receives connections straight from the internet.

1async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {2  let app = Router::new().route("/", get(|| async { "Hello from ngrok-rust!3" }));4  let listener = sess.http_endpoint().listen().await?;5  axum::Server::builder(listener).serve(app.into_make_service()).await?;6}