Accessing flow variables in Node.js

You're viewing Apigee Edge documentation.
Go to the Apigee X documentation.
info

Introduction

Use the apigee-access module to access Apigee Edge flow variables in a Node.js application. The module has methods for getting, setting, and deleting variables. It also has a convenience method for setting an integer variable.

Flow variables exist within the context of an API proxy flow. Some variables are "built-in" to Edge. Others are created when policies execute, and you can create your own variables. Flow variables are typically used to pass data from one policy to another and for setting conditions in conditional flows. For information about flow variables, see Flow variables and conditions.

For an introduction to the apigee-access module and its other features, see Using the apigee-access module.

Working example

Imagine that an Edge policy running on the request flow path sets a variable called AuthenticatedUserId. The following code accesses that variable and prints it to a log. In addition, this code sets a variable. You can then access that variable from a policy, which we illustrate below.

var http = require('http');
var apigee = require('apigee-access');

http.createServer(function (request, response) {
  // The request parameter must be a request object that came from the http module
  var userId = apigee.getVariable(request, 'AuthenticatedUserId');
  apigee.setVariable(request, "custom.foo", "Bar");
  console.log('Authenticated Apigee User ID is %s', userId);
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.end('Hello World\n');
}).listen(8124);

console.log('Server running at http://127.0.0.1:8124/');

You can copy this code into a JavaScript file, deploy it to Edge, and try it out. Call the file server.js. To deploy it use:

apigeetool deploynodeapp -u username -p password -o myorg -e test -n access -d . -m server.js -b /access

After you deploy the application to Edge, add an AssignMessage policy with the following configuration to the ProxyEndpoint request flow:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="AddUserId">
    <DisplayName>AddUserId</DisplayName>
    <FaultRules/>
    <Properties/>  
    <AssignVariable>
        <Name>AuthenticatedUserId</Name>
        <Value>ntesla</Value>
        <Ref/>
    </AssignVariable>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

Next, attach another AssignMessage policy to the TargetEndpoint response preflow:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="SetHeader">
    <DisplayName>SetHeader</DisplayName>
    <FaultRules/>
    <Properties/>
    <Set>
        <Headers>
            <Header name="MySpecialHeader">{custom.foo}</Header>
        </Headers>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

You can call the proxy like this:

curl -i http://myorg-test.apigee.net/access

Now, go to the access proxy's page in the management UI and bring up the Develop view. Click Node.js Logs to view the log output from the proxy. If the proxy is configured properly, you will see that the userId variable was set. You will also see the that the header was set in the cURL output in your terminal window:

HTTP/1.1 200 OK

Content-Type: text/plain
Date: Tue, 27 05 2014 23:20:52 GMT
MySpecialHeader: Bar
Content-Length: 12
Connection: keep-alive

Methods


getVariable

var result = getVariable(httpRequest, name);

Gets a named variable.

Parameters:

  • httpRequest: The request object that comes from the http module.
  • name: (String) The name of the variable to retrieve.

Returns:

A string or a number, depending on the type that was set using setVariable(), when it was created by you elsewhere, or when a policy created it. If you are accessing one of the out-of-the-box Edge variables, you can find a list of types in the Variables Reference. For variable types created by policies, refer to the specific policy reference topic.

Examples:

var apigee = require('apigee-access');
    // "httpRequest" must be a request object that came from the http module
    var val1 = apigee.getVariable(request, 'TestVariable');
    var val2 = apigee.getVariable(request, 'request.client.ip');

setVariable

setVariable(httpRequest, name, value);

Sets a variable. Some variables are read-only, and the setVariable() method throws an exception if you try to set one of them. To determine which variables are read-only, see the Variables Reference.

Parameters:

  • httpRequest: The request object that comes from the http module.
  • name: (String) The name of the variable to retrieve.
  • value: Can be a number, String, boolean, null, or undefined.

Example:

var apigee = require('apigee-access');
    apigee.setVariable(request, 'TestVariable', 'bar');
    // This will throw an exception because client.ip is read-only.
    apigee.setVariable(request, 'client.ip');
    

setIntVariable

setIntVariable(httpRequest, name, value);

The setIntVariable() method is a convenience method that first coerces the value parameter to an integer, and then sets it.

Parameters:

  • httpRequest: The request object that comes from the http module.
  • name: (String) The name of the variable to set.
  • value: The value parameter must be a string or number.

Example:

var apigee = require('apigee-access');
// Convert "123" to an integer and set it
apigee.setIntVariable(request, 'TestVariable', '123');
// Use something that's already a number
apigee.setIntVariable(request, 'TestVariable2', 42);

deleteVariable

Deletes a named variable. It is an error to delete a read-only variable. For a complete list of read-only variables, see the Variables Reference.

deleteVariable(httpRequest, name);

Parameters:

  • httpRequest: The request object that comes from the http module.
  • name: (String) The name of the variable to delete.

Example:

  apigee.deleteVariable(request, 'TestVariable');
    // This will throw an exception because client.ip is a read-only variable.
    apigee.deleteVariable(request, 'client.ip');