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.fromEnvVarV2(),
defaultTtlSeconds: 60,
});
await CacheClientAsync.create(
Configurations.Laptop.latest(),
CredentialProvider.from_environment_variables_v2(),
timedelta(seconds=60),
)
try (CacheClient cacheClient =
CacheClient.create(
CredentialProvider.fromEnvVarV2(),
Configurations.Laptop.v1(),
Duration.ofSeconds(60))) {
// ...
}
CacheClient(
CredentialProvider.fromEnvVarV2(), Configurations.Laptop.latest, 60.seconds
).use { cacheClient ->
//...
}
credentialProvider, err = auth.NewEnvMomentoV2TokenProvider()
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(ctx)
new CacheClient(
Laptop::latest(),
CredentialProvider::fromEnvironmentVariablesV2(),
60
);
let cache_client = CacheClient::builder()
.default_ttl(Duration::from_secs(60))
.configuration(momento::cache::configurations::Laptop::latest())
.credential_provider(CredentialProvider::from_default_env_var_v2()?)
.build()?;
config = Momento.Configurations.Laptop.latest()
credential_provider = Momento.Auth.CredentialProvider.from_env_var_v2!()
default_ttl_seconds = 60.0
Momento.CacheClient.create!(config, credential_provider, default_ttl_seconds)
do {
let credentialProvider = try CredentialProvider.fromEnvironmentVariablesV2()
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.fromEnvironmentVariablesV2(),
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.
You also need to provide a Momento service endpoint. You can find a list of them here.
Once you have an API key and an endpoint, 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 the default environment variables MOMENTO_API_KEY and MOMENTO_ENDPOINT:
- JavaScript
- Python
- Java
- Kotlin
- Go
- C#
- PHP
- Rust
- Elixir
- Swift
CredentialProvider.fromEnvVarV2();
CredentialProvider.from_environment_variables_v2()
CredentialProvider.fromEnvVarV2();
CredentialProvider.fromEnvVarV2()
credentialProvider, err = auth.FromEnvironmentVariablesV2()
if err != nil {
panic(err)
}
new EnvMomentoV2TokenProvider();
CredentialProvider::fromEnvironmentVariablesV2();
let credential_provider = CredentialProvider::from_default_env_var_v2()?;
Momento.Auth.CredentialProvider.from_env_var_v2!()
do {
let credentialProvider = try CredentialProvider.fromEnvironmentVariablesV2()
} catch {
print("Unable to create credential provider: \(error)")
exit(1)
}
Alternately provide custom environment variable names:
- JavaScript
- Python
- Java
- Kotlin
- Go
- C#
- PHP
- Rust
- Elixir
- Swift
CredentialProvider.fromEnvVarV2({
apiKeyEnvVar: 'MOMENTO_API_KEY',
endpointEnvVar: 'MOMENTO_ENDPOINT',
});
CredentialProvider.from_environment_variables_v2(
api_key_env_var="MOMENTO_API_KEY", endpoint_env_var="MOMENTO_ENDPOINT"
)
CredentialProvider.fromEnvVarV2("MOMENTO_API_KEY", "MOMENTO_ENDPOINT");
CredentialProvider.fromEnvVarV2("MOMENTO_API_KEY", "MOMENTO_ENDPOINT")
credentialProvider, err = auth.FromEnvironmentVariablesV2(auth.EnvVarV2Props{
ApiKeyEnvVar: "MOMENTO_API_KEY",
EndpointEnvVar: "MOMENTO_ENDPOINT",
})
if err != nil {
panic(err)
}
new EnvMomentoV2TokenProvider("MOMENTO_API_KEY", "MOMENTO_ENDPOINT");
CredentialProvider::fromEnvironmentVariablesV2("MOMENTO_API_KEY", "MOMENTO_ENDPOINT");
let credential_provider = CredentialProvider::from_env_var_v2("MOMENTO_API_KEY", "MOMENTO_ENDPOINT")?;
Momento.Auth.CredentialProvider.from_env_var_v2!("MOMENTO_API_KEY", "MOMENTO_ENDPOINT")
do {
let credentialProvider = try CredentialProvider.fromEnvironmentVariablesV2(
apiKeyEnvVar: "MOMENTO_API_KEY",
endpointEnvVar: "MOMENTO_ENDPOINT"
)
} catch {
print("Unable to create credential provider: \(error)")
exit(1)
}
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
- Python
- Java
- Kotlin
- Go
- C#
- PHP
- Rust
- Elixir
- Swift
const apiKey = retrieveApiKeyV2FromYourSecretsManager();
// using the us-west-2 region's endpoint for this example
const endpoint = 'cell-4-us-west-2-1.prod.a.momentohq.com';
CredentialProvider.fromApiKeyV2({apiKey: apiKey, endpoint: endpoint});
api_key = retrieve_api_key_v2_from_your_secrets_manager()
endpoint = "cell-4-us-west-2-1.prod.a.momentohq.com"
CredentialProvider.from_api_key_v2(api_key, endpoint)
final String apiKey = retrieveApiKeyV2FromYourSecretsManager();
final String endpoint = "cell-4-us-west-2-1.prod.a.momentohq.com";
CredentialProvider.fromApiKeyV2(apiKey, endpoint);
val apiKey = retrieveApiKeyV2FromYourSecretsManager()
val endpoint = "cell-4-us-west-2-1.prod.a.momentohq.com"
CredentialProvider.fromApiKeyV2(apiKey, endpoint)
apiKey := retrieveApiKeyV2FromYourSecretsManager()
endpoint := "cell-4-us-west-2-1.prod.a.momentohq.com"
props := auth.ApiKeyV2Props{ApiKey: apiKey, Endpoint: endpoint}
credentialProvider, err = auth.FromApiKeyV2(props)
if err != nil {
panic(err)
}
var endpoint = "cell-4-us-west-2-1.prod.a.momentohq.com";
var apiKey = RetrieveApiKeyV2FromYourSecretsManager();
new ApiKeyV2TokenProvider(apiKey, endpoint);
$api_key = retrieveApiKeyV2FromYourSecretsManager();
$endpoint = "cell-4-us-west-2-1.prod.a.momentohq.com";
CredentialProvider::fromApiKeyV2($api_key, $endpoint);
let api_key = retrieve_api_key_v2_from_your_secrets_manager();
let endpoint = "cell-4-us-west-2-1.prod.a.momentohq.com".to_string();
let credential_provider = CredentialProvider::from_api_key_v2(api_key, endpoint)?;
api_key = retrieve_api_key_v2_from_your_secrets_manager()
endpoint = "cell-4-us-west-2-1.prod.a.momentohq.com"
Momento.Auth.CredentialProvider.from_api_key_v2!(api_key, endpoint)
let apiKey = retrieveApiKeyV2FromYourSecretsManager()
let endpoint = "cell-4-us-west-2-1.prod.a.momentohq.com"
do {
let credentialProvider = try CredentialProvider.fromApiKeyV2(
apiKey: apiKey, endpoint: endpoint)
} catch {
print("Unable to create credential provider: \(error)")
exit(1)
}
For an example of how to retrieve credentials from AWS Secrets Manager, see Retrieving a Momento auth token from AWS Secrets Manager.
If you have a disposable auth token or a v1 API key, you can instantiate a CredentialProvider like this:
- JavaScript
- Python
- Kotlin
- Go
- C#
- PHP
- Rust
- Elixir
- Swift
const apiKey = retrieveApiKeyFromYourSecretsManager();
CredentialProvider.fromDisposableToken({apiKey: apiKey});
auth_token = retrieve_api_key_from_your_secrets_manager()
CredentialProvider.from_disposable_token(auth_token)
val authToken = retrieveAuthTokenFromYourSecretsManager()
CredentialProvider.fromDisposableToken(authToken)
authToken := RetrieveApiKeyFromYourSecretsManager()
credentialProvider, err = auth.FromDisposableToken(authToken)
if err != nil {
panic(err)
}
var authToken = RetrieveApiKeyFromYourSecretsManager();
new DisposableTokenProvider(authToken);
$auth_token = retrieveApiKeyFromYourSecretsManager();
CredentialProvider::fromDisposableToken($auth_token);
let auth_token = generate_disposable_token();
let credential_provider = CredentialProvider::from_disposable_token(auth_token)?;
api_key = retrieve_api_key_from_your_secrets_manager()
Momento.Auth.CredentialProvider.from_disposable_token!(api_key)
let authToken = retrieveApiKeyFromYourSecretsManager()
do {
let credentialProvider = try CredentialProvider.fromDisposableToken(token: authToken)
} catch {
print("Unable to create credential provider: \(error)")
exit(1)
}
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).