Skip to main content

Cheat sheet for Node.js with Momento Cache

If you need to get going quickly with Node.js + TypeScript and Momento Cache, this page contains the basic API calls you'll need. Check the Node.js SDK examples for complete, working examples including build configuration files.

Install the Momento client library

Install the client library in an existing Node.js project:

npm install @gomomento/sdk

Set up your auth token

You'll need a Momento auth token to authenticate with Momento. You can get one from the Momento Web Console. Once you have your token, store it in an environment variable so that the Momento client can consume it:

export MOMENTO_AUTH_TOKEN=<your Momento token here>

Note: it is best practice to put the token into something like AWS Secret Manager or GCP Secret Manager instead of an environment variable for enhanced security, but we are using one here for demo purposes.

Import libraries and create a CacheClient object

This code sets up the main function, pulls in the necessary imports, and instantiates the CacheClient that will be used to interact with your cache.

/* eslint-disable @typescript-eslint/no-unused-vars */
import {CacheGet, CreateCache, CacheSet, CacheClient, Configurations, CredentialProvider} from '@gomomento/sdk';

async function main() {
const cacheClient = await CacheClient.create({
configuration: Configurations.Laptop.v1(),
credentialProvider: CredentialProvider.fromEnvironmentVariable({
environmentVariableName: 'MOMENTO_API_KEY',
defaultTtlSeconds: 60,

main().catch(e => {
console.error(`Uncaught exception while running example: ${JSON.stringify(e)}`);
throw e; // Depending on the environment, this might not be necessary.

Create a new cache in Momento Cache

Use this function to create a new cache in your account.

const result = await cacheClient.createCache('test-cache');
if (result instanceof CreateCache.Success) {
console.log("Cache 'test-cache' created");
} else if (result instanceof CreateCache.AlreadyExists) {
console.log("Cache 'test-cache' already exists");
} else if (result instanceof CreateCache.Error) {
throw new Error(
`An error occurred while attempting to create cache 'test-cache': ${result.errorCode()}: ${result.toString()}`

List the names of existing caches in your account

A simple list of the names of caches for the account.

const result = await cacheClient.listCaches();
if (result instanceof ListCaches.Success) {
.map(c => c.getName())
} else if (result instanceof ListCaches.Error) {
throw new Error(`An error occurred while attempting to list caches: ${result.errorCode()}: ${result.toString()}`);

Write an item to a cache

A simple example of doing a set operation. In the client.set call, the TTL is optional. If you did pass it in, this would override the default TTL value set with the client connection object.

const result = await cacheClient.set('test-cache', 'test-key', 'test-value');
if (result instanceof CacheSet.Success) {
console.log("Key 'test-key' stored successfully");
} else if (result instanceof CacheSet.Error) {
throw new Error(
`An error occurred while attempting to store key 'test-key' in cache 'test-cache': ${result.errorCode()}: ${result.toString()}`

Read an item from a cache

This is an example of a simple read operation to get an item from a cache.

const result = await cacheClient.get('test-cache', 'test-key');
if (result instanceof CacheGet.Hit) {
console.log(`Retrieved value for key 'test-key': ${result.valueString()}`);
} else if (result instanceof CacheGet.Miss) {
console.log("Key 'test-key' was not found in cache 'test-cache'");
} else if (result instanceof CacheGet.Error) {
throw new Error(
`An error occurred while attempting to get key 'test-key' from cache 'test-cache': ${result.errorCode()}: ${result.toString()}`

Running the code

You can find complete, working examples in the JavaScript SDK GitHub repo examples directory.


Beyond these basic API calls check out the API reference page for more information on the full assortment of Momento API calls.

Follow this link to see this same type of code but for more advanced calls.