SDK
SDK Javascript v7.x
2

createApiKey #

Available since 7.1.0
Available since Kuzzle 2.1.0

Creates a new API key for the currently logged user.


Copied to clipboard!
createApiKey(description, [options]);

Property Type Description
description
string
API key description
options
object
Additional options

options #

Additional query options

Property Type
(default)
Description
expiresIn
string/number

(-1)
Expiration duration
_id
string

(null)
API key unique ID
refresh
boolean

(false)
If set to wait_for, Kuzzle will not respond until the API key is indexed
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

Notes:

  • expiresIn:
    • if a raw number is provided (not enclosed between quotes), then the expiration delay is in milliseconds. Example: 86400000
    • if this value is a string, then its content is parsed by the ms library. Examples: "6d", "10h"
    • if -1 is provided, the token will never expire

Resolves #

An object containing the newly created API key:

Name Type Description
_id
string
ID of the newly created API key
_source
object
API key content

The API key content has the following properties:

Name Type Description
userId
string
User kuid
expiresAt
number
Expiration date in Epoch-millis format (-1 if the token never expires)
ttl
number
Original TTL
description
string
API key description
token
string
Authentication token associated with this API key

The authentication token token will never be returned by Kuzzle again. If you lose it, you'll have to delete the API key and recreate a new one.

Usage #

Copied to clipboard!
try {
  await kuzzle.auth.login('local', { username: 'john.doe', password: 'password' });
  const apiKey = await kuzzle.auth.createApiKey('Sigfox API key');
  console.log(apiKey);
  /*
  {
    _id: '-fQRa28BsidO6V_wmOcL',
    _source: {
      description: 'Sigfox API key',
      userId: 'john.doe',
      expiresAt: -1,
      ttl: -1,
      token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiJqb2huLmRvZSIsImlhdCI6MTU3ODA0OTMxMn0.XO_keW6EtsCGmPzxVJCChU9VREakEECDGg-N5lhCfF8'
    }
  }
  */
  console.log('API key successfully created');
  // Then use it with your client. Note: You don't need to call login after this because this bypasses the authentication system.
  kuzzle.setAPIKey(apiKey._source.token)
} catch (e) {
  console.error(e);
}