Core Concepts
- Cloud endpoints are centrally managed endpoints in the cloud that can be used to route traffic to agent endpoints.
- Internal endpoints are endpoints that are not 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 will need a local computer withngrok
installed by following our installation guides.
If you are going to be following along using ngrok CLI, you will need:
- An ngrok API key configured on your ngrok agent.
- An ngrok API key as an environment variable named
NGROK_API_KEY
.
Step 1 — Create an Internal Endpoint for the Homepage
Let’s start by creating a internal endpoint for the homepage endpoint, which will handle all non-API traffic.Step 2 - Create an Internal Endpoint for the API
Next, let’s create the another internal endpoint, this one for all traffic to the/api/
path.
We will use a traffic policy to simulate accessing an API endpoint.
API-Key
header, and return a 200 status code with a custom response for all other requests.
Save the traffic policy as a file locally in your directory. Now, let’s create an internal endpoint based on this traffic policy.
Step 3 - Create a Domain for the Cloud Endpoint
Next, let’s create the domain or TCP address that the cloud endpoint will reside on. For the purpose of this guide, we will create an ngrok HTTP domain.- Replace
NGROK_SUBDOMAIN
with the subdomain you’d like to use for this guide.
skip-validation
Step 4 — Create a Traffic Policy for your Cloud Endpoint
Now, we’ll continue and create a traffic policy for the cloud endpoint. All incoming traffic will be preprocessed by this traffic policy, before being forwarded to the internal endpoints which may or may not have their own independent traffic policies. This separation allows for granularity and full control over how traffic is handled.Step 5 — Create an ngrok Cloud Endpoint
Now we can create an ngrok Cloud Endpoint that points to our newly created internal endpoint. Cloud Endpoints can be created via the API or ngrok dashboard.We 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.
The binding is set to public so anyone can access our cloud endpoint.Due to the shell’s difficulty in parsing a more complex string that includes both '
and "
, we will use Postman to
send a POST request to the ngrok API instead of using the ngrok api
command.- Open Postman and create a POST request to
https://api.ngrok.com/endpoints
. - In the Authorization tab, select “API Key”.
- Set Key as
Authorization
- Set Value as
Bearer NGROK_API_KEY
- Set Add to as
Header
- Set Key as
- In the Headers tab, add a new header:
- Create a header with the key
Content-Type
and valueapplication/json
- Create a header with the key
Ngrok-Version
and value2
- Create a header with the key
- 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.
Conclusion
You’ve successfully created a cloud endpoint that can be accessed from the URL you reserved:${NGROK_SUBDOMAIN}.ngrok.app
On most paths, the cloud endpoint will refer to its traffic policy and forward traffic to our homepage internal endpoint, which exposes the app running on local port 80.
For /api/ paths, the cloud endpoint will forward traffic to our API internal endpoint. It requires an Api-Key
header to access, and denies for 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.