Give this to your AI coding agent to complete the full quickstart automatically.
If you’d prefer to skip the boilerplate setup, you can clone the complete example from the ngrok-samples collection.
In that case, you just need to reserve a domain and add your auth token as described below to get started.
What you’ll need
- An ngrok account.
- Your ngrok auth token.
- Go installed on your machine.
You can check this by running
go versionin your terminal.
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 forwards online traffic to.If you don’t have an app to work with, you can create a minimal Go app using the following code to set up a basic HTTP server at port Navigate to the directory where this file is located and run the following command to start the server:
8080.service.go
Install the Go SDK
Open a new terminal, then run the following commands to create a new Go module and install the SDK:
Create a .env file
Create a Add your ngrok auth token and reserved domain to this file:Finally, install the
.env file and add your ngrok auth token and reserved domain to it.
This file needs to be in the same directory where you initialized your Go module in the previous step.
Run the following command to create the .env file:.env
godotenv package to access your environment variables from your code:Create your endpoint
Create your Agent Endpoint, which forwards public traffic to your app.Create a new file named
endpoint.go in the same directory as your .env file and add the following code.
This example:- Starts an Agent Endpoint that forwards from your reserved domain to your service running on port
8080. - Secures the endpoint with a Traffic Policy that requires authentication via Google OAuth.
This example uses ngrok’s default Google OAuth application.
To use your own, see the OAuth Traffic Policy Action documentation.
endpoint.go
Test your endpoint
Test your endpoint by running the following terminal command:This prints your reserved domain URL to the terminal.
When you visit the URL, ngrok prompts you to log in with Google.
After you log in, you see your app or service.If you used the example app in this quickstart, you see “Hello from Go HTTP Server!” displayed in your browser.
What’s next?
In this guide, you learned how to use the Go SDK to create an Agent Endpoint that forwards traffic to your localhost. You saw one way to implement a Traffic Policy directly in your codebase, including an action that adds authentication to your app with no configuration required. What else can you do with these features?- Dig deeper into Traffic Policies, which enable you to grant conditional access, send custom headers, rewrite URLs, and more with your endpoints.
- Learn more about the Agent SDKs to understand how the Go SDK works under the hood.
- Building with Gin? Serve your router through ngrok in one call using the Gin library.
- Check out the ngrok Go SDK repo for more code examples.
- If your use case calls for a centrally managed, always-on endpoint instead of one that’s only available when an agent is running, proceed to getting started with Cloud Endpoints.