The ngrok JavaScript (Node.js) SDK is an open-source package that enables you to quickly and efficiently serve Node.js applications on the internet without the need to configure low-level network primitives like IPs, certificates, load balancers, or ports. You can think of it as the ngrok Agent CLI packaged as a JS library. This quickstart uses the ngrok JavaScript SDK to create an agent endpoint that forwards traffic from the internet to a Node.js app running on your local device, then secure it by requiring visitors to log in with a Google account to access it.
If you’d prefer to skip the boilerplate setup, you can clone the complete example from the ngrok-samples collection. In that case, you’ll just need to reserve a domain and add your auth token as described below to get started.

What you’ll need

1. 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 will forward 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 8080.
service.js
const http = require('http');

const server = http.createServer((req, res) => {
    res.writeHead(200, { 'Content-Type': 'text/html' });
    const html = `
    <html>
    <head><title>Test Page</title></head>
    <body><h1>Hello from Node.js HTTP Server!</h1></body>
    </html>
    `;
    res.end(html);
});

const port = 8080;
server.listen(port, () => {
    console.log(`Serving custom HTML at http://localhost:${port}`);
});
Navigate to the directory where this file is located and run the following command to start the server:
node service.js

2. Install the JavaScript SDK

Open a new terminal, then run the following command to install the JavaScript SDK:
npm install @ngrok/ngrok

3. Create a .env file

Create a .env file and add your ngrok auth token and reserved domain to it. This file needs to be in the same directory where you’ll put the script using ngrok’s JavaScript SDK. The following terminal commands will create this directory, navigate into it, and add the .env file there:
mkdir ngrok-javascript-demo && cd ngrok-javascript-demo
touch .env
Add your ngrok auth token and reserved domain to this file:
.env
NGROK_AUTHTOKEN=your_actual_auth_token_here
NGROK_DOMAIN=your_actual_domain_here
Finally, you’ll need to install the dotenv package to access your environment variable from your code.
npm install dotenv
If you don’t want to use a .env file, you can instead export your auth token as an environment variable in your terminal session, or pass the auth token directly to the forward method in your code.You can set your reserved domain directly in your endpoint file below.

4. Create your endpoint

Create your agent endpoint, which will forward 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
require('dotenv').config();
const ngrok = require('@ngrok/ngrok');

(async function() {
    const listener = await ngrok.forward({
        // The port your app is running on.
        addr: 8080,
        authtoken: process.env.NGROK_AUTHTOKEN,
        domain: process.env.NGROK_DOMAIN,
        // Secure your endpoint with a traffic policy.
        // This could also be a path to a traffic policy file.
        traffic_policy: '{"on_http_request": [{"actions": [{"type": "oauth","config": {"provider": "google"}}]}]}'
    });

    // Output ngrok url to console
    console.log(`Ingress established at ${listener.url()}`);
})();

// Keep the process alive
process.stdin.resume();

5. Test your endpoint

Test your endpoint by running the following terminal command:
node endpoint.js
This should print your reserved domain URL to the terminal. When you visit the URL, you should be prompted to log in with Google. After logging in, you’ll see your app or service. If you used the example app in this quickstart, you’ll 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 an create 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?