ngrok API client library for Golang
This library wraps the ngrok HTTP API to make it
easier to consume in Go.
For creating ngrok tunnels directly from your Go application, check out the ngrok Go Agent SDK instead.
Installation
Installation is as simple as using go get
.
go get github.com/ngrok/ngrok-api-go/v5
Support
The best place to get support using this library is through the ngrok Slack Community. If you find any bugs, please contribute by opening a new GitHub issue.
Documentation
A quickstart guide and a full API reference are included in the ngrok go API documentation on pkg.go.dev
Quickstart
Please consult the documentation for additional examples.
Create an IP Policy that allows traffic from some subnets
package main
import (
"context"
"fmt"
"os"
"github.com/ngrok/ngrok-api-go/v5"
"github.com/ngrok/ngrok-api-go/v5/ip_policies"
"github.com/ngrok/ngrok-api-go/v5/ip_policy_rules"
)
func main() {
fmt.Println(example(context.Background()))
}
func example(ctx context.Context) error {
clientConfig := ngrok.NewClientConfig(os.Getenv("NGROK_API_KEY"))
policies := ip_policies.NewClient(clientConfig)
policyRules := ip_policy_rules.NewClient(clientConfig)
policy, err := policies.Create(ctx, &ngrok.IPPolicyCreate{})
if err != nil {
return err
}
fmt.Println(policy)
for _, cidr := range []string{"24.0.0.0/8", "12.0.0.0/8"} {
rule, err := policyRules.Create(ctx, &ngrok.IPPolicyRuleCreate{
CIDR: cidr,
IPPolicyID: policy.ID,
Action: ngrok.String("allow"),
})
if err != nil {
return err
}
fmt.Println(rule)
}
return nil
}
List all online tunnels
package main
import (
"context"
"fmt"
"os"
"github.com/ngrok/ngrok-api-go/v5"
"github.com/ngrok/ngrok-api-go/v5/tunnels"
)
func main() {
fmt.Println(example(context.Background()))
}
func example(ctx context.Context) error {
clientConfig := ngrok.NewClientConfig(os.Getenv("NGROK_API_KEY"))
tunnels := tunnels.NewClient(clientConfig)
iter := tunnels.List(nil)
for iter.Next(ctx) {
fmt.Println(iter.Item())
}
if err := iter.Err(); err != nil {
return err
}
return nil
}