Developing applications with Momento SDKs
Welcome! This page provides information about common constructs you will need in order to assemble Momento clients in all of our SDKs. This page covers how to provide your Momento credentials (called API keys), how to configure your client, and some basic information about error handling and production readiness.
Constructing a Cache Client
The CacheClient
is the main object you will use in your code to interact with Momento services. To instantiate one, you need to pass a CredentialProvider
, a Configuration
, and a default time to live (TTL) value. The default TTL determines how long items using that CacheClient
will be stored in the cache before the cache deletes them. When performing Set
operations, you can override this TTL value unique to that operation. See Expire data with Time-to-Live (TTL) in Momento Cache for more information.
Here is an example of how to construct a CacheClient
:
- JavaScript
- Python
- Java
- Kotlin
- Go
- PHP
- Rust
- Elixir
- Swift
- Dart
return await CacheClient.create({
configuration: Configurations.Laptop.v1(),
credentialProvider: CredentialProvider.fromEnvironmentVariable('MOMENTO_API_KEY'),
defaultTtlSeconds: 60,
});
await CacheClientAsync.create(
Configurations.Laptop.latest(),
CredentialProvider.from_environment_variable("MOMENTO_API_KEY"),
timedelta(seconds=60),
)
try (CacheClient cacheClient =
CacheClient.create(
CredentialProvider.fromEnvVar("MOMENTO_API_KEY"),
Configurations.Laptop.v1(),
Duration.ofSeconds(60))) {
// ...
}
CacheClient(
CredentialProvider.fromEnvVar("MOMENTO_API_KEY"), Configurations.Laptop.latest, 60.seconds
).use { cacheClient ->
//...
}
context := context.Background()
credentialProvider, err = auth.NewEnvMomentoTokenProvider("MOMENTO_API_KEY")
if err != nil {
panic(err)
}
defaultTtl := 60 * time.Second
eagerConnectTimeout := 30 * time.Second
client, err = momento.NewCacheClientWithEagerConnectTimeout(
config.LaptopLatest(),
credentialProvider,
defaultTtl,
eagerConnectTimeout,
)
if err != nil {
panic(err)
}
client.Ping(context)
new CacheClient(
Laptop::latest(),
CredentialProvider::fromEnvironmentVariable("MOMENTO_API_KEY"),
60
);
let _cache_client = CacheClient::builder()
.default_ttl(Duration::from_secs(60))
.configuration(Laptop::latest())
.credential_provider(CredentialProvider::from_env_var(
"MOMENTO_API_KEY".to_string(),
)?)
.build()?;
config = Momento.Configurations.Laptop.latest()
credential_provider = Momento.Auth.CredentialProvider.from_env_var!("MOMENTO_AUTH_TOKEN")
default_ttl_seconds = 60.0
Momento.CacheClient.create!(config, credential_provider, default_ttl_seconds)
do {
let credentialProvider = try CredentialProvider.fromEnvironmentVariable(envVariableName: "MOMENTO_API_KEY")
let cacheClient = CacheClient(
configuration: CacheClientConfigurations.iOS.latest(),
credentialProvider: credentialProvider,
defaultTtlSeconds: 10
)
} catch {
print("Unable to create cache client: \(error)")
exit(1)
}
try {
final cacheClient = CacheClient(
CredentialProvider.fromEnvironmentVariable("MOMENTO_API_KEY"),
CacheClientConfigurations.latest(),
Duration(seconds: 30));
} catch (e) {
print("Unable to create cache client: $e");
exit(1);
}
Instantiating credential providers using Momento API keys
You need to provide a Momento API key when instantiating a Momento client. If you don't have one yet, you can get one from the Momento Web Console. Once you have a token, provide it to Momento SDKs when you create an instance of CredentialProvider
. There are convenient factory methods provided to construct a CredentialProvider
object, either from an environment variable or from a String. Below is an example of how to instantiate CredentialProvider
from an environment variable:
- JavaScript
- Python
- Java
- Kotlin
- Go
- PHP
- Rust
- Elixir
CredentialProvider.fromEnvironmentVariable('MOMENTO_API_KEY');
CredentialProvider.from_environment_variable("MOMENTO_API_KEY")
CredentialProvider.fromEnvVar("MOMENTO_API_KEY");
CredentialProvider.fromEnvVar("MOMENTO_API_KEY")
credentialProvider, err = auth.NewEnvMomentoTokenProvider("MOMENTO_API_KEY")
if err != nil {
panic(err)
}
CredentialProvider::fromEnvironmentVariable("MOMENTO_API_KEY");
let _credential_provider = CredentialProvider::from_env_var("MOMENTO_API_KEY".to_string());
allow(non_snake_case)]
b fn example_API_ConfigurationLaptop() {
let _config = momento::cache::configurations::Laptop::latest();
allow(non_snake_case)]
b fn example_API_ConfigurationInRegionDefaultLatest() {
let _config = momento::cache::configurations::InRegion::latest();
allow(non_snake_case)]
b fn example_API_ConfigurationInRegionLowLatency() {
let _config = momento::cache::configurations::LowLatency::latest();
allow(non_snake_case)]
b fn example_API_ConfigurationLambdaLatest() {
let _config = momento::cache::configurations::Lambda::latest();
allow(non_snake_case)]
b fn example_API_InstantiateCacheClient() -> Result<(), MomentoError> {
let _cache_client = CacheClient::builder()
.default_ttl(Duration::from_secs(60))
.configuration(Laptop::latest())
.credential_provider(CredentialProvider::from_env_var(
"MOMENTO_API_KEY".to_string(),
)?)
.build()?;
Momento.Auth.CredentialProvider.from_env_var!("MOMENTO_AUTH_TOKEN")
If you're storing your Momento auth token in a secret manager such as AWS Secret Manager, GCP Secret Manager, or a local config file, you must first retrieve the credentials from there and then instantiate a CredentialProvider
from a string, like this:
- JavaScript
- Java
- Kotlin
- Go
- Rust
- Elixir
const apiKey = retrieveApiKeyFromYourSecretsManager();
CredentialProvider.fromString({apiKey: apiKey});
final String authToken = retrieveAuthTokenFromYourSecretsManager();
CredentialProvider.fromString(authToken);
val authToken = retrieveAuthTokenFromYourSecretsManager()
CredentialProvider.fromString(authToken)
apiKey := RetrieveApiKeyFromYourSecretsManager()
credentialProvider, err = auth.NewStringMomentoTokenProvider(apiKey)
if err != nil {
fmt.Println("Error parsing API key:", err)
}
let _credential_provider = CredentialProvider::from_string("my-api-key".to_string());
allow(non_snake_case)]
b fn example_API_CredentialProviderFromEnvVar() {
let _credential_provider = CredentialProvider::from_env_var("MOMENTO_API_KEY".to_string());
allow(non_snake_case)]
b fn example_API_ConfigurationLaptop() {
let _config = momento::cache::configurations::Laptop::latest();
allow(non_snake_case)]
b fn example_API_ConfigurationInRegionDefaultLatest() {
let _config = momento::cache::configurations::InRegion::latest();
allow(non_snake_case)]
b fn example_API_ConfigurationInRegionLowLatency() {
let _config = momento::cache::configurations::LowLatency::latest();
allow(non_snake_case)]
b fn example_API_ConfigurationLambdaLatest() {
let _config = momento::cache::configurations::Lambda::latest();
allow(non_snake_case)]
b fn example_API_InstantiateCacheClient() -> Result<(), MomentoError> {
let _cache_client = CacheClient::builder()
.default_ttl(Duration::from_secs(60))
.configuration(Laptop::latest())
.credential_provider(CredentialProvider::from_env_var(
"MOMENTO_API_KEY".to_string(),
)?)
.build()?;
auth_token = retrieve_auth_token_from_your_secrets_manager()
Momento.Auth.CredentialProvider.from_string!(auth_token)
For an example of how to retrieve credentials from AWS Secrets Manager, see Retrieving a Momento auth token from AWS Secrets Manager.
For general information on Momento authentication, see our auth page.
For more information, see our Response Objects page, and the docs for the specific SDK that you are using (under Develop
->SDKs
in the left nav).