{
  "openapi": "3.0.3",
  "info": {
    "title": "Mirqam Integration API",
    "version": "1.0.0",
    "description": "Feature-gated developer release. https://bot.mirqam.sa/integration-docs/"
  },
  "servers": [
    {
      "url": "https://bot.mirqam.sa/api/v1"
    }
  ],
  "paths": {
    "/account": {
      "get": {
        "summary": "Account mode, channel and capabilities",
        "operationId": "get_account",
        "x-required-scope": "account:read",
        "security": [
          {
            "ApiKey": []
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "id": {
                      "type": "string"
                    },
                    "mode": {
                      "type": "string"
                    },
                    "channel": {
                      "type": "string"
                    },
                    "scopes": {
                      "type": "array",
                      "items": {
                        "type": "string"
                      }
                    },
                    "whatsapp": {
                      "type": "object",
                      "properties": {
                        "mode": {
                          "type": "string"
                        },
                        "status": {
                          "type": "string"
                        }
                      }
                    },
                    "docs": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Validation error",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Invalid credentials or verification failure",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Insufficient scope, account or quota restriction",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Resource not found",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Idempotency or channel conflict. error.code: channel_not_connected, cloud_channel_not_ready (both with Retry-After and error.retryAfter), idempotency_conflict or conversation_requires_live_key",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Rate limited; honor Retry-After",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              },
              "Retry-After": {
                "schema": {
                  "type": "integer"
                },
                "description": "Seconds before retry"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Internal error; retain X-Request-Id",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": []
      }
    },
    "/messages": {
      "post": {
        "summary": "Accept a durable send",
        "operationId": "post_messages",
        "x-required-scope": "messages:send",
        "security": [
          {
            "ApiKey": []
          }
        ],
        "responses": {
          "202": {
            "description": "Success",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Message"
                }
              }
            }
          },
          "400": {
            "description": "Validation error",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Invalid credentials or verification failure",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Insufficient scope, account or quota restriction",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Resource not found",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Idempotency or channel conflict. error.code: channel_not_connected, cloud_channel_not_ready (both with Retry-After and error.retryAfter), idempotency_conflict or conversation_requires_live_key",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Rate limited; honor Retry-After",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              },
              "Retry-After": {
                "schema": {
                  "type": "integer"
                },
                "description": "Seconds before retry"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Internal error; retain X-Request-Id",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "to": {
                    "type": "string",
                    "description": "E.164 phone or participating gateway group ID. Group sends additionally require groups:send.",
                    "pattern": "^(\\+[1-9]\\d{7,14}|\\d{5,24}(-\\d{5,20})?@g\\.us)$"
                  },
                  "type": {
                    "type": "string",
                    "enum": [
                      "text",
                      "image",
                      "document",
                      "audio",
                      "video",
                      "template",
                      "location",
                      "contact"
                    ]
                  },
                  "text": {
                    "type": "string",
                    "maxLength": 4096
                  },
                  "url": {
                    "type": "string",
                    "format": "uri"
                  },
                  "caption": {
                    "type": "string",
                    "maxLength": 1024
                  },
                  "fileName": {
                    "type": "string"
                  },
                  "template": {
                    "type": "object",
                    "properties": {
                      "name": {
                        "type": "string"
                      },
                      "language": {
                        "type": "string"
                      },
                      "components": {
                        "type": "array",
                        "items": {
                          "type": "object"
                        }
                      }
                    },
                    "required": [
                      "name",
                      "language"
                    ],
                    "additionalProperties": true
                  },
                  "ttlSeconds": {
                    "type": "integer",
                    "minimum": 30,
                    "maximum": 86400
                  },
                  "location": {
                    "type": "object",
                    "required": [
                      "latitude",
                      "longitude"
                    ],
                    "properties": {
                      "latitude": {
                        "type": "number",
                        "minimum": -90,
                        "maximum": 90
                      },
                      "longitude": {
                        "type": "number",
                        "minimum": -180,
                        "maximum": 180
                      },
                      "name": {
                        "type": "string",
                        "maxLength": 200
                      },
                      "address": {
                        "type": "string",
                        "maxLength": 500
                      }
                    }
                  },
                  "contact": {
                    "type": "object",
                    "required": [
                      "name",
                      "phone"
                    ],
                    "properties": {
                      "name": {
                        "type": "string",
                        "maxLength": 100
                      },
                      "phone": {
                        "type": "string",
                        "pattern": "^\\+[1-9]\\d{7,14}$"
                      }
                    }
                  }
                },
                "required": [
                  "to",
                  "type"
                ],
                "additionalProperties": true
              }
            }
          }
        },
        "parameters": [
          {
            "in": "header",
            "name": "Idempotency-Key",
            "required": true,
            "schema": {
              "type": "string",
              "minLength": 8,
              "maxLength": 128
            }
          }
        ],
        "description": "Group destinations require BOTH messages:send and groups:send. Cloud group sending and group authentication templates are unsupported by this Mirqam adapter. Test keys simulate sends; they never deliver to WhatsApp."
      },
      "get": {
        "summary": "List messages with cursor pagination",
        "operationId": "get_messages",
        "x-required-scope": "messages:read",
        "security": [
          {
            "ApiKey": []
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "rows": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Message"
                      }
                    },
                    "nextCursor": {
                      "type": "string",
                      "nullable": true
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Validation error",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Invalid credentials or verification failure",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Insufficient scope, account or quota restriction",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Resource not found",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Idempotency or channel conflict. error.code: channel_not_connected, cloud_channel_not_ready (both with Retry-After and error.retryAfter), idempotency_conflict or conversation_requires_live_key",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Rate limited; honor Retry-After",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              },
              "Retry-After": {
                "schema": {
                  "type": "integer"
                },
                "description": "Seconds before retry"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Internal error; retain X-Request-Id",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "in": "query",
            "name": "cursor",
            "schema": {
              "type": "string"
            }
          }
        ]
      }
    },
    "/messages/{id}": {
      "get": {
        "summary": "Inspect a message lifecycle",
        "operationId": "get_messages_id",
        "x-required-scope": "messages:read",
        "security": [
          {
            "ApiKey": []
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Message"
                }
              }
            }
          },
          "400": {
            "description": "Validation error",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Invalid credentials or verification failure",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Insufficient scope, account or quota restriction",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Resource not found",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Idempotency or channel conflict. error.code: channel_not_connected, cloud_channel_not_ready (both with Retry-After and error.retryAfter), idempotency_conflict or conversation_requires_live_key",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Rate limited; honor Retry-After",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              },
              "Retry-After": {
                "schema": {
                  "type": "integer"
                },
                "description": "Seconds before retry"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Internal error; retain X-Request-Id",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "in": "path",
            "name": "id",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ]
      }
    },
    "/verifications": {
      "post": {
        "summary": "Request a managed OTP challenge",
        "operationId": "post_verifications",
        "x-required-scope": "verifications:write",
        "security": [
          {
            "ApiKey": []
          }
        ],
        "responses": {
          "202": {
            "description": "Success",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Verification"
                }
              }
            }
          },
          "400": {
            "description": "Validation error",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Invalid credentials or verification failure",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Insufficient scope, account or quota restriction",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Resource not found",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Idempotency or channel conflict. error.code: channel_not_connected, cloud_channel_not_ready (both with Retry-After and error.retryAfter), idempotency_conflict or conversation_requires_live_key",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Rate limited; honor Retry-After",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              },
              "Retry-After": {
                "schema": {
                  "type": "integer"
                },
                "description": "Seconds before retry"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Internal error; retain X-Request-Id",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "phone": {
                    "type": "string",
                    "pattern": "^\\+[1-9]\\d{7,14}$"
                  },
                  "clientId": {
                    "type": "string",
                    "maxLength": 100
                  },
                  "purpose": {
                    "type": "string",
                    "maxLength": 120
                  }
                },
                "required": [
                  "phone",
                  "clientId",
                  "purpose"
                ],
                "additionalProperties": true
              }
            }
          }
        },
        "parameters": []
      }
    },
    "/verifications/{id}/verify": {
      "post": {
        "summary": "Consume a challenge exactly once",
        "operationId": "post_verifications_id_verify",
        "x-required-scope": "verifications:write",
        "security": [
          {
            "ApiKey": []
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Verified"
                }
              }
            }
          },
          "400": {
            "description": "Validation error",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Invalid credentials or verification failure",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Insufficient scope, account or quota restriction",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Resource not found",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Idempotency or channel conflict. error.code: channel_not_connected, cloud_channel_not_ready (both with Retry-After and error.retryAfter), idempotency_conflict or conversation_requires_live_key",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Rate limited; honor Retry-After",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              },
              "Retry-After": {
                "schema": {
                  "type": "integer"
                },
                "description": "Seconds before retry"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Internal error; retain X-Request-Id",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "code": {
                    "type": "string",
                    "pattern": "^\\d{6}$"
                  },
                  "clientId": {
                    "type": "string"
                  },
                  "purpose": {
                    "type": "string"
                  }
                },
                "required": [
                  "code",
                  "clientId",
                  "purpose"
                ],
                "additionalProperties": true
              }
            }
          }
        },
        "parameters": [
          {
            "in": "path",
            "name": "id",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ]
      }
    },
    "/webhooks": {
      "get": {
        "summary": "List subscriptions",
        "operationId": "get_webhooks",
        "x-required-scope": "webhooks:write",
        "security": [
          {
            "ApiKey": []
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "rows": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Webhook"
                      }
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Validation error",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Invalid credentials or verification failure",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Insufficient scope, account or quota restriction",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Resource not found",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Idempotency or channel conflict. error.code: channel_not_connected, cloud_channel_not_ready (both with Retry-After and error.retryAfter), idempotency_conflict or conversation_requires_live_key",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Rate limited; honor Retry-After",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              },
              "Retry-After": {
                "schema": {
                  "type": "integer"
                },
                "description": "Seconds before retry"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Internal error; retain X-Request-Id",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": []
      },
      "post": {
        "summary": "Create a signed-event subscription",
        "operationId": "post_webhooks",
        "x-required-scope": "webhooks:write",
        "security": [
          {
            "ApiKey": []
          }
        ],
        "responses": {
          "201": {
            "description": "Success",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "id": {
                      "type": "string"
                    },
                    "secret": {
                      "type": "string",
                      "description": "One-time secret on create or rotate"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Validation error",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Invalid credentials or verification failure",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Insufficient scope, account or quota restriction",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Resource not found",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Idempotency or channel conflict. error.code: channel_not_connected, cloud_channel_not_ready (both with Retry-After and error.retryAfter), idempotency_conflict or conversation_requires_live_key",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Rate limited; honor Retry-After",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              },
              "Retry-After": {
                "schema": {
                  "type": "integer"
                },
                "description": "Seconds before retry"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Internal error; retain X-Request-Id",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "url": {
                    "type": "string",
                    "format": "uri"
                  },
                  "events": {
                    "type": "array",
                    "minItems": 1,
                    "items": {
                      "type": "string",
                      "enum": [
                        "message.received",
                        "message.accepted",
                        "message.sent",
                        "message.delivered",
                        "message.read",
                        "message.failed",
                        "message.expired",
                        "message.unknown",
                        "verification.created",
                        "verification.verified"
                      ]
                    }
                  }
                },
                "required": [
                  "url",
                  "events"
                ],
                "additionalProperties": true
              }
            }
          }
        },
        "parameters": []
      }
    },
    "/webhooks/{id}": {
      "patch": {
        "summary": "Pause, resume or rotate a subscription",
        "operationId": "patch_webhooks_id",
        "x-required-scope": "webhooks:write",
        "security": [
          {
            "ApiKey": []
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "id": {
                      "type": "string"
                    },
                    "secret": {
                      "type": "string",
                      "description": "One-time secret on create or rotate"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Validation error",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Invalid credentials or verification failure",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Insufficient scope, account or quota restriction",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Resource not found",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Idempotency or channel conflict. error.code: channel_not_connected, cloud_channel_not_ready (both with Retry-After and error.retryAfter), idempotency_conflict or conversation_requires_live_key",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Rate limited; honor Retry-After",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              },
              "Retry-After": {
                "schema": {
                  "type": "integer"
                },
                "description": "Seconds before retry"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Internal error; retain X-Request-Id",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "active": {
                    "type": "boolean"
                  },
                  "rotate": {
                    "type": "boolean"
                  }
                },
                "required": [],
                "additionalProperties": true
              }
            }
          }
        },
        "parameters": [
          {
            "in": "path",
            "name": "id",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ]
      },
      "delete": {
        "summary": "Cancel a subscription and pending retries",
        "operationId": "delete_webhooks_id",
        "x-required-scope": "webhooks:write",
        "security": [
          {
            "ApiKey": []
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Validation error",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Invalid credentials or verification failure",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Insufficient scope, account or quota restriction",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Resource not found",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Idempotency or channel conflict. error.code: channel_not_connected, cloud_channel_not_ready (both with Retry-After and error.retryAfter), idempotency_conflict or conversation_requires_live_key",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Rate limited; honor Retry-After",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              },
              "Retry-After": {
                "schema": {
                  "type": "integer"
                },
                "description": "Seconds before retry"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Internal error; retain X-Request-Id",
            "headers": {
              "X-Request-Id": {
                "schema": {
                  "type": "string"
                },
                "description": "Correlation ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "in": "path",
            "name": "id",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ]
      }
    },
    "/capabilities": {
      "get": {
        "summary": "Inspect supported channel capabilities",
        "x-required-scope": "account:read",
        "security": [
          {
            "ApiKey": []
          }
        ],
        "parameters": [],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "401": {
            "description": "Invalid or expired API key"
          },
          "403": {
            "description": "Missing scope or inactive subscription"
          },
          "404": {
            "description": "Group does not belong to the channel"
          },
          "409": {
            "description": "groups_require_gateway_channel or channel_not_connected"
          },
          "429": {
            "description": "Rate limited"
          }
        }
      }
    },
    "/groups": {
      "get": {
        "summary": "List participating groups in the API key channel",
        "x-required-scope": "groups:read",
        "security": [
          {
            "ApiKey": []
          }
        ],
        "parameters": [
          {
            "in": "query",
            "name": "limit",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 100,
              "default": 50
            }
          },
          {
            "in": "query",
            "name": "cursor",
            "schema": {
              "type": "string"
            },
            "description": "nextCursor from the previous page. Keep the same search filter."
          },
          {
            "in": "query",
            "name": "search",
            "schema": {
              "type": "string",
              "maxLength": 100
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "rows": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Group"
                      }
                    },
                    "nextCursor": {
                      "type": "string",
                      "nullable": true
                    },
                    "simulated": {
                      "type": "boolean"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Invalid or expired API key"
          },
          "403": {
            "description": "Missing scope or inactive subscription"
          },
          "404": {
            "description": "Group does not belong to the channel"
          },
          "409": {
            "description": "groups_require_gateway_channel or channel_not_connected"
          },
          "429": {
            "description": "Rate limited"
          }
        }
      }
    },
    "/groups/{id}": {
      "get": {
        "summary": "Get group metadata and member roles",
        "x-required-scope": "groups:read",
        "security": [
          {
            "ApiKey": []
          }
        ],
        "parameters": [
          {
            "in": "path",
            "name": "id",
            "required": true,
            "schema": {
              "type": "string"
            },
            "description": "URL-encoded group JID from GET /groups."
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Group"
                }
              }
            }
          },
          "401": {
            "description": "Invalid or expired API key"
          },
          "403": {
            "description": "Missing scope or inactive subscription"
          },
          "404": {
            "description": "Group does not belong to the channel"
          },
          "409": {
            "description": "groups_require_gateway_channel or channel_not_connected"
          },
          "429": {
            "description": "Rate limited"
          }
        }
      }
    },
    "/conversations/{id}/checkout": {
      "get": {
        "summary": "status conversation-only WhatsApp checkout pause (15 minute idle expiry)",
        "operationId": "checkout_status",
        "x-required-scope": "messages:read",
        "security": [
          {
            "ApiKey": []
          }
        ],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Checkout state: conversationId, active, sessionId, expiresAt, reason. Expiry is automatic and persisted."
          },
          "401": {
            "description": "Invalid API key"
          },
          "403": {
            "description": "Missing scope or account access"
          },
          "404": {
            "description": "Conversation outside tenant/channel or group"
          },
          "409": {
            "description": "Test key, human handoff, active session, reused session or session mismatch"
          }
        }
      },
      "post": {
        "summary": "start conversation-only WhatsApp checkout pause (15 minute idle expiry)",
        "operationId": "checkout_start",
        "x-required-scope": "messages:send",
        "security": [
          {
            "ApiKey": []
          }
        ],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Checkout state: conversationId, active, sessionId, expiresAt, reason. Expiry is automatic and persisted."
          },
          "401": {
            "description": "Invalid API key"
          },
          "403": {
            "description": "Missing scope or account access"
          },
          "404": {
            "description": "Conversation outside tenant/channel or group"
          },
          "409": {
            "description": "Test key, human handoff, active session, reused session or session mismatch"
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "sessionId"
                ],
                "properties": {
                  "sessionId": {
                    "type": "string",
                    "pattern": "^[a-zA-Z0-9_-]{8,100}$"
                  }
                }
              }
            }
          }
        }
      }
    },
    "/conversations/{id}/checkout/resume": {
      "post": {
        "summary": "resume conversation-only WhatsApp checkout pause (15 minute idle expiry)",
        "operationId": "checkout_resume",
        "x-required-scope": "messages:send",
        "security": [
          {
            "ApiKey": []
          }
        ],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Checkout state: conversationId, active, sessionId, expiresAt, reason. Expiry is automatic and persisted."
          },
          "401": {
            "description": "Invalid API key"
          },
          "403": {
            "description": "Missing scope or account access"
          },
          "404": {
            "description": "Conversation outside tenant/channel or group"
          },
          "409": {
            "description": "Test key, human handoff, active session, reused session or session mismatch"
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "sessionId"
                ],
                "properties": {
                  "sessionId": {
                    "type": "string",
                    "pattern": "^[a-zA-Z0-9_-]{8,100}$"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "ApiKey": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "mq_test_ or mq_live_ API key"
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "required": [
          "error"
        ],
        "properties": {
          "error": {
            "type": "object",
            "required": [
              "code",
              "requestId"
            ],
            "properties": {
              "code": {
                "type": "string"
              },
              "retryAfter": {
                "type": "integer",
                "description": "Seconds before retrying (channel 409s and blocked sends)"
              },
              "requestId": {
                "type": "string"
              }
            }
          },
          "verified": {
            "type": "boolean"
          }
        }
      },
      "Message": {
        "type": "object",
        "required": [
          "id",
          "status",
          "mode",
          "channel"
        ],
        "properties": {
          "id": {
            "type": "string"
          },
          "status": {
            "type": "string",
            "enum": [
              "accepted",
              "sending",
              "sent",
              "delivered",
              "read",
              "failed",
              "expired",
              "unknown"
            ]
          },
          "mode": {
            "type": "string",
            "enum": [
              "test",
              "live"
            ]
          },
          "channel": {
            "type": "string"
          },
          "providerId": {
            "type": "string",
            "nullable": true
          },
          "verificationId": {
            "type": "string",
            "nullable": true
          },
          "error": {
            "type": "string",
            "nullable": true
          },
          "createdAt": {
            "type": "string",
            "format": "date-time"
          },
          "updatedAt": {
            "type": "string",
            "format": "date-time"
          },
          "expiresAt": {
            "type": "string",
            "format": "date-time"
          },
          "replayed": {
            "type": "boolean"
          }
        }
      },
      "Verification": {
        "type": "object",
        "required": [
          "verificationId",
          "messageId",
          "clientId",
          "purpose",
          "expiresAt",
          "simulated"
        ],
        "properties": {
          "verificationId": {
            "type": "string"
          },
          "messageId": {
            "type": "string"
          },
          "clientId": {
            "type": "string"
          },
          "purpose": {
            "type": "string"
          },
          "expiresAt": {
            "type": "string",
            "format": "date-time"
          },
          "simulated": {
            "type": "boolean"
          },
          "testCode": {
            "type": "string",
            "description": "Test mode only; never returned by live keys"
          }
        }
      },
      "Verified": {
        "type": "object",
        "required": [
          "verified",
          "verificationId",
          "purpose",
          "clientId"
        ],
        "properties": {
          "verified": {
            "type": "boolean",
            "enum": [
              true
            ]
          },
          "verificationId": {
            "type": "string"
          },
          "purpose": {
            "type": "string"
          },
          "clientId": {
            "type": "string"
          }
        }
      },
      "Webhook": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "url": {
            "type": "string",
            "format": "uri"
          },
          "mode": {
            "type": "string"
          },
          "active": {
            "type": "boolean"
          },
          "events": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "created": {
            "type": "string",
            "format": "date-time"
          }
        }
      },
      "Group": {
        "type": "object",
        "required": [
          "id",
          "name",
          "size",
          "announcement"
        ],
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "size": {
            "type": "integer"
          },
          "announcement": {
            "type": "boolean"
          },
          "description": {
            "type": "string"
          },
          "participants": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "id": {
                  "type": "string",
                  "description": "WhatsApp JID; may be a privacy LID, not a phone number."
                },
                "role": {
                  "type": "string",
                  "enum": [
                    "owner",
                    "admin",
                    "member"
                  ]
                }
              }
            }
          },
          "simulated": {
            "type": "boolean"
          }
        }
      }
    }
  }
}
