Navigation

Create & Manage API Keys

Overview

Create a User API Key

let app = App(id: "my-realm-app-id")
// ... log in ...
let user = app.currentUser()!
let client = user.apiKeyAuth()

client.createApiKey(named: "someKeyName") { (maybeApiKey, error) in
    guard error == nil else {
          fatalError("Failed to create key: \(error!)")
    }
    guard let apiKey = maybeApiKey else {
          fatalError("Failed to create key! Error unknown")
    }
    // Use apiKey
}
RLMApp *app = [RLMApp appWithId:@"myappid"];
// ... log in ...
RLMUser *user = [app currentUser];
RLMAPIKeyAuth *client = [user apiKeyAuth];

[client createApiKeyWithName:@"someKeyName" completion:^(RLMUserAPIKey *apiKey, NSError *error) {
   if (error != nil) {
      // ... handle Error ...
   } else {
      // ... use apiKey ...
   }
}];

Look up a User API Key

let app = App(id: "my-realm-app-id")
// ... log in ...
let user = app.currentUser()!
let client = user.apiKeyAuth()

// Fetch a specific API key by ObjectId
client.fetchApiKey(ObjectId("someObjectId")) { (maybeApiKey, error) in
      // ...
}

// Fetch all API keys
client.fetchApiKeys { (keys, error) in
      guard error == nil else {
         fatalError("Failed to fetch keys: \(error!)")
      }
      for key in keys! {
         // use key
      }
}
RLMApp *app = [RLMApp appWithId:@"myappid"];
// ... log in ...
RLMUser *user = [app currentUser];
RLMAPIKeyAuth *client = [user apiKeyAuth];

// Fetch API key by a specific ObjectId
NSError *error = nil;
RLMObjectId *objectId = [[RLMObjectId alloc] initWithString:@"someObjectId" error:&error];
[client fetchApiKey:objectId completion:^(RLMUserAPIKey *apiKey, NSError *error) {
   if (error != nil) {
      // ... handle error ...
   } else {
      // ... use apiKey ...
   }
}];

// Fetch all API keys
[client fetchApiKeysWithCompletion:^(NSArray<RLMUserAPIKey *> *keys, NSError *error) {
   if (error != nil) {
      // ... handle error ...
   } else {
      for (RLMUserAPIKey *key in keys) {
            // ... use key ...
      }
   }
}];

Enable or Disable an API Key

let app = App(id: "my-realm-app-id")
// ... log in ...
let user = app.currentUser()!
let client = user.apiKeyAuth()

// Enable the API key
client.enableApiKey(ObjectId("someObjectId")) { (error) in
    // ...
}

// Disable the API key
client.disableApiKey(apiKey.objectId) { (error) in
    // ...
}
RLMApp *app = [RLMApp appWithId:@"myappid"];
// ... log in ...
RLMUser *user = [app currentUser];
RLMAPIKeyAuth *client = [user apiKeyAuth];

RLMUserAPIKey *apiKey = getExampleApiKey(); // Get an API key

[client enableApiKey:[apiKey objectId] completion:^(NSError *error) {
   // Handle error if any. Otherwise, enable was successful.
}];

[client disableApiKey:[apiKey objectId] completion:^(NSError *error) {
   // Handle error if any. Otherwise, disable was successful.
}];

Delete an API Key

let app = App(id: "my-realm-app-id")
// ... log in ...
let user = app.currentUser()!
let client = user.apiKeyAuth()

client.deleteApiKey(apiKey.objectId) { (error) in
    guard error == nil else {
        fatalError("Failed to delete key: \(error!)")
    }
    // Key deleted
}
RLMApp *app = [RLMApp appWithId:@"myappid"];
// ... log in ...
RLMUser *user = [app currentUser];
RLMAPIKeyAuth *client = [user apiKeyAuth];

RLMUserAPIKey *apiKey = getExampleApiKey(); // Get an API key

[client deleteApiKey:[apiKey objectId] completion:^(NSError *error) {
   // Handle error if any. Otherwise, delete was successful.
}];