On this page
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.
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.
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
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)
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
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)