SDK
SDK Javascript v7.x
2

searchApiKeys #

Available since 7.1.0
Available since Kuzzle 2.1.0

Searches for a user API keys.


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

Property Type Description
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

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
  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.

Copied to clipboard!
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);
}