I think you want
var timeToExpire = 300;
var expirationTime = issuedAtTime+timeToExpire;
print("The expiration time is "+expirationTime);
// var expTime = context.setVariable("expTime", context.getVariable("expirationTime")); NO
context.setVariable("expTime", expirationTime);
// The following is not really necessary, for funcitonal purposes.
// It is interesting only for diagnostics I guess.
var expTime = context.getVariable("expTime");
print("The exp time is "+expTime);
When you are discussing Apigee JS callouts, there are two “scopes” or “contexts” in which variables exists.
One is the “message context” in the API Proxy. All the Apigee policy types work by reading and setting context variables, sometimes called “flow variables”. For example if you use OAuthV2/VerifyAccessToken and it succeeds, the policy sets a number of context variables with information about the token, the api product, the expiry of the token, and so on .
Subsequent policies can “read” these variables by referencing them in message templates or ref=‘’ attributes in the policy configuration. Or, you can refer to these context variables in Condition elements inside your Apigee proxyendpoint or targetendpoint.
a JavaScript step has a distinct “scope” for variables during its execution. We can declare and initialize variables with the var statement, and you can update variables with simple JS assignment syntax. Eg
// initialize a variabled named myVariable
var myVariable = 7;
// update the value of myVariable
myVariable = myVariable * 12;
The variables inside JS and the variables inside the message context are in different scopes. Within a JS module, you can “read” a context variable with context.getVariable() and you can write a contextVariable with context.setVariable().
// 1. get a variable from message context and set a local JS variable with its value
var jsVar = context.getVariable('request.verb');
// jsVar will now hold one of: 'GET', 'POST', 'DELETE', etc.
// depending on the request that is being served.
// 2. set a variable into message context with a value known in local js context.
var localJsValue = (new Date()).valueOf() / 1000; // epoch seconds
context.setVariable('calculated_now', localJsValue);
// subsequent policies or Condition elements in the Apigee proxy can refer to
// a variable named 'calculated_now'
in your case the expirationTime variable is known in JS scope. It seems like you want to set a context variable holding the same value. So
// Set the value of a context variable named expTime to
// the current value of the variable in JS scope, named expirationTime.
context.setVariable('expTime', expirationTime);
For the logic you used,
var expTime = context.setVariable("expTime", context.getVariable("expirationTime")); // wrong
…what you are doing with context.getVariable is reading the variable expirationTime from message context. It has not been previously set in message context. (Though, coincidentally in your JS there is a local JS variable with the same name.) When this JS executes, it causes a GET variable action to appear in the debugsession - you can see that line in your trace snapshot. And you can see the value is empty - because there is no such variable in the message context, the call to context.getVariable() returns an empty value. Then you use context.setVariable to set a context variable named expTime to the same thing you just retrieved (which is nothing). So expTime in the message context gets the value null, or empty.
Finally you are setting the local JS variable named expTime to the output of the context.setVariable() call, which I think is void (aka undefined). And remember the expTime that is known in the JS execution scope is different that the similarly named variable in the message context.
So that is why you see undefined in your print statement output.