Mock your APIs in Azure API Management and Speed Up Development

With microservice architecture being as popular as it is today, being able to Mock your services makes it easy to get all your developers coding right away. We no longer have to wait for backend services to be completely developed or spend time coding throw away responses to provide our calling applications. If we know the interface of the service, we can enable our developers to proceed with implementation and testing of the APIM instance even if the backend is not available to send real responses. Ability to mock responses can be useful in a number of scenarios:

  • When the API façade is designed first and the backend implementation comes later. Or, the backend is being developed in parallel.
  • When the backend is temporarily not operational or not able to scale.

Azure API Management (APIM) makes it easy to Mock your API responses, I’ll walk you through the steps below and hopefully you have an APIM instance setup to test yourself:

  • Browse to the Azure Portal
  • Open an API Management instance.
  • Click on APIs.
  • Click on an API, mine is called Echo API
  • Once you click on your API you should see a page similar to this
  • Click on the Operation that you would like to Mock, I choose one of the Get Operations to test.
  • Click on the pencil next to Frontend so we can modify and set our sample response
  • Now click on the Response Tab on the lower Menu, since a 200 OK response already exists I will click on that response; however if your API doesn’t have one already you can click Add Response.
  • Click on Add Representation
  • Now, we enter our content type and sample response. I chose to use JSON, so it looks like this
  • Make sure to click Save
  • Now if you test your API you still won’t get your Mock Response
  • The last step is to add a policy to your Operation to enable mocking
  • To do this Click on the Add Policy in your Inbound Processing
  • Select Mock Responses
  • I mocked the 200, OK so I didn’t need to make any changes. But you might want to mock a 400 error so you would have that selected.
  • Click Save
  • Now click Test on the Menu
  • Enter Parameters if needed
  • Click Send and if everything was setup properly and parameters were passed you will see your Mock Response

Hopefully, this helped and if you have any questions feel free to reach out. APIM Policies are really powerful and can be used for other tasks like validating JWT tokens, handling errors, rate limiting. Come back to learn more!