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.

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

# 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
  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);
}