SDK
Go
Installation
Setup a project by making a directory:
mkdir hello-ngrok
Then, change to that directory:
cd hello-ngrok
Install go on your system here or use Homebrew:
brew install go
Initialize your go project:
go mod init hello-ngrok
Install the ngrok-go package:
go get golang.ngrok.com/ngrok
Now we need a runnable example. Let's create a new entry file:
touch main.go
Put your app online
Start a network listener to establish an ngrok tunnel for HTTP requests:
hello-ngrok/main.go
package main
import (
"context"
"fmt"
"log"
"net/http"
"golang.ngrok.com/ngrok"
"golang.ngrok.com/ngrok/config"
)
func main() {
if err := run(context.Background()); err != nil {
log.Fatal(err)
}
}
func run(ctx context.Context) error {
listener, err := ngrok.Listen(ctx,
config.HTTPEndpoint(),
ngrok.WithAuthtokenFromEnv(),
)
if err != nil {
return err
}
log.Println("Ingress established at:", listener.URL())
return http.Serve(listener, http.HandlerFunc(handler))
}
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello from ngrok-go!")
}
Run your Go app with your ngrok authtoken as an environment variable. Sign up for a free account to get your authtoken.
NGROK_AUTHTOKEN= go run main.go