How to check if searchClient is successful created? [javascript]

Hey there,
I’m building an Algolia plugin for the Strapi framework and I was wondering how to check if the client has the right credentials/APIkey? Even with no indices initiated or any objects added.

const algoliasearch = require("algoliasearch");

const client = algoliasearch("YourApplicationID", "YourAdminAPIKey");

Anyone any suggestion?

Super cool project! You can always call getApiKey with the key itself to get information about the current key.

const appId = '';
const apiKey = '';
algoliasearch(appId, apiKey).getApiKey(apiKey).then(console.log);

To check if the key has the right access you should check acl but also if it has index restrictions.

1 Like

Thanks @Jerska I’ll try that when I have some time.

@Jerska, I tried it out, but it only works for search-only API keys, not for admin keys.
Any idea how to check on startup if an admin key is valid?

Hello @mattias ,

algoliasearch(appId, searchApiKey).getApiKey(searchApiKey).then(console.log)

->

{
  value: 'xxxxxxxxx',
  createdAt: 1568727096,
  acl: [ 'search' ],
  validity: 0,
  description: 'Search-only API Key'
}
algoliasearch(appId, adminApiKey).getApiKey(adminApiKey).then(console.log)

->

{
  value: 'yyyyyyyy',
  acl: [
    'search',
    'addObject',
    'deleteObject',
    'deleteIndex',
    'settings',
    'editSettings',
    'browse',
    'analytics',
    'listIndexes',
    'seeUnretrievableAttributes',
    'logs',
    'recommendation',
    'usage',
    'nluWriteProject',
    'nluReadProject',
    'nluWriteEntity',
    'nluReadEntity',
    'nluWriteIntent',
    'nluReadIntent',
    'nluPrediction',
    'nluReadQA'
  ],
  validity: 0
}

Will this work for you?
Let us know if you need more help on this.
I’m excited to hear more about your plugin for Strapi!

Hi @eunjae.lee, thanks for your response. It works, just like you (and @Jerska earlier) suggested :sweat_smile:.

algoliasearch(appId, adminApiKey).getApiKey(adminApiKey).then(console.log)

For now I developed a hook: strapi-hook-algolia, this adds a service to a Strapi application which then can be used in lifecycle hooks of content-types. In the near future I plan on creating a plugin, this will add more possibilities in a more generic less configurable way. I’ll give an update on the plugin.

1 Like

Awesome :slight_smile: Thanks for the update!