---
title: "Create a principal API key"
method: POST
path: "/v2/directories/{directoryId}/principals/{principalId}/api-keys"
tags: ["KongPrincipalCredential"]
---

# Create a principal API key

`POST /v2/directories/{directoryId}/principals/{principalId}/api-keys`

## Request

- Base URL: `https://global.api.konghq.com`
- URL: `https://global.api.konghq.com/v2/directories/{directoryId}/principals/{principalId}/api-keys`
- Auth: one of:
  - HTTP bearer
  - HTTP bearer
  - HTTP bearer
  - HTTP bearer

Creates a new API key for the specified principal.
Keys can be system-generated (v1) or imported with a custom secret, and may have an optional expiration date.

## Path parameters

- `directoryId` string, uuid, required
- `principalId` string, uuid, required

## Request body

- CreatePrincipalKeyBody
  - `type` 'imported' | 'v1', required
  - `name` string, nullable: Human-readable name of the API key. `null` if the key has no name.
  - `secret` string, nullable: Required if `type` is `imported`.
  - `expires_at` string, date-time, nullable: An ISO-8601 timestamp representation of entity expiration date.
  - `managed_by` ManagedBy: Stores information about what manages this entity, such as the tool or system responsible for its lifecycle (for example, `terraform`). Keys must be 1–63 characters long, start and end with an alphanumeric character, and may contain dashes (`-`), underscores (`_`), and dots (`.`). Values must be 1–127 characters long and must not be empty. Keys and values must not contain spaces or control characters.
  - `metadata` KongIdentityMetadata, nullable: A map of metadata key-value pairs associated with this entity. This metadata is passed to the data plane to enrich the execution context. During authentication, metadata is inherited and overridden in a hierarchical manner: Directory metadata is inherited by Principals, which is then inherited and overridden by Credentials.
  - `owner_id` string, uuid, nullable: Contains a unique identifier used for this resource. Principal that owns a project key. Required when creating a key on a project group and rejected with `invalid_parameters` (400) when the holder group is not a project group. The owner must be a member of the project. Immutable.

## Response `201`

A Kong Principal Key

- KongPrincipalKeyWithSecret: An API key associated with a principal, returned at creation with its secret. `metadata` is always returned in key responses; it is never required on requests.
  - `id` string, uuid, required: Contains a unique identifier used for this resource.
  - `name` string, nullable, required: Human-readable name of the API key. `null` if the key has no name.
  - `created_at` string, date-time, required: An ISO-8601 timestamp representation of entity creation date.
  - `updated_at` string, date-time, required: An ISO-8601 timestamp representation of entity update date.
  - `expires_at` string, date-time, nullable, required: An ISO-8601 timestamp representation of entity expiration date.
  - `managed_by` ManagedBy, required: Stores information about what manages this entity, such as the tool or system responsible for its lifecycle (for example, `terraform`). Keys must be 1–63 characters long, start and end with an alphanumeric character, and may contain dashes (`-`), underscores (`_`), and dots (`.`). Values must be 1–127 characters long and must not be empty. Keys and values must not contain spaces or control characters.
  - `metadata` KongIdentityMetadata, nullable: A map of metadata key-value pairs associated with this entity. This metadata is passed to the data plane to enrich the execution context. During authentication, metadata is inherited and overridden in a hierarchical manner: Directory metadata is inherited by Principals, which is then inherited and overridden by Credentials.
  - `owner_id` string, uuid, nullable, required: Owner principal of a project key; `null` when the key is not a project key.
  - `secret` string, required: The API key secret value. This is only returned when the key is first created and cannot be retrieved again later.

## Other responses

- `400`: Bad Request
- `401`: Unauthorized
- `403`: Forbidden
- `409`: Conflict

## Changes

- **2026-12-03** `9c8d300657bd`: 1 info
  - endpoint added

[Operation history](https://skmtc.dev/kong/apis/konnect-api-beta/changes/v2/directories/:directoryId/principals/:principalId/api-keys/post.md)

---

[API](https://skmtc.dev/kong/apis/konnect-api-beta.md) - [All operations](https://skmtc.dev/kong/apis/konnect-api-beta/llms.txt) - [OpenAPI document](https://skmtc.dev/kong/apis/konnect-api-beta/revisions/9c8d300657bd?raw)
