Validate Admin API Key in Algolia PHP client

I’m trying to validate the Application ID and Admin API key in my backend PHP (using the PHP client) using the below code:

    try {
        $userkey = $this->client->getUserKeyACL($api_key);
    } catch (\AlgoliaSearch\AlgoliaException $e) {
       //do something here
    }

The code always falls in the catch block when I use the correct keys and ID. Is there a way to validate the Admin Key and Application ID?

Hi there,

Here is how we do it in our WordPress plugin: https://github.com/algolia/algoliasearch-wordpress/blob/master/includes/class-algolia-api.php#L80

I hope that helps :wink:

@rayrutjes thanks for the reply. I actually took your code as reference. Still, it isn’t working. Also, the function is_valid_credentials1 which uses assert_valid_credentials function as mentioned by you seems isn’t used anywhere.

This is where the method is called: https://github.com/algolia/algoliasearch-wordpress/blob/b806e3fcea6d7982928682a57fc2523426c71e1a/includes/admin/class-algolia-admin-page-settings.php#L230

I’m pretty confident it works :wink:

Oops. Sorry.

Will check it out and post here after testing.

Thanks a ton!

1 Like

I’d previously removed the try-catch block which makes the GET request for /1/keys returning all keys other than the admin one.

What made me thinking was that if I got an error, the Algolia Dashboard logged the API /1/keys/{hashed_admin_api_key}. But didn’t logged the API at all in case of a successful request. Why is that? (Just out of curiosity)

Thanks for your help.