API Design First approach: Implementing quick mock API's using swagger hub and postman

Consider a scenario, where you as a API developer been ask to to implement the API's for a new mobile/web application team. The requirement is to implement a application to allow users to manage orders.

        Mobile APP -> API infra->Backend enterprise system

While implement a full working API might take days, you also do not want the application team to wait till the API's are ready. Thus, in most case we end up implementing the mock API's with sample scenarios.

In this post, we will define an API using swagger openapi 3.0 yaml specification in swaggerhub. We will then use the definition to implement mock API's in postman.

Even though postman provide option to define the API's, i liked swagger hub much better, it allow you to view your API on right side. 

For this post I implemented API definition in swagger, please find the below link.

https://app.swaggerhub.com/apis-docs/lopesrohan1988/ordermanagement/1.0.0

API Definition can be found at below link:

https://github.com/lopesrohan1988/CloudNaiveApp/blob/main/API/ApiSpec.yaml


Once you have the API definition agreed with all the stack holders, next step is to implement mock API's. 

You can follow below steps to setup mock api's:

1. Login to Postman.

2. Create API by copying the API definition from SwaggerHUB



3. Click on Generate Collection button and select API Mocking option












4. Now click on collections, and under your collection you will find the mock auto generated responses based on the API definition.

You can modify the responses or even add new responses based on the input parameter.

For example, I added 2 success response for order # 100 and 200 and 1 not found response for order # 300.

Example:






5. Once the Mock server is setup, you can copy the mock server URL and update the swagger definition on SwaggerHub. This will allow you to test your API's from SwaggerUI with mock server as a backend.








Also, since the mock collection is maintained in postman, it is very easy to change or added new mock responses. There are lot of  API design products, but I found it very easy and quick to get it implemented using postman & swaggerhub.


https://www.postman.com/

https://app.swaggerhub.com/



Popular posts from this blog

JAVA embedding in Oracle SOA 12c

Passing/Receiving HTTP header in SOA BPEL

Integrating Weblogic with IBM MQ over JMS