Wait for API key

Hello,

I’ve an issue in my login process: Each time than a user login on my platform, I create for him a specific Algolia API Key; but, if I use directly after the creation the API key, Algolia give me an error saying than the key is invalid; I generally need to wait ~3 seconds to use it.

My problem is than I can’t use the waitFor API function on the creation of a key, because the creation of a key don’t return any taskID.

It’s a big problem for me, because my login process is “blocked” by waiting ~3sec. With a taskID, the process will be much smoother. Ideal case will be a waitFor directly in my front.

Thanks in advance for your help :slight_smile:

Hi Alefort,

Indeed key management doesn’t return a taskID, for “historical reasons”. We hope that one day this is unified but it’s not a priority at the moment.

The best way to check of the key exists is to call getApiKey($generatedKey) in a loop and return when it exists. Something like the following (note that I haven’t tested the code, there might be typos):

    public function newApiKey(AlgoliaSearch\Client $client, array $keyParams): array
    {
        $keyResponse = $client->addApiKey($keyParams);
        $count = 1;
        $res = null;
        
            do {
                try {
                    $res = $client->getApiKey($keyResponse['key']);
                    return $keyResponse;
                } catch (\Exception $e) {
                    usleep(100);
                } finally {
                    $count++;
                }
            } while ($count <= 5);
            
        return $keyResponse;
    }

If you want to go even further, you may need to check each host, this should not be an issue the vast majority of the case, but just in case.

    public function createKey($client)
    {
        $keyResponse = $client->addApiKey($keyParams);
        
        $appId = getenv('ALGOLIA_APP_ID');
        $adminKey = getenv('ALGOLIA_API_KEY');
        
        $hosts = array(
            $appId.'-1.algolianet.com',
            $appId.'-2.algolianet.com',
            $appId.'-3.algolianet.com',
        );

        foreach ($hosts as $host) {
            $c = new AlgoliaSearch\Client($appId, $adminKey, [$host]);

            $this->waitForKey($keyResponse['key'], $c);
        }
    }

    public function waitForKey($key, $client)
    {
        $count = 1;
        
            do {
                try {
                    $res = $client->getApiKey($key);
                    return;
                } catch (\Exception $e) {
                    usleep(100);
                } finally {
                    $count++;
                }
            } while ($count <= 5);
            
        return;
    }

Please let me know if that worked for you. In the meantime, I’ll see if we can directly add this feature in all Api Clients. :tada:

Hello, thank you for your answer!

Work for me but cause some troubles: My API is configured to never wait more than 6 seconds in any calls, and on some cases I see than the creation of Algolia keys took more than 4 seconds (On our login process, with others keys generations of others vendors it’s a problem).

I could do a function waitForKey on my front, trying to use a key every 3 seconds with a max tries but I can’t know, in case Algolia told me than the key don’t exist, if it’s because the key really don’t exist or just exist yet.

If something like that could be integrated in the AlgoliaSearch.js it’s could be great: AlgoliaSearch try to use one key, if the key don’t exist we precise a maxTries and a delay between checks and AlgoliaSearch could keep trying. For me, this way is much better than using an admin key to check if the key exist.

Thank you again :wink: