SDK
SDK Javascript v7.x
2

searchApiKeys #

Available since 7.1.0
Available since Kuzzle 2.1.0

Searches for a user API keys.


searchApiKeys(userId, [query], [options]);

PropertyTypeDescription
userId
string
User kuid
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 for the user.

options #

Additional query options

PropertyType
(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
timeout
number

(-1)
Time (in ms) during which a request will still be waited to be resolved. Set it -1 if you want to wait indefinitely
triggerEvents
boolean

(false)
If set to true, will trigger events even if using Embeded SDK. You should always ensure that your events/pipes does not create an infinite loop.
Available since Kuzzle 2.31.0

Resolves #

Resolves an object with the following properties:

NameTypeDescription
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:

NameTypeDescription
_id_
string
API key unique ID
_source
object
API key definition without the token field

Usage #

With the ElasticSearch Query DSL syntax.

try {
  const promises = [];
  // Create some API keys
  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);
  const results = await kuzzle.security.searchApiKeys('john.doe', {
    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);
}

With the Koncorde Filters DSL syntax.

try {
  const promises = [];
  // Create some API keys for user "jared.doe"
  promises.push(
    kuzzle.security.createApiKey('john.doe', 'Sigfox API key'));
  promises.push(
    kuzzle.security.createApiKey('john.doe', 'LoRa 6 month API key', {
      expiresIn: 36000
    }));
  promises.push(
    kuzzle.security.createApiKey('john.doe', 'LoRa permanent API key', {
      expiresIn: 42000, refresh: 'wait_for'
    }));
  await Promise.all(promises);
  const results = await kuzzle.security.searchApiKeys('john.doe', {
    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": "john.doe",
          "expiresAt": 31557600000,
          "ttl": 420000
        }
      },
      {
        "_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.`);
} catch (e) {
  console.error(e);
}