How to create a REST API with a custom query and processing?
EN | ES
Intro
In this tutorial we will see how to create a method in a REST API with parameters and with a post-processing.
Create APIÂ
For the example we will use the public ontology HelsinkiPopulation.
To do this:
We will access the CloudLab Environment Control Panel with a Developer user:Â
https://lab.onesaitplatform.com/controlpanel
We will go toÂ
My APIs section in the DEVELOPMENT menu and select CREATE.
3) In the API data, we will select the ontologyÂ
HelsinkiPopulation:
 Â
Create QUERY for API
Then, in Operations, we will markÂ
QUERY (CUSTOM)Â to create a custom operation:
 Â
As we are working with the HelsinkiPopulation ontology, which returns the population in Helsinki by years (we can see it by going to the Query Tool):
 Â
We will make a method that returns the population when we pass the year as a parameter. The query would be:
 Â
We return to the operation and complete the data of the operation, to pass the year as a parameter, we will mark it like this {$ year}
 Â
The platform will ask us for the data type of the parameter and whether it is a query in SQL or not:
 Â
We select SAVE the API.
Invoke the API
Now we can invoke it with a User Token. We can obtain the User Token from the option:Â .
 Â
  To invoke it, we will select the SWAGGER option:
 We will select HTTPS in the invocation:
 Â
 We will select the method:
 Â
 and click . This will request the User Token and the year:
 Â
  When invoking it, we will get the results:
 Â
Craate a Processing for the APIÂ
Now let's see how to add a post-processed to this method. For that, we will edit the API method:
 Â
  and enable the Process by selecting  . This will enable the processing of the method in JavaScript (via Nashorn).
  The data results of the invocation to the method are returned in an attribute called data and we must end up returning a String. An example processing is generated.
 Â
 We will modify this process to keep only the year and the population, by deleting the root element:
 Â
Upon invoking the API again, we will now get:
 Â