Wait for an API key to be valid
The
waitForApiKey
method is only available in thesearch
client context.
Adding, updating or deleting API keys is not always instantaneous, which is why you might want to ensure the job has been processed before jumping to an other task.
We provide a waitForApiKey
helper method for you to easily wait for a specific operation
made on a key
.
- C#
- Dart
- Go
- Java
- JavaScript
- Kotlin
- PHP
- Python
- Scala
An
operation
can either beadd
|update
|delete
import { algoliasearch } from 'algoliasearch';
const client = algoliasearch('<YOUR_APP_ID>', '<YOUR_API_KEY>');
const { key } = await client.addApiKey({
acl: ['analytics', 'browse', 'editSettings'],
});
// Poll the task status with defaults values
await client.waitForApiKey({ operation: 'add', key });
// The fields to update on your API key
const updatesToPerform: ApiKey = {
acl: ['analytics', 'search'],
indexes: ['products'],
};
// Call for update
await client.updateApiKey({
key,
apiKey: updatesToPerform,
});
// Wait for update to be done
await client.waitForApiKey({
operation: 'update',
key,
// We provide the updated fields to check if the changes have been applied
apiKey: updatesToPerform,
});
// Call for delete
await client.deleteApiKey({ key });
// Wait for delete to be done
await client.waitForApiKey({ operation: 'delete', key });
An
operation
can either beadd
|update
|delete
$client = SearchClient::create(
'<YOUR_APP_ID>',
'<YOUR_API_KEY>'
);
$response = $client->addApiKey([
'acl' => ['analytics', 'browse', 'editSettings'],
]);
$key = $response['key'];
// Poll the task status with defaults values
$client->waitForApiKey('add', $key);
// The fields to update on your API key
$updatesToPerform = [
'acl' => ['analytics', 'search'],
'indexes' => ['products'],
];
// Call for update
$client->updateApiKey($key, $updatesToPerform);
// Wait for update to be done
$client->waitForApiKey('update', $key, $updatesToPerform);
// Call for delete
$client->deleteApiKey($key);
// Wait for delete to be done
$client->waitForApiKey('delete', $key);
import com.algolia.api.SearchClient;
import com.algolia.model.search.*;
import com.algolia.utils.*;
SearchClient client = new SearchClient("<YOUR_APP_ID>", "<YOUR_API_KEY>");
AddApiKeyResponse keyResponse = client.addApiKey(new ApiKey().addAcl(Acl.ANALYTICS).addAcl(Acl.BROWSE).addAcl(Acl.EDIT_SETTINGS));
String key = keyResponse.getKey();
// Poll the task status with defaults values
client.waitForApiKey(ApiKeyOperation.ADD, key);
// The fields to update on your API key
ApiKey updatesToPerform = new ApiKey()
.addAcl(Acl.ANALYTICS)
.addAcl(Acl.SEARCH)
.addIndexes("products");
// Call for update
client.updateApiKey(key, updatesToPerform);
// Wait for update to be done
client.waitForApiKey(
ApiKeyOperation.UPDATE,
key,
// We provide the updated fields to check if the changes have been applied
updatesToPerform
);
// Call for delete
client.deleteApiKey(key);
// Wait for delete to be done
client.waitForApiKey(ApiKeyOperation.DELETE, key);
import com.algolia.client.api.SearchClient
import com.algolia.client.extensions.*
import com.algolia.client.model.search.*
val client = SearchClient("<YOUR_APP_ID>", "<YOUR_API_KEY>")
val keyResponse = client.addApiKey(apiKey = ApiKey(acl = listOf(Acl.Analytics, Acl.Browse, Acl.EditSettings)))
// Poll the task status with defaults values
client.waitKeyCreation(keyResponse.key)
// The fields to update on your API key
val updatesToPerform = ApiKey(acl = listOf(Acl.Analytics, Acl.Search), indexes = listOf("products"))
// Call for update
client.updateApiKey(key = keyResponse.key, apiKey = updatesToPerform)
// Wait for update to be done
client.waitKeyUpdate(
key = keyResponse.key,
apiKey = updatesToPerform, // We provide the updated fields to check if the changes have been applied
);
// Call for delete
client.deleteApiKey(key = keyResponse.key)
// Wait for delete to be done
client.waitKeyDelete(key = keyResponse.key)
var client = SearchClient(appId: '<YOUR_APP_ID>', apiKey: '<YOUR_API_KEY>');
var keyResponse = await client.addApiKey(apiKey : ApiKey(acl : [Acl.analytics, Acl.browse, Acl.editSettings]));
// Poll the task status with defaults values
await client.waitKeyCreation(keyResponse.key);
// The fields to update on your API key
var updatesToPerform = ApiKey(acl: [Acl.analytics, Acl.search], indexes: ['products']);
// Call for update
await client.updateApiKey(key: keyResponse.key, apiKey: updatesToPerform);
// Wait for update to be done
await client.waitKeyUpdate(
key: keyResponse.key,
apiKey: updatesToPerform, // We provide the updated fields to check if the changes have been applied
);
// Call for delete
await client.deleteApiKey(key: keyResponse.key);
// Wait for delete to be done
await client.waitKeyDelete(key: keyResponse.key);
import (
"github.com/algolia/algoliasearch-client-go/v4/algolia/search"
)
indexName := "<INDEX_NAME>"
appID := "<APPLICATION_ID>"
apiKey := "<API_KEY>"
searchClient, _ := search.NewClient(appID, apiKey)
apiKeyStruct := search.NewApiKey([]search.Acl{"search"})
addApiKeyResponse, err := searchClient.AddApiKey(searchClient.NewApiAddApiKeyRequest(apiKeyStruct))
if err != nil {
panic(err)
}
_, err = searchClient.WaitForApiKey(
addApiKeyResponse.Key,
apiKeyStruct,
"add",
nil,
nil,
nil,
)
if err != nil {
panic(err)
}
apiKeyStruct.SetAcl([]search.Acl{"search", "addObject"})
updateApiKeyResponse, err := searchClient.UpdateApiKey(
searchClient.NewApiUpdateApiKeyRequest(
addApiKeyResponse.Key,
apiKeyStruct,
),
)
if err != nil {
panic(err)
}
_, err = searchClient.WaitForApiKey(
updateApiKeyResponse.Key,
apiKeyStruct,
"update",
nil,
nil,
nil,
)
if err != nil {
panic(err)
}
deleteApiKeyResponse, err := searchClient.DeleteApiKey(searchClient.NewApiDeleteApiKeyRequest(addApiKeyResponse.Key))
if err != nil {
panic(err)
}
_, err = searchClient.WaitForApiKey(
deleteApiKeyResponse.Key,
apiKeyStruct,
"delete",
nil,
nil,
nil,
)
if err != nil {
panic(err)
}