WSO2 API Manager- How to Prototype an API

Prototyping an API means you can test an API without having a real backend. API will behave in the same way like with a real backend and will respond to the client with some prototyped data.

I will be using API Manager 2.0.0 here in this article.

First login to APIM Publisher. And start creating a new API

Create new API

Select Design New API and Click Start Creating button

Design New API

Fill your api name, context, version etc and create api resources you need.

Here I’m creating an API called Student API and I’m defining one GET resource as getstudentinfo/{studenid}. What we expect here is to get student info by passing studend id.

Basic details about your API
getstudentinfo GET resource defined

Go to Implement stage, Select Prototyped API and select Inline radio button.

You will get to see all the resources you defined in previous step. By clicking on each resource, you will be able to see the inline editor to put your inline script in.

Select Inline

Now use your javascript knowledge to play with it :)

Here are 2 samples if you would like to refer.

Example 01

mc.setProperty('CONTENT_TYPE', 'application/json');
var
studentid =mc.getProperty('uri.var.studentid');
mc.setPayloadJSON('{"studentid":"' + studentid + '", "studentname":"Mahesh Chinthaka","year":"grade 10","sex":"male"}');
Click on the resource and add script

Click Deploy as Prototype button and then go to APIM store. Invoke the api and see the response :)

Response after invoking the api

Example 02

mc.setProperty('CONTENT_TYPE', 'application/json');
var
studentid =mc.getProperty('uri.var.studentid');
var response = [];
var marks = [];
var subject1 = new Object();
subject1.name = "English";
subject1.marks = "80";
var subject2 = new Object();
subject2.name = "Maths"
subject2.marks = "88"
marks.push(subject1);
marks.push(subject2);
var student  = new Object();
student.id = studentid;
student.marks = marks;
response.push(student);
mc.setPayloadJSON(response);
Response for example 02 script

See you ! :)