Blog

How to Mock API Response in Azure API Management?

Introduction

Mocking provides a way for APIs to return sample response even when their backend is under development.

This enables app developers to proceed with implementation and testing and can be useful in many scenarios:

·       When the API façade is designed first, and the backend implementation comes later or is worked upon in parallel

·       When the backend is temporarily not operational or not able to scale

Scenario

mock api response

In one of our recent BizTalk HL7 project, where we are loading the EDI 837 claims to SQL data ware house of a health care medical group as shown in the above diagram.

Once data is loaded to DW database, there are bunch of SSIS packages which runs to populate different kind of jobs and reports. These data are also used by operation department through internal web application. So, IT team raised a requirement for REST API to access patient details and claim data in their web app. The dotted box in the above diagram is the requirement.

Since we already have BizTalk middleware in place we decided to develop a BizTalk interface to pull the data and publish it as a REST API.

Database team did not want to give direct access to their table so they decide to create a view as per our requirement which BizTalk would have access to.

Now till we develop this API, front end team cannot proceed with implementation. They had dependency on BizTalk team. To get rid of this dependency, we thought of trying Mock Response in Azure API Management so that both the team can work parallelly on their respective implementation.

To begin, we had to come up with the following sample response.

Mock Response for a given memberId:

mock api response

Steps to create Mock Response in Azure Portal

1. Create a new Azure API Management service instance. In the Azure portal, select Create a resource > Enterprise Integration > API management.

mock api response

2.       Once your APIM service instance is created, select APIs from under API MANAGEMENT

mock api response

3.       Create a blank API 

mock api response

4.       Provide Display Name and select Unlimited Product and click on create.

mock api response

5.       Now add a new operation.

mock api response

6.       Provide Name, URL and Display Name. Click on Response tab as shown below

mock api response

7.       Click on Add response and select 200 OK. 

mock api response

8.       Select application/json as CONTENT TYPE under Representation as shown below.

mock api response

9.       Add the following JSON sample response in SAMPLE box as shown below. Then click Create

{

  “PatientId”: “ABC50920”,

  “MemberId”: “XYZ001”,

  “FirstName”: “RICHARD”,

  “LastName”: “CARDONE”,

  “DateOfBirth”: “09/20/1985”,

  “Gender”: “M”,

  “AddressLine1”: “2021 MORSE AVE”,

  “CityName”: “SAN LEANDRO”,

  “StateorProvinceCode”: “CA”,

  “ZipCode”: “95835”,

  “HealthPlan”: “BS”,

  “HealthPlanName”: “BLUE SHIELD OF CALIFORNIA”,

  “FullName”: “CARDONE,RICHARD “

}

mock api response

10.       Select the newly created operation (Member Detail) and then select the Form-based editor in Inbound processing as shown below.

mock api response

11.       Click on Mocking tab

mock api response

12.       Under Mocking tab, select Static response radio button. Then select 200 response as shown below.

mock api response

13. Click on Save

mock api response

14.       Now your mock response API is ready and it would look like as below. You can see the message “Mocking is enabled”

mock api response

Testing

Click on Test tab. Click on Send button.

mock api response

You should get the mock response as shown below.

mock api response

To test the API using postman, you need to copy the Request URL from the Test tab as shown below.

mock api response

You also need to provide the subscription key.

mock api response

Summary

API Management enables you to package and publish APIs to your internal developer team and external partner. You can build an API façade for your existing backend which can be managed and protected from misuse and abuse using API Management.

In this post we created a new API in the APIM and used it for the Mock response. Mocking can be very useful to return sample response even when their backend is under development.

This enables app developers to proceed with implementation and testing and can be useful in many scenarios:

·       When the API façade is designed first, and the backend implementation comes later or is worked upon in parallel

·       When the backend is temporarily not operational or not able to scale

Related Posts


VNB Consulting at Microsoft PPCC24 Conference, Las Vegas

September 24, 2024

Power Platform Conference 2024: Highlights & Key Takeaways

Hey Power Platform enthusiasts! We’re still buzzing from the electrifying Power Platform Community Conference (PPCC) in Vegas. It was a week overflowing with groundbreaking announcements, all centered around Power Platform and the transformative power of AI. Buckle up, because we’re about to dive into a treasure of updates that will supercharge your automation game! This

Power Platform Community Conference Banner

September 6, 2024

Power Platform Community Conference 2024 #PPCC24

VNB is returning to the Power Platform Community Conference #PPCC24 as an exhibitor for the third time in a row!