searchApiKeys

Available since 7.1.0 Available since Kuzzle 2.1.0

Searches API keys for the currently loggued user.


Copied to clipboard!
searchApiKeys([query], [options]);

Property Type Description
query
object
Search query
options
object
Additional options

query

The search query to apply to API keys content, using ElasticSearch Query DSL syntax.

If left empty, the result will return all available API keys of the currently loggued user.

options

Additional query options

Property Type
(default)
Description
from
number

(0)
Offset of the first document to fetch
size
number

(10)
Maximum number of documents to retrieve per page

Resolves

Resolves an object with the following properties:

Name Type Description
hits
object[]
Array of objects representing found API keys
total
number
Total number of API keys found. Depending on pagination options, this can be greater than the actual number of API keys in a single result page

Each object of the hits array has the following properties:

Name Type Description
_id_
string
API key unique ID
_source
object
API key definition without the token field

Usage

Copied to clipboard!
try {
  const promises = [];

  // Create some API keys for user "john.doe"
  promises.push(
    kuzzle.security.createApiKey('john.doe', 'Sigfox API key'));
  promises.push(
    kuzzle.security.createApiKey('john.doe', 'LoRa 6 month API key', {
      expiresIn: '6m'
    }));
  promises.push(
    kuzzle.security.createApiKey('john.doe', 'LoRa permanent API key', {
      refresh: 'wait_for'
    }));

  await Promise.all(promises);

  // Log as "john.doe"
  await kuzzle.auth.login('local', { username: 'john.doe', password: 'password' });

  const results = await kuzzle.auth.searchApiKeys({
    match: {
      description: 'LoRa'
    }
  });

  console.log(results);
  /*
  {
    "total": 2,
    "hits": [
      {
        "_id": "znEwbG8BJASM_0-bWU-q",
        "_source": {
          "description": "LoRa permanent API key",
          "userId": "john.doe",
          "expiresAt": -1,
          "ttl": -1
        }
      },
      {
        "_id": "zXEwbG8BJASM_0-bWU-q",
        "_source": {
          "description": "LoRa 1 year API key",
          "userId": "john.doe",
          "expiresAt": 31557600000,
          "ttl": 360000
        }
      }
    ]
  }
  */

  console.log(`Found ${results.total} API keys matching "LoRa"`);
} catch (e) {
  console.error(e);
}