SDK
SDK Javascript v7.x
2

searchApiKeys #

Available since 7.1.0
Available since Kuzzle 2.1.0

Searches API keys for the currently logged 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.

Available since 7.4.8

This method also supports the Koncorde Filters DSL to match documents by passing the lang argument with the value koncorde.
Koncorde filters will be translated into an Elasticsearch query.

Koncorde bool operator and regexp clause are not supported for search queries.

If left empty, the result will return all available API keys of the currently logged 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
lang
string
Specify the query language to use. By default, it's elasticsearch but koncorde can also be used.
Available since 7.4.8

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 #

With the ElasticSearch Query DSL syntax.

Copied to clipboard!
try {
  const promises = [];
  // Create some API keys for user "jared.doe"
  promises.push(
    kuzzle.security.createApiKey('jared.doe', 'Sigfox API key'));
  promises.push(
    kuzzle.security.createApiKey('jared.doe', 'LoRa 6 month API key', {
      expiresIn: '6m'
    }));
  promises.push(
    kuzzle.security.createApiKey('jared.doe', 'LoRa permanent API key', {
      refresh: 'wait_for'
    }));
  await Promise.all(promises);
  // Log as "jared.doe"
  await kuzzle.auth.login('local', { username: 'jared.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": "jared.doe",
          "expiresAt": -1,
          "ttl": -1
        }
      },
      {
        "_id": "zXEwbG8BJASM_0-bWU-q",
        "_source": {
          "description": "LoRa 1 year API key",
          "userId": "jared.doe",
          "expiresAt": 31557600000,
          "ttl": 360000
        }
      }
    ]
  }
  */
  console.log(`Found ${results.total} API keys matching "LoRa"`);
} catch (e) {
  console.error(e);
}

With the Koncorde Filters DSL syntax.

Copied to clipboard!
try {
  const promises = [];
  // Create some API keys for user "jared.doe"
  promises.push(
    kuzzle.security.createApiKey('jared.doe', 'Sigfox API key'));
  promises.push(
    kuzzle.security.createApiKey('jared.doe', 'LoRa 6 month API key', {
      expiresIn: 36000
    }));
  promises.push(
    kuzzle.security.createApiKey('jared.doe', 'LoRa permanent API key', {
      expiresIn: 42000, refresh: 'wait_for'
    }));
  await Promise.all(promises);
  // Log as "jared.doe"
  await kuzzle.auth.login('local', { username: 'jared.doe', password: 'password' });
  const results = await kuzzle.auth.searchApiKeys({
    or: [
      {
        equals: {
          ttl: 42000
        }
      },
      {
        equals: {
          ttl: 36000
        },
      }
    ]
  }, { lang: 'koncorde' });
  console.log(results);
  /*
  {
    "total": 2,
    "hits": [
      {
        "_id": "znEwbG8BJASM_0-bWU-q",
        "_source": {
          "description": "LoRa permanent API key",
          "userId": "jared.doe",
          "expiresAt": 31557600000,
          "fingerprint": "4ee98cb8c614e99213e7695f822e42325d86c93cfaf39cb40e860939e784c8e6",
          "ttl": 420000
        }
      },
      {
        "_id": "zXEwbG8BJASM_0-bWU-q",
        "_source": {
          "description": "LoRa 1 year API key",
          "userId": "jared.doe",
          "expiresAt": 31557600000,
          "fingerprint": "f822e42325d86c93cfaf39cb40e860939e784c8e64ee98cb8c614e99213e7695",
          "ttl": 420000
        }
      }
    ]
  }
  */
  console.log(`Found ${results.total} API keys.`);
} catch (e) {
  console.error(e);
}