RESTwithUS variables work in many ways like traditional programming language variables: You create a variable, select a suitable name for it (e.g. name variable CustomerId
if you plan to store the id of the customer there), assign some value to the variable and then use the value elsewhere in the application.
This allows you to:
You can set the variable value in many places ranging from provider settings to code. Let's have a look at the options:
If there is a value used in every provider operation (or is just used repeatedly) – and it is prone to change in the future – best way is to store it in provider variables. A typical example is API keys: they are the same for every provider operation, you may need to change them in the future, plus it’s a sensitive information.
As an added value consider, that you will be storing the value in one place – so if there comes the time to change an API key, you just need to change it in one place and you're done.
To set provider variables select your API provider fron the list and open Related / Variables:
Now enter all provider variables and their values:
Operation variables can be set in the same way like provider variables, just this time select your operation from the list and open Related / Variables:
This time the variable value will be used for the selected operation only – plus you can't use Hide Value and Do not Overwrite Value options here.
The idea here is to save the contents of a JSON node into a variable – and use the variable later in the operation. Let's say you have following JSON response and you want to save the countryCode
node value to a variable and work with it later. Select the countryCode
node and open Details / Details from menu:
Go to the Variables tab and fill in your variable name into Variable Name field:
Tip: You can set a variable value like this in Request, Response and even in the Entity schema.
You can use ADD_VARIABLE
function from API Script RWU codeunit to set the variable value with code.
If you want to use the variable in operation URL, URL parameters or headers, use the following code (with an empty string as a first parameter):
APIScriptRWU.ADD_VARIABLE('','CustomerId',1); //Sets the variable CustomerId = 1
If you want to use the variable in JSON request node, use the code like this (specify exact JSON node where you want to use the variable):
APIScriptRWU.ADD_VARIABLE('/name','CustomerName','Contoso Ltd.'); //Sets the variable CustomerName in name node
To get the variable value, use syntax:
{{!VariableName}}
(e.g. {{!ApiKey}}
) for a required variable. If any required variable in a request is empty, the operation will end up with error.{{VariableName}}
(e.g. {{ApiKey}}
) for an optional variable. Optional variables are just deleted from the operation, if they have no value.You can use variables almost everywhere: