|
| 1 | +--- |
| 2 | +code: true |
| 3 | +type: page |
| 4 | +title: createApiKey |
| 5 | +description: Creates a new API key for the currently loggued user. |
| 6 | +--- |
| 7 | + |
| 8 | +# createApiKey |
| 9 | + |
| 10 | +<SinceBadge version="7.1.0" /> |
| 11 | + |
| 12 | +<SinceBadge version="Kuzzle 2.1.0" /> |
| 13 | + |
| 14 | +Creates a new API key for the currently loggued user. |
| 15 | + |
| 16 | +<br /> |
| 17 | + |
| 18 | +```js |
| 19 | +createApiKey(description, [options]); |
| 20 | +``` |
| 21 | + |
| 22 | +<br /> |
| 23 | + |
| 24 | +| Property | Type | Description | |
| 25 | +| --- | --- | --- | |
| 26 | +| `description` | <pre>string</pre> | API key description | |
| 27 | +| `options` | <pre>object</pre> | Additional options | |
| 28 | + |
| 29 | +### options |
| 30 | + |
| 31 | +Additional query options |
| 32 | + |
| 33 | +| Property | Type<br />(default) | Description | |
| 34 | +| --- | --- | --- | |
| 35 | +| `expiresIn` | <pre>string/number</pre><br />(`-1`) | Expiration duration | |
| 36 | +| `_id` | <pre>string</pre><br />(`null`) | API key unique ID | |
| 37 | +| `refresh` | <pre>boolean</pre><br />(`false`) | If set to `wait_for`, Kuzzle will not respond until the API key is indexed | |
| 38 | + |
| 39 | +**Notes**: |
| 40 | +- `expiresIn`: |
| 41 | + - if a raw number is provided (not enclosed between quotes), then the expiration delay is in milliseconds. Example: `86400000` |
| 42 | + - if this value is a string, then its content is parsed by the [ms](https://www.npmjs.com/package/ms) library. Examples: `"6d"`, `"10h"` |
| 43 | + - if `-1` is provided, the token will never expire |
| 44 | + |
| 45 | +## Resolves |
| 46 | + |
| 47 | +An object containing the newly created API key: |
| 48 | + |
| 49 | +| Name | Type | Description | |
| 50 | +| --------- | ----------------- | ---------------- | |
| 51 | +| `_id` | <pre>string</pre> | ID of the newly created API key | |
| 52 | +| `_source` | <pre>object</pre> | API key content | |
| 53 | + |
| 54 | +The API key content has the following properties: |
| 55 | + |
| 56 | +| Name | Type | Description | |
| 57 | +| --------- | ----------------- | ---------------- | |
| 58 | +| `userId` | <pre>string</pre> | User kuid | |
| 59 | +| `expiresAt` | <pre>number</pre> | Aexpiration date in UNIX micro-timestamp format (`-1` if the token never expires) | |
| 60 | +| `ttl` | <pre>number</pre> | Original TTL | |
| 61 | +| `description` | <pre>string</pre> | API key description | |
| 62 | +| `token` | <pre>string</pre> | Authentication token associated with this API key | |
| 63 | + |
| 64 | +::: warning |
| 65 | +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. |
| 66 | +::: |
| 67 | + |
| 68 | +## Usage |
| 69 | + |
| 70 | +<<< ./snippets/create-api-key.js |
0 commit comments