Getting Started with Cloud Endpoints and Traffic Policy via API
Prerequisites
- ngrok Account - Ensure you have an active ngrok account.
- ngrok Agent - Download and install the ngrok agent for your operating system.
- ngrok API Key - Create an API key via the dashboard.
- A Reserved Domain - You'll need to reserve a domain via the dashboard.
Step 1: Add your API key to the Agent
Create an API key on your ngrok dashboard and then run the following in your terminal:
ngrok config add-api-key <your-api-key>
Step 2: Create your Traffic Policy file
Create a file named policy.yml
with the following contents:
on_http_request:
- actions:
- type: custom-response
config:
status_code: 200
content: "Hello, World!"
This will be the Traffic Policy used on your Cloud Endpoint to respond to each HTTP request with a simple “Hello, World!” message.
Step 3: Create your Cloud Endpoint via API
Create your Cloud Endpoint via the API by running the following in your terminal (make sure to replace <your-reserve-domain>
with your reserved domain):
ngrok api endpoints create \
--url https://<your-reserve-domain> \
--traffic-policy "$(<policy.yml)"
Step 4: Test it out
Once you have created your Cloud Endpoint you can now open the URL in your web browser. You should see the "Hello, World!" message displayed in your browser.
Next Steps
🎉 Congratulations!
You've successfully set up your first Cloud Endpoint with a custom Traffic Policy via the API.
To learn more about ngrok's Traffic Policy and it's capabilities, check out the following resources:
- Learn about the core concepts like phases and rules.
- Check out the examples, use-cases and guides.
- The list of available actions, macros and variables you can use.