{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://artifacts.apis.io/samva-dev/json-schema/samva-dev-api-key-access-schema.json",
  "title": "ApiKeyAccess",
  "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/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
    }
  ],
  "$defs": {
    "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
    },
    "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"
    }
  }
}