Accessing key value maps in Node.js

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

You can use the apigee-access module to get Edge key value map (KVM) data from within a Node.js application. This method of KVM retrieval is an alternative to using the Key Value Map Operations policy to retrieve KVM data in an API proxy flow.

KVMs provide long-term persistence of arbitrary key/value pairs that you can retrieve at runtime. For example, you can store quota values, OAuth access token expiration values, and OAuth refresh token expiration values in a KVM for each deployment environment, then retrieve the values in your code.

You can create a KVM at one of three scopes: organization, environment, and apiproxy. For example, if you create a KVM at the apiproxy scope for the "foo" proxy, only the "foo" proxy can access the KVM; or if you create a KVM at the "test" environment scope, all API proxies deployed in an organization's "test" environment can access the KVM, but none of the proxies deployed in the "prod" environment can access it.

After you create a KVM with the management API, management UI, or the Key Value Map Operations policy (see Long-term persistence caching), use the functions here to retrieve the data.

For a great KVM pattern, see https://community.apigee.com/content/kbentry/24906/a-pattern-for-caching-kvm-values.html.

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

Methods

The following methods work on both encrypted and unencrypted KVMs.


getKeyValueMap

var kvm = apigee.getKeyValueMap('kvm_name', 'scope');
var kvm = apigee.getKeyValueMap('kvm_name', 'api', 'proxy_name');

Retrieves a KVM at a particular scope (organization, environment, api, or revision). After the KVM object is returned, use the getKeys and get functions to return the key names or a specific key value.

Parameters:

  • kvm_name - The name of the KVM to access.
  • scope - The scope of the KVM. One of organization, environment, api, or revision.
  • proxy_name - For a scope of api only, the name of the API proxy.

Returns:

A KVM object.

Example:

var apigee = require('apigee-access');
var kvm = apigee.getKeyValueMap('my_kvm', 'environment');

If you set scope to api then a third parameter is required - the name of the API proxy. For example:

var kvm = apigee.getKeyValueMap('my_kvm', 'api', 'myApiProxy');

getKeys

var kvmKeys = apigee.getKeys(function(err, keys_array);

Returns an array containing the names of all keys in the KVM.

Parameters:

  • callback: (Required) The callback function uses two parameters:
    • An Error object if the operation fails.
    • An object (keys_array in the example above) that represents the array of KVM key names.

Returns:

An array of KVM key names.

Example:

var apigee = require('apigee-access');
var kvm = apigee.getKeyValueMap('my_kvm', 'environment');
  kvm.getKeys(function(err, keys_array) {
    // use the array of key names here
}); 

get

kvm.get('key', function(err, key_value));

Gets the value of a KVM key.

Parameters:

  • key: (Required) A string that uniquely identifies the item in the cache.

  • callback: (Required) The callback function uses two parameters:

    • An Error object if the operation fails.
    • The object containing the KVM key value as a String.

Example:

var apigee = require('apigee-access');
var kvm = apigee.getKeyValueMap('my_kvm', 'environment');
  kvm.get('foo_key', function(err, key_value) {
    // Use the key value here. For example the following assigns the value
    // to a 'kvmvalue' variable in the response, which can be used by policies:
      apigee.setVariable(response, 'kvmvalue', key_value);
});