Skip to main content

ngrok Rust SDK Quickstart

The ngrok Rust SDK enables you to quickly and efficiently serve Rust applications on the internet without the need to configure low-level network primitives like IPs, certificates, load balancers, or ports.

This quickstart uses ngrok's Rust SDK to put a Rust app running on your local device online and secure it by requiring visitors to log in with a Google account to access it.

What you'll need

  1. An ngrok account.
  2. Your ngrok auth token, which you can find in the dashboard.
  3. Rust installed on your machine. You can check this by running rustc --version in your terminal.

1. Reserve your domain

A new URL is generated for you every time you start an endpoint. To maintain a consistent URL, you can reserve a free static domain connected to your ngrok account.

To do so, navigate to the dashboard, visit the Domains section, and select + New Domain. You can choose a free static domain, or you can use a custom domain you already own.

2. Start your app or service

Start up the app or service you'd like to put online. This is the app that your agent endpoint will forward online traffic to.

If you don't have an app to work with, you can set up a basic HTTP server at port 8080.

First, create a directory for your Rust project and navigate into it:

Loading…

Next, create a new Rust project:

Loading…

Then, replace the contents of src/main.rs with the following code to set up a basic HTTP server:

Loading…

To run the server, navigate to the root directory of the project (where the Cargo.toml file is located) and run the following command:

Loading…

3. Install the Rust SDK

Create a new Rust project and use the following terminal commands to install the Rust SDK.

tip

If your terminal is already running an app or service, you'll need to open a new terminal window or tab to follow this step.

Loading…

Then, edit your Cargo.toml file to include the necessary dependencies:

Loading…

5. Create your endpoint

Create your agent endpoint, which will forward public traffic to your app.

In the ngrok-rust-demo directory you created in the previous step, add the following code to your src/main.rs file.

This example:

  • Starts an Agent endpoint that forwards from your reserved domain to your service running on port 8080.
    • If your app is running on a different port, change the example code to reflect that.
  • Secures the endpoint with a Traffic Policy that requires authentication via Google OAuth.
    • Traffic policies allow you to control how traffic is handled by your endpoint, including authentication, headers, and more.
tip

This example uses ngrok's default Google OAuth application. To use your own, see the OAuth Traffic Policy Action documentation.

Loading…

6. Test your endpoint

Test your endpoint by running the following terminal command, swapping in your authtoken.

Loading…

Assuming everything is set up correctly, this should print your reserved domain URL to the terminal.

When you visit the URL, you should be prompted to log in with Google. After logging in, you see your app or service.

If you used the example app in this quickstart, you'll see "Hello from Rust HTTP Server!" displayed in your browser.

What's next?