{
  "openapi": "3.0.3",
  "info": {
    "title": "WAG WAG API",
    "version": "0.2.0",
    "description": "WAG WAG independent messaging API. Server-side bearer keys with tenant isolation. OpenAI message translations are opt-in; original messages and HighLevel continue unchanged. See translations.md for integration."
  },
  "servers": [
    {
      "url": "https://bridge.wagwagchat.com",
      "description": "WAG WAG public API origin"
    }
  ],
  "security": [
    {
      "ApiKey": []
    }
  ],
  "paths": {
    "/health": {
      "get": {
        "summary": "API/database health",
        "operationId": "get__health",
        "description": "Does not verify worker liveness or Evolution health.",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    }
                  },
                  "required": [
                    "ok"
                  ]
                }
              }
            }
          },
          "default": {
            "description": "Request failed. See error.code; no WhatsApp send is automatically retried.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "security": []
      }
    },
    "/v1/me": {
      "get": {
        "summary": "Current tenant and key permissions",
        "operationId": "get__v1_me",
        "description": "",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "tenant_id": {
                      "type": "string",
                      "format": "uuid"
                    },
                    "scopes": {
                      "type": "array",
                      "items": {
                        "type": "string"
                      }
                    }
                  },
                  "required": [
                    "tenant_id",
                    "scopes"
                  ]
                }
              }
            }
          },
          "default": {
            "description": "Request failed. See error.code; no WhatsApp send is automatically retried.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/v1/keys": {
      "post": {
        "summary": "Issue a scoped server-side key",
        "operationId": "post__v1_keys",
        "description": "Cannot grant scopes absent from the calling key. Required scope: `keys:manage`.",
        "responses": {
          "201": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Key"
                }
              }
            }
          },
          "default": {
            "description": "Request failed. See error.code; no WhatsApp send is automatically retried.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "label": {
                    "type": "string",
                    "maxLength": 100
                  },
                  "scopes": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "enum": [
                        "connections:read",
                        "connections:write",
                        "messages:read",
                        "messages:write",
                        "webhooks:manage",
                        "keys:manage",
                        "messages:translate"
                      ]
                    }
                  }
                },
                "required": [
                  "label",
                  "scopes"
                ],
                "additionalProperties": false
              }
            }
          }
        }
      }
    },
    "/v1/keys/{id}": {
      "delete": {
        "summary": "Revoke own tenant key",
        "operationId": "delete__v1_keys_id",
        "description": " Required scope: `keys:manage`.",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "revoked": {
                      "type": "boolean"
                    }
                  },
                  "required": [
                    "revoked"
                  ]
                }
              }
            }
          },
          "default": {
            "description": "Request failed. See error.code; no WhatsApp send is automatically retried.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "$ref": "#/components/parameters/Id"
          }
        ]
      }
    },
    "/v1/connections": {
      "post": {
        "summary": "Provision a new API-only line",
        "operationId": "post__v1_connections",
        "description": "Does not import or reconfigure existing HighLevel lines. provisioning_unknown needs review; no automatic create retry. Required scope: `connections:write`.",
        "responses": {
          "201": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Connection"
                }
              }
            }
          },
          "default": {
            "description": "Request failed. See error.code; no WhatsApp send is automatically retried.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "$ref": "#/components/parameters/IdempotencyKey"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "label": {
                    "type": "string",
                    "maxLength": 100
                  }
                },
                "required": [
                  "label"
                ],
                "additionalProperties": false
              }
            }
          }
        }
      },
      "get": {
        "summary": "List own lines",
        "operationId": "get__v1_connections",
        "description": " Required scope: `connections:read`.",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Connection"
                      }
                    }
                  },
                  "required": [
                    "data"
                  ]
                }
              }
            }
          },
          "default": {
            "description": "Request failed. See error.code; no WhatsApp send is automatically retried.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/v1/connections/{id}": {
      "get": {
        "summary": "Read cached line",
        "operationId": "get__v1_connections_id",
        "description": " Required scope: `connections:read`.",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Connection"
                }
              }
            }
          },
          "default": {
            "description": "Request failed. See error.code; no WhatsApp send is automatically retried.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "$ref": "#/components/parameters/Id"
          }
        ]
      }
    },
    "/v1/connections/{id}/status": {
      "get": {
        "summary": "Read fresh provider connection status",
        "operationId": "get__v1_connections_id_status",
        "description": " Required scope: `connections:read`.",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Connection"
                }
              }
            }
          },
          "default": {
            "description": "Request failed. See error.code; no WhatsApp send is automatically retried.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "$ref": "#/components/parameters/Id"
          }
        ]
      }
    },
    "/v1/connections/{id}/connect": {
      "post": {
        "summary": "Request pairing for own line",
        "operationId": "post__v1_connections_id_connect",
        "description": "At most one provider connect request per 60 seconds; connected lines are not restarted. Required scope: `connections:write`.",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Pairing"
                }
              }
            }
          },
          "default": {
            "description": "Request failed. See error.code; no WhatsApp send is automatically retried.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "$ref": "#/components/parameters/Id"
          }
        ]
      }
    },
    "/v1/connections/{id}/pairing": {
      "get": {
        "summary": "Read unexpired QR",
        "operationId": "get__v1_connections_id_pairing",
        "description": " Required scope: `connections:read`.",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Pairing"
                }
              }
            }
          },
          "default": {
            "description": "Request failed. See error.code; no WhatsApp send is automatically retried.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "$ref": "#/components/parameters/Id"
          }
        ]
      }
    },
    "/v1/messages": {
      "post": {
        "summary": "Send text, image, video or PDF",
        "operationId": "post__v1_messages",
        "description": "201 means the message resource exists, not delivered. 202 means durably queued because of a local send-rate window or earlier same-line work. The worker automatically sends only queued requests, in line order, after eligibility is rechecked. Inspect status/error_code and subscribe to message.status; accepted is not delivery confirmation. Duplicate Idempotency-Key with the same body returns the same message and current status. failed/uncertain/cancelled are terminal and are never automatically resent. Required scope: `messages:write`. Verified incoming customer activity to this physical line opens a rolling 24-hour reply window, exempt from outreach pacing and distinct-recipient quota. Otherwise configured new_daily_limit applies to distinct people per rolling 24 hours (new-account default 10), plus 5/minute, 20/10 minutes and 60/hour pacing. Same-person repeats count once but refresh their quota timestamp. Total daily limits and safety checks remain. Active conversations can bypass unrelated cold queued work, while same-conversation order is preserved.",
        "responses": {
          "201": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Message"
                }
              }
            }
          },
          "default": {
            "description": "Request failed. See error.code; no WhatsApp send is automatically retried.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "202": {
            "description": "Queued due to first-outreach quota, sending speed or earlier same-conversation work. Inspect queue_reason and scheduled_at; do not submit a new idempotency key. Waiting normally expires after 24h, with at most 60s fixed initial quota scheduling grace.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Message"
                }
              }
            }
          },
          "429": {
            "description": "Queue full (send_queue_full), daily quota (daily_limit), or API request quota (rate_limit). Rejected requests are not automatically enrolled or resent.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "$ref": "#/components/parameters/IdempotencyKey"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/MessageInput"
              }
            }
          }
        }
      }
    },
    "/v1/messages/{id}": {
      "get": {
        "summary": "Read message state",
        "operationId": "get__v1_messages_id",
        "description": " Required scope: `messages:read`.",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Message"
                }
              }
            }
          },
          "default": {
            "description": "Request failed. See error.code; no WhatsApp send is automatically retried.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "$ref": "#/components/parameters/Id"
          },
          {
            "name": "translation_language",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "Include cached translation or null for this language. Does not trigger AI."
          }
        ]
      }
    },
    "/v1/messages/{id}/media": {
      "get": {
        "summary": "Retrieve media base64",
        "operationId": "get__v1_messages_id_media",
        "description": "Inbound download depends on provider availability/expiry; not a permanent media archive. Required scope: `messages:read`.",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "base64": {
                      "type": "string"
                    },
                    "mime_type": {
                      "type": "string"
                    },
                    "file_name": {
                      "type": "string",
                      "nullable": true
                    }
                  },
                  "required": [
                    "base64",
                    "mime_type",
                    "file_name"
                  ]
                }
              }
            }
          },
          "default": {
            "description": "Request failed. See error.code; no WhatsApp send is automatically retried.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "$ref": "#/components/parameters/Id"
          }
        ]
      }
    },
    "/v1/conversations": {
      "get": {
        "summary": "List own conversations",
        "operationId": "get__v1_conversations",
        "description": " Required scope: `messages:read`.",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Conversation"
                      }
                    },
                    "next_cursor": {
                      "type": "string",
                      "nullable": true
                    }
                  },
                  "required": [
                    "data",
                    "next_cursor"
                  ]
                }
              }
            }
          },
          "default": {
            "description": "Request failed. See error.code; no WhatsApp send is automatically retried.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "$ref": "#/components/parameters/Limit"
          },
          {
            "$ref": "#/components/parameters/Cursor"
          }
        ]
      }
    },
    "/v1/conversations/{id}/messages": {
      "get": {
        "summary": "Read conversation history",
        "operationId": "get__v1_conversations_id_messages",
        "description": " Required scope: `messages:read`.",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Message"
                      }
                    },
                    "next_cursor": {
                      "type": "string",
                      "nullable": true
                    }
                  },
                  "required": [
                    "data",
                    "next_cursor"
                  ]
                }
              }
            }
          },
          "default": {
            "description": "Request failed. See error.code; no WhatsApp send is automatically retried.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "$ref": "#/components/parameters/Id"
          },
          {
            "$ref": "#/components/parameters/Limit"
          },
          {
            "$ref": "#/components/parameters/Cursor"
          },
          {
            "name": "translation_language",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "Include cached translation or null for this language. Does not trigger AI."
          }
        ]
      }
    },
    "/v1/events": {
      "get": {
        "summary": "Read own event history",
        "operationId": "get__v1_events",
        "description": " Required scope: `messages:read`.",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Event"
                      }
                    },
                    "next_cursor": {
                      "type": "string",
                      "nullable": true
                    }
                  },
                  "required": [
                    "data",
                    "next_cursor"
                  ]
                }
              }
            }
          },
          "default": {
            "description": "Request failed. See error.code; no WhatsApp send is automatically retried.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "$ref": "#/components/parameters/Limit"
          },
          {
            "$ref": "#/components/parameters/Cursor"
          }
        ]
      }
    },
    "/v1/webhooks": {
      "post": {
        "summary": "Register event receiver",
        "operationId": "post__v1_webhooks",
        "description": "HTTPS port 443, public DNS addresses only. No redirects. Up to eight delivery attempts; deduplicate event IDs and validate signatures. No backfill of events before registration. Required scope: `webhooks:manage`.",
        "responses": {
          "201": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/WebhookCreated"
                }
              }
            }
          },
          "default": {
            "description": "Request failed. See error.code; no WhatsApp send is automatically retried.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "url": {
                    "type": "string",
                    "format": "uri",
                    "maxLength": 2048
                  }
                },
                "required": [
                  "url"
                ],
                "additionalProperties": false
              }
            }
          }
        }
      },
      "get": {
        "summary": "List receivers (secrets omitted)",
        "operationId": "get__v1_webhooks",
        "description": " Required scope: `webhooks:manage`.",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Webhook"
                      }
                    }
                  },
                  "required": [
                    "data"
                  ]
                }
              }
            }
          },
          "default": {
            "description": "Request failed. See error.code; no WhatsApp send is automatically retried.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/v1/webhooks/{id}": {
      "delete": {
        "summary": "Disable event receiver",
        "operationId": "delete__v1_webhooks_id",
        "description": " Required scope: `webhooks:manage`.",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "disabled": {
                      "type": "boolean"
                    }
                  },
                  "required": [
                    "disabled"
                  ]
                }
              }
            }
          },
          "default": {
            "description": "Request failed. See error.code; no WhatsApp send is automatically retried.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "$ref": "#/components/parameters/Id"
          }
        ]
      }
    },
    "/v1/translation-settings": {
      "get": {
        "operationId": "getTranslationSettings",
        "summary": "Translation preferences and supported languages",
        "description": "Requires messages:read. Disabled by default.",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/TranslationSettings"
                }
              }
            }
          },
          "default": {
            "description": "Request failed; original message remains unchanged.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      },
      "patch": {
        "operationId": "updateTranslationSettings",
        "summary": "Enable translations and change default language",
        "description": "Requires messages:read and messages:translate. Does not backfill or submit messages.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "additionalProperties": false,
                "properties": {
                  "enabled": {
                    "type": "boolean"
                  },
                  "target_language": {
                    "type": "string",
                    "example": "tr"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/TranslationSettings"
                }
              }
            }
          },
          "default": {
            "description": "Request failed; original message remains unchanged.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/v1/messages/{id}/translation": {
      "post": {
        "operationId": "requestMessageTranslation",
        "summary": "Translate an incoming message or reuse cached result",
        "description": "Requires messages:read and messages:translate. Only stored inbound text/captions; never sends WhatsApp messages. Natural idempotency by message content and language. Explicit retry:true retries a failed job and consumes budget.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "additionalProperties": false,
                "properties": {
                  "target_language": {
                    "type": "string",
                    "example": "tr"
                  },
                  "retry": {
                    "type": "boolean",
                    "default": false
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Translation"
                }
              }
            }
          },
          "202": {
            "description": "Translation job state",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Translation"
                }
              }
            }
          },
          "default": {
            "description": "Request failed; original message remains unchanged.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      },
      "get": {
        "operationId": "getMessageTranslation",
        "summary": "Read translation state without provider calls",
        "description": "Requires messages:read. 404 when missing.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          },
          {
            "name": "target_language",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "Omit to use account default (tr initially)."
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Translation"
                }
              }
            }
          },
          "default": {
            "description": "Request failed; original message remains unchanged.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/v1/messages/{messageId}/cancel": {
      "post": {
        "summary": "Cancel an unsent queued message",
        "operationId": "cancelQueuedMessage",
        "description": "Requires messages:write for the owning tenant. Idempotent for an already cancelled message. Returns message_not_queued (409) once dispatch has begun; cancellation cannot recall a transmitted message.",
        "parameters": [
          {
            "name": "messageId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "requestBody": {
          "required": false,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "additionalProperties": false
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Cancelled message; emits message.status on first cancellation.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Message"
                }
              }
            }
          },
          "default": {
            "description": "Request rejected; see error.code.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "ApiKey": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "ww_live_*"
      }
    },
    "parameters": {
      "IdempotencyKey": {
        "name": "Idempotency-Key",
        "in": "header",
        "required": true,
        "schema": {
          "type": "string",
          "pattern": "^[A-Za-z0-9_.:-]{8,128}$"
        },
        "description": "Retain for all retries of the same operation; different body returns 409."
      },
      "Id": {
        "name": "id",
        "in": "path",
        "required": true,
        "schema": {
          "type": "string",
          "format": "uuid"
        }
      },
      "Limit": {
        "name": "limit",
        "in": "query",
        "schema": {
          "type": "integer",
          "minimum": 1,
          "maximum": 100,
          "default": 50
        }
      },
      "Cursor": {
        "name": "cursor",
        "in": "query",
        "schema": {
          "type": "string"
        },
        "description": "Opaque next_cursor from the previous response. Newest first."
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "object",
            "properties": {
              "code": {
                "type": "string"
              },
              "request_id": {
                "type": "string",
                "format": "uuid"
              }
            },
            "required": [
              "code",
              "request_id"
            ]
          }
        },
        "required": [
          "error"
        ]
      },
      "Key": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "api_key": {
            "type": "string",
            "description": "Only returned once. Store server-side."
          },
          "scopes": {
            "type": "array",
            "items": {
              "type": "string"
            }
          }
        },
        "required": [
          "id",
          "api_key",
          "scopes"
        ]
      },
      "Connection": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "label": {
            "type": "string"
          },
          "status": {
            "type": "string",
            "enum": [
              "provisioning",
              "provisioning_unknown",
              "open",
              "close",
              "connecting",
              "refused",
              "unknown"
            ]
          },
          "phone": {
            "type": "string",
            "nullable": true
          },
          "restricted": {
            "type": "boolean"
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          },
          "updated_at": {
            "type": "string",
            "format": "date-time"
          },
          "sending_policy": {
            "type": "object",
            "readOnly": true,
            "description": "Rolling reply/outreach policy. first_outreach_limit is the configured account value, not a fixed global cap.",
            "properties": {
              "reply_window_hours": {
                "type": "integer",
                "example": 24
              },
              "first_outreach_limit": {
                "type": "integer",
                "example": 10
              },
              "first_outreach_window_hours": {
                "type": "integer",
                "example": 24
              },
              "cold_pacing": {
                "type": "object",
                "properties": {
                  "per_minute": {
                    "type": "integer",
                    "example": 5
                  },
                  "per_10_minutes": {
                    "type": "integer",
                    "example": 20
                  },
                  "per_hour": {
                    "type": "integer",
                    "example": 60
                  }
                }
              }
            }
          }
        },
        "required": [
          "id",
          "label",
          "status",
          "phone",
          "restricted",
          "created_at",
          "updated_at"
        ]
      },
      "Pairing": {
        "type": "object",
        "properties": {
          "connection_id": {
            "type": "string",
            "format": "uuid"
          },
          "status": {
            "type": "string"
          },
          "qr": {
            "type": "string",
            "description": "Base64 QR image; never expose publicly. May be omitted when already connected.",
            "nullable": true
          },
          "expires_at": {
            "type": "string",
            "format": "date-time",
            "nullable": true
          }
        },
        "required": [
          "connection_id",
          "status"
        ]
      },
      "MediaUpload": {
        "type": "object",
        "properties": {
          "base64": {
            "type": "string",
            "maxLength": 10485760,
            "description": "Canonical base64, decoded size at most 7 MiB. No URLs."
          },
          "mime_type": {
            "type": "string",
            "enum": [
              "image/png",
              "image/jpeg",
              "video/mp4",
              "application/pdf"
            ]
          },
          "file_name": {
            "type": "string",
            "maxLength": 100
          }
        },
        "required": [
          "base64",
          "mime_type",
          "file_name"
        ],
        "additionalProperties": false
      },
      "TextContent": {
        "type": "object",
        "properties": {
          "type": {
            "type": "string",
            "enum": [
              "text"
            ]
          },
          "text": {
            "type": "string",
            "minLength": 1,
            "maxLength": 4096
          }
        },
        "required": [
          "type",
          "text"
        ],
        "additionalProperties": false
      },
      "MediaContent": {
        "type": "object",
        "properties": {
          "type": {
            "type": "string",
            "enum": [
              "image",
              "video",
              "document"
            ]
          },
          "text": {
            "type": "string",
            "maxLength": 4096
          },
          "media": {
            "$ref": "#/components/schemas/MediaUpload"
          }
        },
        "required": [
          "type",
          "media"
        ],
        "additionalProperties": false
      },
      "MessageInput": {
        "type": "object",
        "properties": {
          "connection_id": {
            "type": "string",
            "format": "uuid"
          },
          "to": {
            "type": "string",
            "pattern": "^\\+[1-9]\\d{7,14}$"
          },
          "conversation_id": {
            "type": "string",
            "format": "uuid"
          },
          "content": {
            "oneOf": [
              {
                "$ref": "#/components/schemas/TextContent"
              },
              {
                "$ref": "#/components/schemas/MediaContent"
              }
            ]
          }
        },
        "required": [
          "connection_id",
          "content"
        ],
        "additionalProperties": false,
        "oneOf": [
          {
            "required": [
              "to"
            ],
            "not": {
              "required": [
                "conversation_id"
              ]
            }
          },
          {
            "required": [
              "conversation_id"
            ],
            "not": {
              "required": [
                "to"
              ]
            }
          }
        ]
      },
      "Content": {
        "type": "object",
        "properties": {
          "text": {
            "type": "string"
          },
          "media": {
            "type": "object",
            "properties": {
              "mime_type": {
                "type": "string",
                "nullable": true
              },
              "file_name": {
                "type": "string",
                "nullable": true
              },
              "available": {
                "type": "boolean"
              }
            },
            "required": [
              "mime_type",
              "file_name",
              "available"
            ]
          }
        },
        "required": [
          "text"
        ]
      },
      "Message": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "connection_id": {
            "type": "string",
            "format": "uuid"
          },
          "conversation_id": {
            "type": "string",
            "format": "uuid"
          },
          "direction": {
            "type": "string",
            "enum": [
              "inbound",
              "outbound"
            ]
          },
          "type": {
            "type": "string"
          },
          "content": {
            "$ref": "#/components/schemas/Content"
          },
          "status": {
            "type": "string",
            "enum": [
              "sending",
              "accepted",
              "delivered",
              "read",
              "received",
              "failed",
              "uncertain",
              "queued",
              "cancelled"
            ]
          },
          "error_code": {
            "type": "string",
            "nullable": true,
            "description": "Queue terminal errors include queue_expired, queued_authorization_revoked, queue_payload_unreadable, connection_identity_changed, cancelled_by_user, connection_disconnected, or the current send guard error. Delivery uncertainty requires review and is never automatically retried."
          },
          "source": {
            "type": "string",
            "enum": [
              "api",
              "whatsapp_device"
            ]
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          },
          "updated_at": {
            "type": "string",
            "format": "date-time"
          },
          "translation": {
            "allOf": [
              {
                "$ref": "#/components/schemas/Translation"
              }
            ],
            "nullable": true,
            "description": "Only present with translation_language. Null if no cached translation. GET never translates."
          },
          "queued_at": {
            "type": "string",
            "format": "date-time",
            "description": "Present only for a message that was queued."
          },
          "scheduled_at": {
            "type": "string",
            "format": "date-time",
            "nullable": true,
            "description": "Earliest next eligibility check; may move as rolling limits change. Null after dispatch or cancellation. No guaranteed delivery time."
          },
          "expires_at": {
            "type": "string",
            "format": "date-time",
            "description": "Maximum waiting deadline (24 hours after enqueue). Expiry sets status failed and error_code queue_expired."
          },
          "queue_reason": {
            "type": "string",
            "nullable": true,
            "enum": [
              "first_outreach_limit",
              "line_rate_limit",
              "conversation_order",
              null
            ],
            "readOnly": true,
            "description": "Reason for automatic waiting. Null once dispatch begins."
          }
        },
        "required": [
          "id",
          "connection_id",
          "conversation_id",
          "direction",
          "type",
          "content",
          "status",
          "error_code",
          "source",
          "created_at",
          "updated_at"
        ]
      },
      "Conversation": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "connection_id": {
            "type": "string",
            "format": "uuid"
          },
          "phone": {
            "type": "string",
            "description": "null for unresolved LID; never replaced with the sending line phone.",
            "nullable": true
          },
          "last_message_at": {
            "type": "string",
            "format": "date-time"
          }
        },
        "required": [
          "id",
          "connection_id",
          "phone",
          "last_message_at"
        ]
      },
      "Event": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "type": {
            "type": "string"
          },
          "data": {
            "type": "object",
            "additionalProperties": true
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          }
        },
        "required": [
          "id",
          "type",
          "data",
          "created_at"
        ]
      },
      "Webhook": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "url": {
            "type": "string",
            "format": "uri"
          },
          "active": {
            "type": "boolean"
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          }
        },
        "required": [
          "id",
          "url",
          "active",
          "created_at"
        ]
      },
      "WebhookCreated": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "url": {
            "type": "string",
            "format": "uri"
          },
          "signing_secret": {
            "type": "string",
            "description": "Shown once; verify HMAC of timestamp + dot + raw body."
          }
        },
        "required": [
          "id",
          "url",
          "signing_secret"
        ]
      },
      "Translation": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "message_id": {
            "type": "string",
            "format": "uuid"
          },
          "target_language": {
            "type": "string"
          },
          "source_language": {
            "type": "string",
            "nullable": true
          },
          "status": {
            "type": "string",
            "enum": [
              "pending",
              "processing",
              "completed",
              "skipped",
              "failed"
            ]
          },
          "text": {
            "type": "string",
            "nullable": true
          },
          "error_code": {
            "type": "string",
            "nullable": true
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          },
          "updated_at": {
            "type": "string",
            "format": "date-time"
          }
        },
        "required": [
          "id",
          "message_id",
          "target_language",
          "status",
          "text"
        ]
      },
      "TranslationSettings": {
        "type": "object",
        "properties": {
          "enabled": {
            "type": "boolean",
            "default": false
          },
          "available": {
            "type": "boolean"
          },
          "target_language": {
            "type": "string",
            "default": "tr"
          },
          "daily_limit": {
            "type": "integer",
            "default": 500
          },
          "languages": {
            "type": "object",
            "additionalProperties": {
              "type": "string"
            }
          },
          "updated_at": {
            "type": "string",
            "nullable": true,
            "format": "date-time"
          }
        }
      }
    }
  }
}
