Skip to main content

Jun 18, 2025

ngrok's Go SDK v2: simple, streamlined, more powerful

I’m excited to let you all know we’ve just published the v2 API of ngrok-go, our idiomatic open-source Go package for embedding ngrok directly into your Go applications. This v2 update modernizes the API, simplifies integration, and exposes new ngrok platform features for a leaner, more idiomatic interface that’s easier to learn, use, and maintain.

With just a few lines of code, ngrok-go adds a secure, environment-agnostic API gateway in front of your Go application—no matter where it runs. When you use ngrok-go, you will receive traffic the same way no matter where you run your app: any cloud, CI environment, on your laptop, … anywhere.

Over the last two years, ngrok-go has been adopted by thousands of developers and integrated into 80+ open source packages. Meanwhile, ngrok’s own platform has evolved—prompting a major upgrade to keep the SDK in step. Let’s take a closer look at what’s new in v2.

Here’s what’s new in v2

  • Streamlined API surface: We reduced the SDK from 3 subpackages to 1 and trimmed over 150 exported identifiers down to just 50. You’ll find fewer concepts to learn, and more clarity in how to use them. See the new API reference docs.
  • Removed and consolidated features:
    • Removed labeled tunnels and unnecessary convenience methods.
    • Consolidated middleware and access control into a single Traffic Policy API.
  • Introduced fine-grained Dialer and TLS controls:
  • Simplified forwarding API: added a unified, simpler Forward API when you want to use ngrok-go to forward connections to another process like the ngrok agent
  • Simplified events API: There are fewer APIs needed to handle events and server RPCs and they are extensible so that additional ones can be added with minimal additions to the API surface
  • Unified logging with log/slog: Now uses Go’s new standard structured logging library, log/slog, so your ngrok logs integrate cleanly with modern observability stacks.
  • Unified terminology: We’ve aligned the SDK with ngrok’s platform concepts—Endpoints, Agents, and Traffic Policies—eliminating confusion and making the experience consistent with other ngrok tools and SDKs.

Example: serve a “Hello World” Go app with ngrok-go v2

Let’s try ngrok-go v2! First, install the SDK with:

go get golang.ngrok.com/ngrok/v2

Then export your auth token by entering the following in your terminal:

export NGROK_AUTHTOKEN=<your_auth_token>

And go run the following code:

1package main2 3import (4	"context"5	"fmt"6	"log"7	"net/http"8	"golang.ngrok.com/ngrok/v2"9)10 11func main() {12	l, err := ngrok.Listen(context.Background())13	if err != nil {14		log.Fatal(err)15	}16	fmt.Println("endpoint url: ", l.URL())17	http.Serve(l, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {18		fmt.Fprintln(w, "Hello from your ngrok-delivered Go app!")19	}))20}

Traffic Policy: declare traffic management in your Go code

Traffic Policy is ngrok’s configuration language that enables you to filter, match, manage, and orchestrate traffic with a simple CEL-based rules engine. Much of the v1 package API has been replaced with Traffic Policy. Let’s see how easy that is to use to add authentication to an endpoint:

1package main2 3import (4	"context"5	"fmt"6	"log"7	"net/http"8	"golang.ngrok.com/ngrok/v2"9)10 11func main() {12	if err := run(context.Background()); err != nil {13		log.Fatal(err)14	}15}16 17const trafficPolicy = `18on_http_request:19	- name: OAuth20		actions:21			- type: oauth22				config:23					auth_id: oauth24					provider: google25	- name: bad email26		expressions:27			- actions.ngrok.oauth.identity.email != 'alan@example.com'28		actions:29			- type: custom-response30				config:31					body: "Access denied: ${actions.ngrok.oauth.identity.name}!"32					status_code: 40333`34 35func run(ctx context.Context) error {36	// Create an HTTP listener with the traffic policy37	ln, err := ngrok.Listen(ctx,38		ngrok.WithTrafficPolicy(trafficPolicy),39		ngrok.WithDescription("traffic policy example"),40	)41	if err != nil {42		return err43	}44	// Serve HTTP traffic on the ngrok endpoint45	log.Println("Endpoint online", ln.URL())46	return http.Serve(ln, http.HandlerFunc(handler))47}48 49func handler(w http.ResponseWriter, r *http.Request) {50	fmt.Fprintf(w, "Hello, %s\n", r.Header.Get("authenticated-user"))51}

Although it may seem unconventional at first to define traffic policy as YAML instead of typed Go code, we’ve found this approach greatly enhances the user experience. YAML-based traffic policies are easy to test, share, and reuse across Cloud Endpoints, Agent Endpoints, the Kubernetes Operator, and SDKs. You can copy and modify examples from documentation or the community with ease. Additionally, using Traffic Policy enables access to new traffic features without needing to upgrade your SDK.

Get started with ngrok-go v2

First, install the SDK: go get golang.ngrok.com/ngrok/v2

Then check out our other resources:

We distribute agent SDK libraries in Go, Rust, Python and Javascript. Go is the first agent SDK to get a 2.0 facelift, but you can expect us to roll out improved APIs for all languages over the remainder of the year.

If you have questions or feedback about ngrok-go v2 or any of our SDKs, reach out—we’d love to hear how we can make things better.