Navigation

Manage User API Keys - .NET SDK

On this page

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

Application users can generate user API keys with the .NET SDK. 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.

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.

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.

var newKey = await user.ApiKeys.CreateAsync("someKeyName");
Console.WriteLine($"I created a key named {newKey.Name}. " +
$"Is it enabled? {newKey.IsEnabled}");

To get a single key:

var key = await user.ApiKeys.FetchAsync(ObjectId.Parse("00112233445566778899aabb"));
Console.WriteLine($"I fetched the key named {key.Name}. " +
$"Is it enabled? {key.IsEnabled}");

To get all keys:

var allKeys = await user.ApiKeys.FetchAllAsync();
foreach (var key in allKeys)
{
Console.WriteLine($"I fetched the key named {key.Name}. " +
$"Is it enabled? {key.IsEnabled}");
}
var key = await user.ApiKeys.FetchAsync(ObjectId.Parse("00112233445566778899aabb"));
if (!key.IsEnabled)
{
// enable the key
await user.ApiKeys.EnableAsync(key.Id);
}
else
{
// disable the key
await user.ApiKeys.DisableAsync(key.Id);
}
await user.ApiKeys.DeleteAsync(ObjectId.Parse("00112233445566778899aabb"));
Give Feedback

On this page

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