# Create an API key

`POST /api/v1/api-keys`

Mints a 256-bit key. The plaintext secret is returned in this response and nowhere else — only its sha256 hash and last 4 characters are stored. This route deliberately does NOT honor Idempotency-Key: replaying a stored response would mean persisting the secret a second time.

No API key can call this route: it needs a signed-in dashboard user with the right role. A key answers 403.

## Request body

JSON Schema:

```json
{
  "type": "object",
  "properties": {
    "name": {
      "type": "string",
      "minLength": 1,
      "maxLength": 200
    },
    "key_type": {
      "type": "string",
      "enum": [
        "developer",
        "agent",
        "publishable"
      ],
      "example": "developer",
      "description": "developer -> `gpy_` prefix, agent -> `agk_`, publishable -> `pk_` (browser-safe, takes no scopes). Defaults to developer."
    },
    "scopes": {
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 1,
        "maxLength": 100
      },
      "maxItems": 50
    },
    "expires_at": {
      "anyOf": [
        {
          "type": "string",
          "format": "date-time"
        },
        {
          "type": "string",
          "enum": [
            "never"
          ]
        }
      ],
      "description": "When the key stops working; must be in the future. `never` for no expiry. Omitted: 90 days from now, except a publishable key, which never expires by default."
    }
  },
  "required": [
    "name"
  ]
}
```

## Responses

### 201

API key created. Store `secret` now; it is unrecoverable.

JSON Schema:

```json
{
  "type": "object",
  "properties": {
    "data": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string",
          "format": "uuid",
          "example": "3fa85f64-5717-4562-b3fc-2c963f66afa6"
        },
        "name": {
          "type": "string"
        },
        "key_type": {
          "type": "string",
          "enum": [
            "developer",
            "agent",
            "publishable"
          ],
          "example": "developer"
        },
        "mode": {
          "type": "string",
          "enum": [
            "test",
            "live"
          ]
        },
        "key_prefix": {
          "type": "string",
          "enum": [
            "gpy_",
            "agk_",
            "pk_"
          ]
        },
        "key_hint": {
          "type": "string"
        },
        "scopes": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "expires_at": {
          "type": [
            "string",
            "null"
          ],
          "format": "date-time",
          "description": "Null when the key never expires."
        },
        "secret": {
          "type": "string",
          "description": "The full plaintext key, returned by THIS response only. Only a sha256 hash is stored; it cannot be retrieved again.",
          "example": "gpy_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
        }
      },
      "required": [
        "id",
        "name",
        "key_type",
        "mode",
        "key_prefix",
        "key_hint",
        "scopes",
        "expires_at",
        "secret"
      ]
    },
    "_agent": {
      "type": "object",
      "properties": {
        "summary": {
          "type": "string"
        },
        "status": {
          "type": "string",
          "enum": [
            "ok"
          ]
        },
        "next_actions": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "warnings": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      },
      "required": [
        "summary",
        "status",
        "next_actions",
        "warnings"
      ]
    },
    "api_version": {
      "type": "string",
      "example": "2026-07-01"
    }
  },
  "required": [
    "data",
    "_agent",
    "api_version"
  ]
}
```
