Skip to main content
This guide provides an example of how to forward traffic to internal endpoints based on path using ngrok Cloud Endpoints. The Cloud Endpoint has a Traffic Policy to route traffic to one of two internal endpoints depending on the path, but each internal endpoint also has its own Traffic Policy to independently handle incoming traffic.

Core concepts

  • Cloud Endpoints are centrally managed endpoints in the cloud that you can use to route traffic to Agent Endpoints.
  • Internal endpoints are endpoints that aren’t publicly accessible and are only reachable from within your network by Cloud Endpoints via the forward action.
  • Path-based routing involves directing incoming traffic to different destinations based on the path component of a URL.

Prerequisites

To follow this guide, you need a local computer with ngrok installed by following the installation guides. If you’re following along using the ngrok CLI, you need: If you’re following along using CURL, you need:
  • An ngrok API key as an environment variable named NGROK_API_KEY.
1

Create an internal endpoint for the homepage

Start by creating an internal endpoint for the homepage endpoint, which handles all non-API traffic.
After running, you should see the endpoint with an “online” status at your domain. It routes traffic to the app running at your local port 80, which you can set up to have any HTTP server running.
2

Create an internal endpoint for the API

Next, create another internal endpoint, this one for all traffic to the /api/ path.Use a Traffic Policy to simulate accessing an API endpoint.
The above Traffic Policy denies all requests that don’t have an API-Key header, and returns a 200 status code with a custom response for all other requests.Save the Traffic Policy as a file locally in your directory. Now, create an internal endpoint based on this Traffic Policy.
After running, you should see the endpoint with an “online” status for your endpoint.
3

Create a domain for the Cloud Endpoint

Next, create the domain or TCP address that the Cloud Endpoint resides on. For the purpose of this guide, create an ngrok HTTP domain.
  • Replace NGROK_SUBDOMAIN with the subdomain you’d like to use for this guide.
After running, you should see the following:
skip-validation
When you have completed this step, you can move on to the next step.
4

Create a Traffic Policy for your Cloud Endpoint

Now, create a Traffic Policy for the Cloud Endpoint. This Traffic Policy preprocesses all incoming traffic before ngrok forwards it to the internal endpoints, which may or may not have their own independent Traffic Policies. This separation gives you granularity and full control over how ngrok handles traffic.
The Traffic Policy simply forwards traffic to one of the two internal endpoints based on the path. You can create more complex traffic handling at your discretion.
5

Create an ngrok Cloud Endpoint

Now create an ngrok Cloud Endpoint that points to the newly created internal endpoint. You can create Cloud Endpoints via the API or the ngrok dashboard.
Use the URL from Step 2 as the url value in the Traffic Policy, and the Traffic Policy from Step 3 as the traffic-policy value. This request sets the binding to public so anyone can access the Cloud Endpoint.Due to the shell’s difficulty in parsing a more complex string that includes both ' and ", this example uses Postman to send a POST request to the ngrok API instead of using the ngrok api command.
  1. Open Postman and create a POST request to https://api.ngrok.com/endpoints.
  2. In the Authorization tab, select API Key.
    • Set Key as Authorization
    • Set Value as Bearer NGROK_API_KEY
    • Set Add to as Header
  3. In the Headers tab, add a new header:
    • Create a header with the key Content-Type and value application/json
    • Create a header with the key Ngrok-Version and value 2
  4. In the Body tab, create a raw JSON:
  • Replace YOUR_NGROK_API_KEY with your ngrok API key.
  • Replace NGROK_SUBDOMAIN with the value used in Step 3.
For more details on using the API to create a Cloud Endpoint, see the API documentation for endpoints.

Conclusion

You’ve successfully created a Cloud Endpoint that you can access from the URL you reserved: ${NGROK_SUBDOMAIN}.ngrok.app On most paths, the Cloud Endpoint refers to its Traffic Policy and forwards traffic to the homepage internal endpoint, which exposes the app running on local port 80. For /api/ paths, the Cloud Endpoint forwards traffic to the API internal endpoint. It requires an Api-Key header to access, and denies all other requests. You won’t be able to access the API normally from the browser, but you can use CURL or Postman to test it.