Create a Prototype API with an Inline Script¶
Generally, you would need to create APIs with inline scripts for testing purposes. You can deploy a new API or a new version of an existing API as a prototype. Thereby, this provides subscribers an early implementation of the API that they can try out and test without a subscription or monetization, and provide feedback to improve the API. After a period of time, the publishers can make changes that the users requested and publish the API.
Let's create a prototyped API with an inline script, deploy it as a prototype, and invoke it using the API Console, which is integrated in the API Developer Portal.
Step 1 - Create a Prototype API with Inline Script¶
-
Sign in to the API Publisher using
admin
as the username and password.https://<hostname>:9443/publisher
Example:
https://localhost:9443/publisher
-
Click CREATE API, and click Design a new REST API.
-
Provide the API name, context, and version. Click CREATE.
For this example, provide the following values.
Name Location_API Context /location Version 1.0.0 You are directed to the API Overview page.
-
Click Resources to navigate to the Resources page.
Select the HTTP Verb, provide the URI Pattern and click on + to add a new resource.
HTTP Verb GET URI Pattern /{town} Tip
After the new Resource is added, delete the default Resources (/*) by clicking on the Delete Button [1] of each resource. Or select all the resources at once by clicking on Select all for Delete [2] button.
-
Expand the newly added resource.
Note that the path parameter named
town
is set in the Parameters section.Tip
To specify multiple parameters in the API resource, separate the parameters with a forward slash in the URI Pattern.
{param1}/{param2}
-
Click SAVE to save the API.
-
Click Endpoints to navigate to the Endpoints page.
-
Select Prototype Implementation in the Prototype Endpoint card, which is in the Select an Endpoint Type to Add page, and click ADD.
Note that this page has been prompted because no endpoints have been added to the API yet.
You will be directed to the endpoints page.
Note
The inline JavaScript engine does not provide support for SOAP APIs. If you opt for the endpoint implementation method instead of inline, you need to provide an endpoint to a prototype API. For example,
<http://ws.cdyne.com/phoneverify/phoneverify.asmx>
-
Expand the
GET
method and enter the following as the script.This script reads the payload that the user sends with the API request and returns it as a JSON value. The value mc is the message context.
mc.setProperty('CONTENT_TYPE', 'application/json'); // Set the content type of the payload to the message context var town = mc.getProperty('uri.var.town'); // Get the path parameter 'town' and store in a variable mc.setPayloadJSON('{ "Town" : "'+town+'"}'); // Set the new payload to the message context.
-
Click SAVE to save the API.
Step 2 - Deploy the API as a prototype¶
- Click Lifecycle to navigate to the Lifecycle page.
- Click DEPLOY AS A PROTOTYPE to deploy the API as a prototype.
[![](https://apim.docs.wso2.com/en/3.0.0/assets/img/learn/create-api-prototype-lc-page.png)](https://apim.docs.wso2.com/en/3.0.0/assets/img/learn/create-api-prototype-lc-page.png)
Step 3 - Invoke the API¶
-
Click View in Dev Portal to navigate to the API Developer Portal after the API is deployed.
!!! tip
You can invoke prototyped APIs without signing in to the API Developer Portal or subscribing to the API. The purpose of a prototype API is to advertise and provide an early implementation of the API for users to test.
The Location API opens in the Developer Portal.
-
Click Try Out to navigate to the API Console.
-
Expand the
GET
method, click Try it out. -
Give any value for the town (e.g., London) and click Execute to invoke the API.
Note that the payload that you gave as a JSON output appears in the response.
You have successfully created an API with inline script, deployed it as a prototype, and invoked it via the integrated API Console.
An API can also be prototyped by moving the API to the PROTOTYPED
state by changing the API lifecycle state and providing the prototype endpoints.
For more information, see the Deploy and Test Prototype APIs tutorial.
Info
Related Guides