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.
- Node.js installed on your machine.
You can check this by running
node -vin 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 Node.js 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.js
Install the JavaScript SDK
Open a new terminal, then run the following command to install the JavaScript 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 put the script using ngrok’s JavaScript SDK.
The following terminal commands create this directory, navigate into it, and add the .env file there:.env
dotenv package to access your environment variable from your code.Create your endpoint
Create your Agent Endpoint, which forwards public traffic to your app.Create a new file named
endpoint.js 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 Google OAuth authentication.
This example uses ngrok’s default Google OAuth application.
To use your own, see the OAuth Traffic Policy Action documentation.
endpoint.js
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 Node.js HTTP Server!” displayed in your browser.
What’s next?
In this guide, you learned how to use the JavaScript 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 SDKs to understand how the JavaScript SDK works under the hood.
- Building with Next.js? Run
next devwith a public URL in one command using the Next.js library. - Read the full JavaScript SDK documentation 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.