Skip to Content
Author's profile photo Nabheet Madan

Demystifying use of Custom Script in SAP Cloud Platform API Management


The trigger for this blog is very a good SAP Cloud Platform API management course going on at openSAP.  While going through one of the unit where it was shown how to add different elements in API flow via policy designer for example quota, Verify API key etc. That is when it occurred to me can we have our custom logic added during the request cycle. That is where we found this Extension Policies option where we can add our own Javascript/Python code.  So i decided to dig more.

I decided to use our current ES5 SalesOrderSet call from the course and started to experiment.The moment you start challenges are the first thing which we always face so read on.

How do we find out the available variable names?

The first challenge for me was how will i read the request or response in the javascript file. On searching further and reading the great blogs by Sven Huberti found the way to read or write the data from or to the request or variables.

Sample code to read data from request

/* Variable name shall be available*/
context.getVariable(<Variable name here>);

Sample code to write data to request

/* Variable name if does not exist it will be created*/
context.setVariable(<Variable name>, Variable Value)

So finally we found out how to use the JavaScript the next step was to try it .So in our simple POC we are going to add a new variable value for incoming request and modify the response (delivery status) received from Gateway system. This POC had its own set of learning so read on.

Discovering Debugging API calls.

First we added the code for incoming request where i was setting the value of new variable. This variable can be used further.


The moment i added my code to JS for incoming request  the first thing i was faced with was 500 error. Although the error was pretty basic, request variable is not in JSON format so change it, the error was fixed. But i did not know whether my script was setting the  variable or not that is where i discovered debugging and found out my script was working.


Now we were able to add variable next step was to modify the delivery status text to a custom one in response. This was verified in the output.

var response = JSON.parse(context.getVariable("response.content"));
switch(response.d.DeliveryStatus ) {
   case 'D':
       response.d.DeliveryStatus = 'Delivered the goods';
   case 'C':
       response.d.DeliveryStatus = 'Cancelled the goods';
       response.d.DeliveryStatus = 'Goods Not available';

What is next

So after exploring Javascript Extension Policies option i will be trying out Service callout option to call any external API’s. I believe there are many hidden features which will be discovered in coming weeks of the Open SAP course. Will share the experience, feel free to provide your feedback open to all ears.

#Mustread API blog series by Divya just found out, great stuff going on.

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Sven Huberti
      Sven Huberti

      Hello Nabheet!

      great to read that my blog posts did help you in your journey to learning API Management!

      And thanks for your great post: it really shows how you can simply transform an API without having a complete team rewrite the actual backend API itself. Also, this is what developers want: simple to use APIs. No codes like "D" and "C", but understandble text!

      One last remark: I just noticed that you were referring to me as "Former Member": I am still alive and kicking, and still working at SAP! The "Former Member" tag may have been displayed during our transition to the new Blog System, so it's a mere technical glitch here.

      Have fun with the OpenSAP course and see you online or at the TechEd!


      Author's profile photo Nabheet Madan
      Nabheet Madan
      Blog Post Author

      Thanks Sven Huberti for the feedback! I actually wanted to refer it to your blog series but somehow got converted to former member did not notice, corrected it:).  #openSAP course for API is a must attend.



      Author's profile photo Ramana Reddy Reddy Moole
      Ramana Reddy Reddy Moole

      Please share any python script examples for getting context variables.