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
.
- JavaScript
- PHP
- Java
An
operation
can either beadd
|update
|delete
js
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 valuesawait client.waitForApiKey({ operation: 'add', key });// The fields to update on your API keyconst updatesToPerform: ApiKey = {acl: ['analytics', 'search'],indexes: ['products'],};// Call for updateawait client.updateApiKey({key,apiKey: updatesToPerform,});// Wait for update to be doneawait client.waitForApiKey({operation: 'update',key,// We provide the updated fields to check if the changes have been appliedapiKey: updatesToPerform,});// Call for deleteawait client.deleteApiKey({ key });// Wait for delete to be doneawait client.waitForApiKey({ operation: 'delete', key });
js
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 valuesawait client.waitForApiKey({ operation: 'add', key });// The fields to update on your API keyconst updatesToPerform: ApiKey = {acl: ['analytics', 'search'],indexes: ['products'],};// Call for updateawait client.updateApiKey({key,apiKey: updatesToPerform,});// Wait for update to be doneawait client.waitForApiKey({operation: 'update',key,// We provide the updated fields to check if the changes have been appliedapiKey: updatesToPerform,});// Call for deleteawait client.deleteApiKey({ key });// Wait for delete to be doneawait client.waitForApiKey({ operation: 'delete', key });
An
operation
can either beadd
|update
|delete
php
$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);
php
$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);
java
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 valuesclient.waitForApiKey(ApiKeyOperation.ADD, key);// The fields to update on your API keyApiKey updatesToPerform = new ApiKey().addAcl(Acl.ANALYTICS).addAcl(Acl.SEARCH).addIndexes("products");// Call for updateclient.updateApiKey(key, updatesToPerform);// Wait for update to be doneclient.waitForApiKey(ApiKeyOperation.UPDATE,key,// We provide the updated fields to check if the changes have been appliedupdatesToPerform);// Call for deleteclient.deleteApiKey(key);// Wait for delete to be doneclient.waitForApiKey(ApiKeyOperation.DELETE, key);
java
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 valuesclient.waitForApiKey(ApiKeyOperation.ADD, key);// The fields to update on your API keyApiKey updatesToPerform = new ApiKey().addAcl(Acl.ANALYTICS).addAcl(Acl.SEARCH).addIndexes("products");// Call for updateclient.updateApiKey(key, updatesToPerform);// Wait for update to be doneclient.waitForApiKey(ApiKeyOperation.UPDATE,key,// We provide the updated fields to check if the changes have been appliedupdatesToPerform);// Call for deleteclient.deleteApiKey(key);// Wait for delete to be doneclient.waitForApiKey(ApiKeyOperation.DELETE, key);