# API keys

API key used in [HMAC authenticaton scheme](/authentication/authentication-schemes/hmac-authentication.md) and is represented by [`ApiKey`](/data-models.md#apikey) class.

To enable API key usage, the API key must be activated by setting the `isActive` field to true. Once the API key is authorized on the server, the request will be executed within the context of the user specified by their `userName`, operating on the device specified by the `deviceName`.

## API methods

## Get API keys

<mark style="color:blue;">`GET`</mark> `http://{server-address}/apikeys`

Returns all API keys that matches query parameters. Result is of type [`QueryResult<ApiKey>`](/data-models.md#queryresult).

#### Query Parameters

| Name     | Type      | Description                                                                                                              |
| -------- | --------- | ------------------------------------------------------------------------------------------------------------------------ |
| id       | string    | Supports NData syntax.                                                                                                   |
| isActive | bool      | Supports NData syntax.                                                                                                   |
| $skip    | int       | Pagination property. Describing number of records to skip from beginning of result collection.                           |
| $take    | int       | Pagination property. Maximum count of items to be returned.                                                              |
| $sort    | string\[] | <p>Comma separated list of property names. Please see NData syntax.<br></p>                                              |
| $count   | bool      | If set to `true`, response will not contain `items` collection. Use to get resource count, not the resources themselves. |

#### Headers

| Name                                            | Type   | Description                                    |
| ----------------------------------------------- | ------ | ---------------------------------------------- |
| Authorization<mark style="color:red;">\*</mark> | string | Authorization header with authorization token. |

{% tabs %}
{% tab title="200: OK Successful response" %}

```javascript
{
    "items": [
        {
            "id": "64b00bc5ba312b6744126a87",
            "clientId": "60f33924e92a4262763a08230c6c385f33c497f3",
            "clientSecret": "df80d7cae59b9a9c3ee76a3750f80ef9d0c9398083d0dc591952cd675926944c",
            "isActive": true,
            "createdAt": "2023-07-13T14:35:49.506Z",
            "name": "My API key",
            "description": "This is an API key description",
            "userName": "john.doe",
            "deviceName": "BackOffice",
            "permissions": [],
            "_v": 1
        }
    ],
    "count": 1,
    "totalCount": 1
}
```

{% endtab %}
{% endtabs %}

## Get API key

<mark style="color:blue;">`GET`</mark> `http://{server-address}/apikeys/{id}`

Result is of type [API key](/data-models.md#apikey).

#### Path Parameters

| Name                                 | Type   | Description        |
| ------------------------------------ | ------ | ------------------ |
| id<mark style="color:red;">\*</mark> | string | Unique identifier. |

#### Headers

| Name                                            | Type   | Description                                    |
| ----------------------------------------------- | ------ | ---------------------------------------------- |
| Authorization<mark style="color:red;">\*</mark> | string | Authorization header with authorization token. |

{% tabs %}
{% tab title="200: OK Resource found." %}

```javascript
{
    "id": "64b00bc5ba312b6744126a87",
    "clientId": "60f33924e92a4262763a08230c6c385f33c497f3",
    "clientSecret": "df80d7cae59b9a9c3ee76a3750f80ef9d0c9398083d0dc591952cd675926944c",
    "isActive": true,
    "createdAt": "2023-07-13T14:35:49.506Z",
    "name": "My API key",
    "description": "This is an API key description",
    "userName": "john.doe",
    "deviceName": "BackOffice",
    "permissions":
    [],
    "_v": 1
}
```

{% endtab %}

{% tab title="404: Not Found API key with given identifier is not found." %}

```javascript
{
    "title": "Zdroj nebol nájdený",
    "status": 404
}
```

{% endtab %}
{% endtabs %}

## Create API key

<mark style="color:green;">`POST`</mark> `http://{server-address}/apikeys`

Creates new API key. Result is of type [API key](/data-models.md#apikey).

**Required rights**

Authorized user must have `ApiKeyCreate` [right ](/user-rights.md)assigned.

#### Headers

| Name                                            | Type   | Description                                    |
| ----------------------------------------------- | ------ | ---------------------------------------------- |
| Authorization<mark style="color:red;">\*</mark> | string | Authorization header with authorization token. |

#### Request Body

| Name                                     | Type   | Description      |
| ---------------------------------------- | ------ | ---------------- |
| (body)<mark style="color:red;">\*</mark> | ApiKey | Model to create. |

{% tabs %}
{% tab title="201: Created Successfuly created." %}

```javascript
{
    "id": "64b00bc5ba312b6744126a87",
    "clientId": "60f33924e92a4262763a08230c6c385f33c497f3",
    "clientSecret": "df80d7cae59b9a9c3ee76a3750f80ef9d0c9398083d0dc591952cd675926944c",
    "isActive": true,
    "createdAt": "2023-07-13T14:35:49.506Z",
    "name": "My API key",
    "description": "This is an API key description",
    "userName": "john.doe",
    "deviceName": "BackOffice",
    "permissions":
    [],
    "_v": 1
}
```

{% endtab %}
{% endtabs %}

## Create or update API key

<mark style="color:orange;">`PUT`</mark> `http://{server-address}/apikeys/{id}`

Creates new or updates existing API key. Result is of type [API key](/data-models.md#apikey).

**Required rights**

Authorized user must have assigned `ApiKeyCreate` or `ApiKeyUpdate` [right](/user-rights.md).

#### Path Parameters

| Name                                 | Type   | Description        |
| ------------------------------------ | ------ | ------------------ |
| id<mark style="color:red;">\*</mark> | string | Unique identifier. |

#### Headers

| Name                                            | Type   | Description                                    |
| ----------------------------------------------- | ------ | ---------------------------------------------- |
| Authorization<mark style="color:red;">\*</mark> | string | Authorization header with authorization token. |

#### Request Body

| Name                                     | Type   | Description                |
| ---------------------------------------- | ------ | -------------------------- |
| (body)<mark style="color:red;">\*</mark> | ApiKey | Model to create or update. |

{% tabs %}
{% tab title="200: OK Successfuly created or updated." %}

```javascript
{
    "id": "64b00bc5ba312b6744126a87",
    "clientId": "60f33924e92a4262763a08230c6c385f33c497f3",
    "clientSecret": "df80d7cae59b9a9c3ee76a3750f80ef9d0c9398083d0dc591952cd675926944c",
    "isActive": true,
    "createdAt": "2023-07-13T14:35:49.506Z",
    "name": "My API key",
    "description": "This is an API key description",
    "userName": "john.doe",
    "deviceName": "BackOffice",
    "permissions":
    [],
    "_v": 2
}
```

{% endtab %}
{% endtabs %}

## Delete API key

<mark style="color:red;">`DELETE`</mark> `http://{server-address}/apikeys/{id}`

**Required rights**

Authorized user must have `ApiKeyDelete` [right ](/user-rights.md)assigned.

#### Path Parameters

| Name                                 | Type   | Description        |
| ------------------------------------ | ------ | ------------------ |
| id<mark style="color:red;">\*</mark> | string | Unique identifier. |

#### Headers

| Name                                            | Type   | Description                                    |
| ----------------------------------------------- | ------ | ---------------------------------------------- |
| Authorization<mark style="color:red;">\*</mark> | string | Authorization header with authorization token. |

{% tabs %}
{% tab title="200: OK Successfuly deleted." %}

```javascript
{
    "id": "64b00bc5ba312b6744126a87",
    "clientId": "60f33924e92a4262763a08230c6c385f33c497f3",
    "clientSecret": "df80d7cae59b9a9c3ee76a3750f80ef9d0c9398083d0dc591952cd675926944c",
    "isActive": true,
    "createdAt": "2023-07-13T14:35:49.506Z",
    "name": "My API key",
    "description": "This is an API key description",
    "userName": "john.doe",
    "deviceName": "BackOffice",
    "permissions":
    [],
    "_v": 1
}
```

{% endtab %}

{% tab title="404: Not Found Given resource was not found." %}

```javascript
{
    "title": "Zdroj nebol nájdený",
    "status": 404
}
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developers.portos.sk/api-reference/api-keys.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
