Navigation

Create & Manage User API Keys

User API keys are generated for specific application users by the client SDKs. You can allow devices or services to communicate with MongoDB Realm on behalf of a user by associating a unique user API key with each device or service.

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

Info With Circle IconCreated with Sketch.Note

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

To create a new user API key, call the create() or createAsync() methods of a logged in user's apiKeyAuth instance. The user API key will be associated with the logged in user and can be used to interact with MongoDB Realm on their behalf. You cannot create user API keys for anonymous users.

Important With Circle IconCreated with Sketch.Important
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()
user!!.apiKeys.createAsync("Name-of-the-API-Key") { result ->
if (result.isSuccess) {
Log.v("EXAMPLE", "Successfully created API key: ${result.get().value}")
} else {
Log.e("EXAMPLE", "Error creating API key: ${result.error}")
}
}

To get a list of all user API keys associated with the logged in user, call the synchronous fetchAll() method or asynchronous fetchAll() method of a logged in user's ApiKeyAuth instance.

Important With Circle IconCreated with Sketch.Important
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()
user!!.apiKeys
.fetchAll { result ->
if (result.isSuccess) {
Log.v("EXAMPLE", "Successfully fetched API keys: ${result.get().toTypedArray()}")
} else {
Log.e("EXAMPLE", "Error fetching API keys: ${result.error}")
}
}

To look up a specific user API key for the logged in user, pass the key's id to the fetch() or fetchAsync() methods of a logged in user's ApiKeyAuth instance.

Important With Circle IconCreated with Sketch.Important
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()
user!!.apiKeys.fetchAsync(api_key_id) { result ->
if (result.isSuccess) {
Log.v("EXAMPLE", "Successfully fetched API key: ${result.get()}")
} else {
Log.e("EXAMPLE", "Error fetching API key: ${result.error}")
}
}

To disable a specific user API key without deleting it, pass the key's id to the disable() or disableAsync() methods of a logged in user's ApiKeyAuth instance.

val user = app.currentUser()
user!!.apiKeys.disableAsync(api_key_id) { result ->
if (result.isSuccess) {
Log.v("EXAMPLE", "Successfully disabled API key.")
} else {
Log.e("EXAMPLE", "Error disabling API key: ${result.error}")
}
}

To enable a specific user API key that was previously disabled, pass the key's id to the enable() or enableAsync() methods of a logged in user's ApiKeyAuth instance.

val user = app.currentUser()
user!!.apiKeys.enableAsync(api_key_id) { result ->
if (result.isSuccess) {
Log.v("EXAMPLE", "Successfully enabled API key.")
} else {
Log.e("EXAMPLE", "Error fetching API key: ${result.error}")
}
}

To permanently remove a specific user API key, pass the key's id to the delete() or deleteAsync() methods of a logged in user's ApiKeyAuth instance.

val user = app.currentUser()
user!!.apiKeys.deleteAsync(api_key_id) { result ->
if (result.isSuccess) {
Log.v("EXAMPLE", "Successfully deleted API key.")
} else {
Log.e("EXAMPLE", "Error deleting API key: ${result.error}")
}
}
Give Feedback