Navigation

Create & Manage API Keys

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.

let app = App(id: YOUR_REALM_APP_ID)
// ... log in ...
// User must not be an anonymous user.
let user = app.currentUser!
let client = user.apiKeysAuth
client.createAPIKey(named: "someKeyName") { (apiKey, error) in
guard error == nil else {
print("Failed to create key: \(error!)")
return
}
// Use apiKey
}
let app = App(id: YOUR_REALM_APP_ID)
// ... log in ...
let user = app.currentUser!
let client = user.apiKeysAuth
// Fetch a specific API key by ObjectId
client.fetchAPIKey(ObjectId("00112233445566778899aabb")) { (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
print(key.name)
}
}
let app = App(id: YOUR_REALM_APP_ID)
// ... log in ...
let user = app.currentUser!
let client = user.apiKeysAuth
// Enable the API key
client.enableAPIKey(ObjectId("00112233445566778899aabb")) { (error) in
// ...
}
let apiKey: UserAPIKey?
// ... Obtain a user API key ...
// Disable the API key
client.disableAPIKey(apiKey!.objectId) { (error) in
// ...
}
let app = App(id: YOUR_REALM_APP_ID)
// ... log in ...
let user = app.currentUser!
let client = user.apiKeysAuth
let apiKey: UserAPIKey?
// ... Obtain a user API key ...
client.deleteAPIKey(apiKey!.objectId) { (error) in
guard error == nil else {
print("Failed to delete key: \(error!)")
return
}
// Key deleted
}
Give Feedback