WSO2 is now Choreo LabsLearn more

Trusted AI Governance
Build

Create agents and AI apps with the tools and languages your teams already use.

Connect

Link agents securely to your APIs, data, models and business systems.

Run

Deploy, observe and operate agents and AI apps reliably at any scale.

Govern

Control identity, access, cost and policy for every agent in production.

Agent Management

Developer Platform

  • API Management Cloud

    Design, secure and publish APIs with governance and analytics built in.

    Sign Up Now
  • Integration Cloud

    Connect apps, data and AI agents with low-code and pro-code integration.

    Sign Up Now
  • Identity & Access Management Cloud

    Secure sign-in and access for customers, partners, workforce and agents.

    Sign Up Now
  • Agent Management Cloud

    Build, run and govern AI agents with full visibility and control.

    Sign Up Now
  • Developer Platform Cloud

    Ship cloud-native apps faster on an internal developer platform.

    Sign Up Now

Self-Hosted

Full control over your stack. Deploy directly to your own servers, bare metal, cloud, or Kubernetes environment. Your data never leaves your perimeter.

Product Downloads
Choreo Labs
AI Governance
Platform
Solutions
Resources
Blog

Retrieving REST parameters in JavaScript API prototypes

JavaScript prototypes are a great way to quickly try your API ideas and collect community feedback while working on the proper longterm implementation.

And good prototypes obviously produce meaningful outputs based on supplied parameters. In this blog post, we will show how you can access REST parameters from the prototype code.

There are two kinds of REST parameters:

WSO2 API Cloud obviously supports both. Let's have a look at how you deal with them:

Path Parameters

You add path parameters in {} brackets when supplying URL Pattern for your API definition:

Adding path parameter

To edit the actual prototype implementation, on the Implementation step, you pick Prototyped API and the Inline option:

Path parameter prototype code

Here's the code sample that we used:

mc.setProperty('CONTENT_TYPE', 'application/json');
var id = mc.getProperty('uri.var.id');

mc.setPayloadJSON('{ "data" : "' + id +'"}');

As you can see, we retrieve the path parameter as uri.var.id property.

Once we publish the API as prototype, we can invoke it in API Store and see it working:
Path parameter invoked

Query Parameter

Query parameters are great for optional parameters that are named and can be used in any order. These are the parameters, added in the end of the query in ?parameter=value format.

You add these parameters using the Add Parameter button for the API resource:

Adding query parameter

On the Implementation step, we pick the Prototyped API / Inline option and provide the JavaScript code:

Query parameter prototype code

As you can see, the code is very similar to the one we used for path parameter. The only difference is the way we retrieve the parameter. This time, we address it as query.param.mask:

mc.setProperty('CONTENT_TYPE', 'application/json');
var mask = mc.getProperty('query.param.mask');

mc.setPayloadJSON('{ "data" : "' + mask + '"}');

Once you deploy the prototype, you can invoke it in API Store and see it working:

Query parameter invoked

Have an API to prototype? Try it in WSO2 API Cloud!