Skip to main content

API reference for Momento Storage

Control APIs

These API methods are used to manage and control stores.

Create store

Creates a store with the provided name

Attributes:

NameTypeDescription
storeNameStringName of the store to be created.
const result = await storageClient.createStore(storeName);
switch (result.type) {
case CreateStoreResponse.AlreadyExists:
console.log(`Store '${storeName}' already exists`);
break;
case CreateStoreResponse.Success:
console.log(`Store '${storeName}' created`);
break;
case CreateStoreResponse.Error:
throw new Error(
`An error occurred while attempting to create store '${storeName}': ${result.errorCode()}: ${result.toString()}`
);
}

Delete store

Deletes a store

Attributes:

NameTypeDescription
storeNameStringName of the store to be deleted.
const result = await storageClient.deleteStore(storeName);
switch (result.type) {
case DeleteStoreResponse.Success:
console.log(`Store '${storeName}' deleted`);
break;
case DeleteStoreResponse.Error:
throw new Error(
`An error occurred while attempting to delete store '${storeName}': ${result.errorCode()}: ${result.toString()}`
);
}

List stores

Lists all stores

const result = await storageClient.listStores();
switch (result.type) {
case ListStoresResponse.Success:
console.log(
`Stores:\n${result
.stores()
.map(c => c.getName())
.join('\n')}\n\n`
);
break;
case ListStoresResponse.Error:
throw new Error(`An error occurred while attempting to list stores: ${result.errorCode()}: ${result.toString()}`);
}

Data APIs

These API methods are used to directly interact with data in a store.

Put

Puts a value into the store. If a value for this key is already present it will be replaced by the new value.

NameTypeDescription
storeNameStringName of the store.
keyStringThe key under which the value is to be added.
valueString / Bytes / Integer / DoubleThe value to be stored.
// to store a string value:
const result = await storageClient.putString(storeName, 'test-key', 'test-value');
switch (result.type) {
case StoragePutResponse.Success:
console.log("Key 'test-key' stored successfully");
break;
case StoragePutResponse.Error:
throw new Error(
`An error occurred while attempting to store key 'test-key' in store '${storeName}': ${result.errorCode()}: ${result.toString()}`
);
}

// Momento storage also supports these other data types:
await storageClient.putInt(storeName, 'test-key', 42);
await storageClient.putDouble(storeName, 'test-key', 3.14);
await storageClient.putBytes(storeName, 'test-key', Buffer.from('test-value'));

Get

Get the value stored for the given key.

NameTypeDescription
storeNameStringName of the store.
keyStringThe key whose value is to be retrieved.
const getResponse = await storageClient.get(storeName, 'test-key');
// simplified style; assume the value was found, and that it was a string
console.log(`string hit: ${getResponse.value()!.string()!}`);

// if the value was an integer:
const integerGetResponse = await storageClient.get(storeName, 'test-integer-key');
console.log(`integer hit: ${integerGetResponse.value()!.int()!}`);

// pattern-matching style; safer for production code
switch (getResponse.type) {
case StorageGetResponse.Found:
// if you know the value is a string:
console.log(`Retrieved value for key 'test-key': ${getResponse.value().string()!}`);
break;
case StorageGetResponse.NotFound:
console.log(`Key 'test-key' was not found in store '${storeName}'`);
break;
case StorageGetResponse.Error:
throw new Error(
`An error occurred while attempting to get key 'test-key' from store '${storeName}': ${getResponse.errorCode()}: ${getResponse.toString()}`
);
}

Delete

Delete the value stored for the given key.

NameTypeDescription
storeNameStringName of the store.
keyStringThe key whose value is to be deleted.
const result = await storageClient.delete(storeName, 'test-key');
switch (result.type) {
case StorageDeleteResponse.Success:
console.log("Key 'test-key' deleted successfully");
break;
case StorageDeleteResponse.Error:
throw new Error(
`An error occurred while attempting to delete key 'test-key' from store '${storeName}': ${result.errorCode()}: ${result.toString()}`
);
}

Current status of API support in SDKs

For the current status of API support in various SDK languages, see the language support page.