The generated API clients are a work in progress, you can also find our stable clients on the Algolia documentation.

Skip to main content

Wait for an API key to be valid

The waitForApiKey method is only available in the search 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.

An operation can either be add | 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 });