{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://artifacts.apis.io/samva-dev/json-schema/samva-dev-api-key-schema.json",
  "title": "ApiKey",
  "x-generated": "2026-11-10",
  "x-method": "derived",
  "x-generator": "derive-json-schema.py",
  "x-source": "openapi/samva-dev-api-keys-api-openapi.yml#/components/schemas/ApiKey",
  "type": "object",
  "properties": {
    "id": {
      "type": "string"
    },
    "name": {
      "type": "string"
    },
    "key": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ]
    },
    "start": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ]
    },
    "end": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ]
    },
    "description": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ]
    },
    "createdAt": {
      "type": "string"
    },
    "expiresAt": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ]
    },
    "lastUsedAt": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ]
    },
    "isActive": {
      "type": "boolean"
    },
    "access": {
      "$ref": "#/$defs/ApiKeyAccess"
    },
    "owner": {
      "anyOf": [
        {
          "$ref": "#/$defs/ApiKeyOwner"
        },
        {
          "type": "null"
        }
      ]
    }
  },
  "required": [
    "id",
    "name",
    "start",
    "end",
    "createdAt",
    "expiresAt",
    "lastUsedAt",
    "isActive",
    "access",
    "owner"
  ],
  "additionalProperties": false,
  "$defs": {
    "ApiKeyAccess": {
      "anyOf": [
        {
          "type": "object",
          "properties": {
            "mode": {
              "type": "string",
              "enum": [
                "full"
              ]
            }
          },
          "required": [
            "mode"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "mode": {
              "type": "string",
              "enum": [
                "restricted"
              ]
            },
            "resources": {
              "$ref": "#/$defs/ApiKeyResourceAccess"
            },
            "mailboxes": {
              "anyOf": [
                {
                  "$ref": "#/$defs/ApiKeyMailboxAccess"
                },
                {
                  "type": "null"
                }
              ]
            },
            "namespaceId": {
              "anyOf": [
                {
                  "$ref": "#/$defs/NamespaceId"
                },
                {
                  "type": "null"
                }
              ],
              "description": "Confines the key to one namespace: it reaches only that namespace's domains, senders, numbers, mailboxes, contacts, templates, and messages, never organization-level ones, and only endpoints that confine to a namespace (others answer 403). It holds grants only on `mailboxes`, `messages`, `email`, `domains`, `contacts`, and `templates`. A key scoped to listed mailboxes holds mailbox access only."
            }
          },
          "required": [
            "mode",
            "resources"
          ],
          "additionalProperties": false
        }
      ]
    },
    "ApiKeyMailboxAccess": {
      "type": "object",
      "properties": {
        "permissions": {
          "type": "array",
          "prefixItems": [
            {
              "type": "string",
              "enum": [
                "read",
                "update",
                "send",
                "quarantine.read"
              ]
            }
          ],
          "minItems": 1,
          "items": {
            "type": "string",
            "enum": [
              "read",
              "update",
              "send",
              "quarantine.read"
            ]
          },
          "description": "`read` reads mailboxes, threads, and messages; `update` changes thread state, labels, drafts, and notes; `send` sends and replies; `quarantine.read` reads quarantined mail."
        },
        "mailboxIds": {
          "anyOf": [
            {
              "type": "array",
              "prefixItems": [
                {
                  "$ref": "#/$defs/MailboxId"
                }
              ],
              "minItems": 1,
              "items": {
                "$ref": "#/$defs/MailboxId"
              }
            },
            {
              "type": "null"
            }
          ],
          "description": "The mailboxes the key may act on. Omit to reach every mailbox the owner can."
        },
        "sendMode": {
          "anyOf": [
            {
              "$ref": "#/$defs/MailboxSendMode"
            },
            {
              "type": "null"
            }
          ],
          "description": "Defaults to `send`. With `approval`, each send waits until a person other than the key approves it."
        }
      },
      "required": [
        "permissions"
      ],
      "additionalProperties": false
    },
    "ApiKeyOwner": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string"
        },
        "name": {
          "type": "string"
        },
        "email": {
          "type": "string"
        }
      },
      "required": [
        "id",
        "name",
        "email"
      ],
      "additionalProperties": false
    },
    "ApiKeyResourceAccess": {
      "type": "object",
      "properties": {
        "messages": {
          "anyOf": [
            {
              "type": "string",
              "enum": [
                "read",
                "write"
              ]
            },
            {
              "type": "null"
            }
          ]
        },
        "email": {
          "anyOf": [
            {
              "type": "string",
              "enum": [
                "read",
                "write"
              ]
            },
            {
              "type": "null"
            }
          ]
        },
        "domains": {
          "anyOf": [
            {
              "type": "string",
              "enum": [
                "read",
                "write"
              ]
            },
            {
              "type": "null"
            }
          ]
        },
        "phoneNumbers": {
          "anyOf": [
            {
              "type": "string",
              "enum": [
                "read",
                "write"
              ]
            },
            {
              "type": "null"
            }
          ]
        },
        "contacts": {
          "anyOf": [
            {
              "type": "string",
              "enum": [
                "read",
                "write"
              ]
            },
            {
              "type": "null"
            }
          ]
        },
        "conversations": {
          "anyOf": [
            {
              "type": "string",
              "enum": [
                "read",
                "write"
              ]
            },
            {
              "type": "null"
            }
          ]
        },
        "templates": {
          "anyOf": [
            {
              "type": "string",
              "enum": [
                "read",
                "write"
              ]
            },
            {
              "type": "null"
            }
          ]
        },
        "agents": {
          "anyOf": [
            {
              "type": "string",
              "enum": [
                "read",
                "write"
              ]
            },
            {
              "type": "null"
            }
          ]
        },
        "webhooks": {
          "anyOf": [
            {
              "type": "string",
              "enum": [
                "read",
                "write"
              ]
            },
            {
              "type": "null"
            }
          ]
        },
        "apiKeys": {
          "anyOf": [
            {
              "type": "string",
              "enum": [
                "read",
                "write"
              ]
            },
            {
              "type": "null"
            }
          ]
        },
        "organization": {
          "anyOf": [
            {
              "type": "string",
              "enum": [
                "read",
                "write"
              ]
            },
            {
              "type": "null"
            }
          ]
        },
        "billing": {
          "anyOf": [
            {
              "type": "string",
              "enum": [
                "read",
                "write"
              ]
            },
            {
              "type": "null"
            }
          ]
        },
        "analytics": {
          "anyOf": [
            {
              "type": "string",
              "enum": [
                "read",
                "write"
              ]
            },
            {
              "type": "null"
            }
          ]
        }
      },
      "additionalProperties": false
    },
    "MailboxId": {
      "type": "string",
      "pattern": "^mbx_[0-9a-hjkmnp-tv-z]{16,24}$",
      "x-samva-brand": "MailboxId"
    },
    "MailboxSendMode": {
      "type": "string",
      "enum": [
        "send",
        "approval"
      ],
      "description": "How the grantee's sends execute: `send` runs them within account limits; `approval` holds each one until a different member approves it."
    },
    "NamespaceId": {
      "type": "string",
      "pattern": "^ns_[0-9a-hjkmnp-tv-z]{16,24}$",
      "x-samva-brand": "NamespaceId"
    }
  }
}