---
type: api
title: Create an enrollment API key
description: Create an enrollment API key for a given agent policy.<br/><br/>[Required authorization] Route required privileges: fleet-agents-all.
url: https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/kibana/v0/operation/operation-post-fleet-enrollment-api-keys
resource: https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/kibana/v0/operation/operation-post-fleet-enrollment-api-keys
products:
  - Kibana
---

# Create an enrollment API key

- Server: `https://{kibana_url}`

## Paths

- `POST` `/api/fleet/enrollment_api_keys`

- [Api key auth](/elastic/docs-builder/docs/4371/api/doc/kibana/v0/authentication#apikeyauth)
- [Basic auth](/elastic/docs-builder/docs/4371/api/doc/kibana/v0/authentication#basicauth)

## Description

Create an enrollment API key for a given agent policy.<br/><br/>[Required authorization] Route required privileges: fleet-agents-all.

## Request Body

`application/json`

- `expiration` (string · max: 20) — optional
  The expiration time for the enrollment token, expressed as a duration (for example, 30d, 24h, 90m). By default, enrollment tokens never expire.
- `name` (string) — optional
- `policy_id` (string) — required

## Responses

#### `200`

Successful response

`application/json`

- `action` (enum)
  Values: `created`
- `item` ({})
  - `active` (boolean)
    When false, the enrollment API key is revoked and cannot be used for enrolling Elastic Agents.
  - `api_key` (string)
    The enrollment API key (token) used for enrolling Elastic Agents.
  - `api_key_id` (string)
    The ID of the API key in the Security API.
  - `created_at` (string)
  - `expire_at` (string)
    The expiration date of the enrollment token as an ISO 8601 date string. Absent when the token never expires.
  - `hidden` (boolean)
  - `id` (string)
  - `name` (string)
    The name of the enrollment API key.
  - `policy_id` (string)
    The ID of the agent policy the Elastic Agent will be enrolled in.
#### `400`

Bad Request

`application/json`

- `attributes` (object)
- `error` (string)
- `errorType` (string)
- `message` (string)
- `statusCode` (number)
## Code Examples

#### Console

```console
POST /api/fleet/enrollment_api_keys
{
  "expiration": "2025-01-01T00:00:00.000Z",
  "name": "My enrollment key",
  "policy_id": "policy-id-1"
}
```

#### curl

```curl
curl -X POST "https://{kibana_url}/api/fleet/enrollment_api_keys" \
  -H "kbn-xsrf: true" \
  -H "Content-Type: application/json" \
  -d '{
  "expiration": "2025-01-01T00:00:00.000Z",
  "name": "My enrollment key",
  "policy_id": "policy-id-1"
}'
```

## Response Examples

#### Post enrollment api key

The created enrollment API key

```json
{
  "action": "created",
  "item": {
    "active": true,
    "api_key": "api-key-value-1",
    "api_key_id": "api-key-id-1",
    "created_at": "2024-01-01T00:00:00.000Z",
    "id": "key-id-1",
    "name": "My enrollment key",
    "policy_id": "policy-id-1"
  }
}
```

#### Generic error response

Example of a generic error response

```json
{
  "error": "Bad Request",
  "message": "An error message describing what went wrong",
  "statusCode": 400
}
```

