How Do I Mock API Endpoints?
You can mock API responses using ngrok Endpoints. This enables you to test your application's functionality without having to stand up a full backend service or make requests to real APIs.
The following steps will walk you through using Traffic Policy to mock API responses.
1. Create a Traffic Policy
When requests are made to an Endpoint with a Traffic Policy, the policy can execute certain actions before returning a response or forwarding the traffic to downstream services. Using request variables, you can check many aspects of a request before sending a response, such as the value of its headers, the scheme, the user agent, and more.
The following example Traffic Policy checks the URL path of all incoming requests, then returns a custom response if the path matches /api/example
:
Loading…
2. Apply the Traffic Policy to an Endpoint
There are multiple ways to apply the Traffic Policy from the previous section to your Endpoints. How you should do so depends on the type of Endpoint you're using.
Agent Endpoints
Use the following terminal command to edit your ngrok config file:
Loading…
See the guide on using Traffic Policy with a config file for more information.
Cloud Endpoints
- Navigate to the Endpoints page.
- Select the Cloud Endpoint you want to use. You'll be taken to a page where you can edit the Traffic Policy directly.
3. Test the Traffic Policy
To test the Traffic Policy defined in this guide, send a request to your endpoint like this:
Loading…
The response should look like this:
Loading…