Docs Menu
Docs Home
/ /
Atlas Device SDKs
/
/

Manage User API Keys - Kotlin SDK

On this page

  • Create a User API Key
  • Look up a User API Key
  • Enable or Disable a User API Key
  • Delete a User API Key

You can use the Kotlin SDK to create and manage User API keys that allow devices or services to communicate with Realm on behalf of an existing user without requiring the user to re-authenticate.

User API keys are associated with a user object created by another non-anonymous authentication provider. Each user can associate up to 20 user keys with their account.

You can manage keys through the user.apiKeyAuth interface.

Tip

User API keys are not the same as server API keys, which allow a user or service to directly authenticate with Realm using the API Key authentication provider. To learn more about server API keys, refer to API Key Authentication.

To create a new user API key, pass a unique key name to ApiKeyAuth.create(). The created key will be associated with the logged-in user and can be used to interact with Realm on their behalf. You cannot create user API keys for anonymous users.

Warning

Store the API Key Value

The SDK only returns the value of the user API key when you create it. Make sure to store the key value securely so that you can use it to log in.

If you lose or do not store the key value there is no way to recover it. You will need to create a new user API key.

val user = app.currentUser!!
val provider = user.apiKeyAuth
// Create an API key for the logged-in user
val key = provider.create("apiKeyName")

To get a list of all user API keys associated with the logged-in user, call ApiKeyAuth.fetchAll().

To find a specific user API key for the logged-in user, pass the key's id to ApiKeyAuth.fetch().

val user = app.currentUser!!
val provider = user.apiKeyAuth
// Get all keys for the logged-in user
val apiKeys = provider.fetchAll()
// Get a specific key by its ID
val apiKey = provider.fetch(key.id)

You can enable or disable a key by calling ApiKeyAuth.enable() or ApiKeyAuth.disable() with the key's id. You cannot use disabled keys to log in on behalf of the user.

val user = app.currentUser!!
val provider = user.apiKeyAuth
// ... fetch the key to enable or disable
// Enable an API key that's currently disabled
provider.enable(key.id)
// Disable an API key that's currently enabled
provider.disable(key.id)

To permanently remove a specific user API key, pass the key's id to ApiKeyAuth.delete(). You cannot recover deleted keys or use them to log in on behalf of the user.

val user = app.currentUser!!
val provider = user.apiKeyAuth
// ... fetch the key to delete
// Delete the specified API key
provider.delete(key.id)
← 
 →