You're viewing **Apigee Edge** documentation.  
[Go to the
**Apigee X** documentation](http://cloud.google.com/apigee/docs/api-platform/reference/variables-reference). info

> [!WARNING]
> **Private Cloud Deprecation Notice:**
>
> **Support for [Node.js](https://docs.apigee.com/api-platform/nodejs/overview-nodejs-apigee-edge) proxies
> and [secure vaults](https://docs.apigee.com/api-platform/nodejs/using-secure-store)**
>
> Since 2013, Apigee has supported the
> [use of Node.js proxies within Apigee
> Edge](https://www.programmableweb.com/news/apigee-api-platform-now-supports-node.js-allowing-developers-to-build-highly-customized-apis-and-apps/brief/2013/10/25) through the use of [Trireme](https://github.com/apigee/trireme), which enables you to run your Node.js code
> within Apigee Edge for Private Cloud and expose it as an API.
>
>
> Apigee recommends Private Cloud customers take the following steps:
>
> - Migrate data in secure vaults to [Encrypted Key Value Maps](https://docs.apigee.com/api-platform/cache/key-value-maps#aboutencrypted) (KVM) to access sensitive data from an API proxy.
> - Migrate Trireme (Node.js) based API proxies to a native Node.js environment outside of Apigee.
>
>
> See also [Apigee deprecations and retirements](https://docs.apigee.com/release/deprecated-features).

> [!WARNING]
> **Public Cloud Retired feature:** The traditional, Trireme-based
> Node.js support on Apigee Edge Cloud is retired and, by extension,
> [apigee-access](https://docs.apigee.com/api-platform/nodejs/using-apigee-access) is also
> retired because it is only used in Trireme-based Node.js applications deployed on Apigee Edge.
> Trireme-based Node.js was retired on October 10, 2019. See
> [Trireme-based Node.js End of Life](https://docs.apigee.com/release/notes/trireme-nodejs-eol).

## Introduction

Use the `apigee-access` module to access Apigee Edge **flow
variables** in a Node.js application. The module has methods for [getting](https://docs.apigee.com/api-platform/nodejs/access-flow-variables-nodejs#getVariable), [setting](https://docs.apigee.com/api-platform/nodejs/access-flow-variables-nodejs#setVariable), and [deleting](https://docs.apigee.com/api-platform/nodejs/access-flow-variables-nodejs#deleteVariable) variables. It also has a convenience method for [setting an integer variable](https://docs.apigee.com/api-platform/nodejs/access-flow-variables-nodejs#setIntVariable).

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](https://docs.apigee.com/api-platform/fundamentals/flow-variables-and-conditions).

For an introduction to the `apigee-access` module and its other features, see
[Using the apigee-access module](https://docs.apigee.com/api-platform/nodejs/using-apigee-access).

## 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.

```javascript
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
```

> [!NOTE]
> **Note:** For information about using `apigeetool` to deploy standalone Node.js applications like this one, see [Deploying a standalone Node.js
> app](https://docs.apigee.com/api-platform/nodejs/deploying-standalone-nodejs-app).

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

```javascript
<?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:

```javascript
<?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:

```javascript
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](https://docs.apigee.com/api-platform/nodejs/access-flow-variables-nodejs#getVariable)
- [setVariable](https://docs.apigee.com/api-platform/nodejs/access-flow-variables-nodejs#setVariable)
- [setIntVariable](https://docs.apigee.com/api-platform/nodejs/access-flow-variables-nodejs#setIntVariable)
- [deleteVariable](https://docs.apigee.com/api-platform/nodejs/access-flow-variables-nodejs#deleteVariable)

*** ** * ** ***

### 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](https://docs.apigee.com/api-platform/reference/variables-reference). For variable types
created by policies, refer to the specific policy reference topic.

**Examples:**

```javascript
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](https://docs.apigee.com/api-platform/reference/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:**

```javascript
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:**

```javascript
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](https://docs.apigee.com/api-platform/reference/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:**

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