---
type: api
title: Create an APM agent key
description: **Spaces method and path for this operation:**
url: https://www.elastic.co/elastic/docs-builder/docs/4493/api/doc/kibana/v8/operation/operation-createagentkey
resource: https://www.elastic.co/elastic/docs-builder/docs/4493/api/doc/kibana/v8/operation/operation-createagentkey
products:
  - Kibana
---

# Create an APM agent key

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

## Paths

- `POST` `/api/apm/agent_keys`
- `POST` `/s/{space_id}/api/apm/agent_keys`

## Description

Refer to [Spaces](https://www.elastic.co/docs/deploy-manage/manage-spaces) for more information.

Create a new agent key for APM.

## Authentication

- Api key: `Authorization: <value>`
  These APIs use key-based authentication. You must create an API key and use the encoded value in the request header. For example: `Authorization: ApiKey base64AccessApiKey`
- Basic: `Authorization: Basic <credentials>`

## Header parameters

- `elastic-api-version` (enum · default: 2023-10-31) — required
  The version of the API to use
  Values: `2023-10-31`
- `kbn-xsrf` (string) — required
  A required header to protect against CSRF attacks

## Request body

`application/json`

- `name` (string) — required
  Agent name
- `privileges` (enum[]) — required
  Privileges configuration
  Values: `event:write`, `config_agent:read`

## Responses

#### `200`

Agent key created successfully

`application/json`

- `agentKey` (object)
  Agent key
  - `api_key` (string)
  - `encoded` (string)
  - `expiration` (integer · format: int64)
  - `id` (string)
  - `name` (string)
#### `400`

Bad Request response

`application/json`

- `error` (string)
  Error type
- `message` (string)
  Error message
- `statusCode` (number)
  Error status code
#### `401`

Unauthorized response

`application/json`

- `error` (string)
  Error type
- `message` (string)
  Error message
- `statusCode` (number)
  Error status code
#### `403`

Forbidden response

`application/json`

- `error` (string)
  Error type
- `message` (string)
  Error message
- `statusCode` (number)
  Error status code
#### `500`

Internal Server Error response

`application/json`

- `error` (string)
  Error type
- `message` (string)
  Error message
- `statusCode` (number)
  Error status code
## Code examples

#### Console

```console
POST kbn:/api/apm/agent_keys
{
  "name": "string",
  "privileges": [
    "event:write"
  ]
}
```

#### curl

```curl
curl -X POST "${KIBANA_URL}/api/apm/agent_keys" \
  -H "elastic-api-version: 2023-10-31" \
  -H "kbn-xsrf: true" \
  -H "Authorization: ApiKey ${API_KEY}" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "string",
    "privileges": [
      "event:write"
    ]
  }'
```

