{
  "openapi": "3.1.0",
  "info": {
    "title": "Switchline API",
    "version": "1.0.0",
    "description": "Create a workspace credential with access to individual connections. Bearer credentials are shown once. All Unix timestamps use seconds."
  },
  "servers": [
    {
      "url": "/v1"
    }
  ],
  "security": [
    {
      "bearer": []
    }
  ],
  "paths": {
    "/connections/{connection}/consent": {
      "parameters": [
        {
          "in": "path",
          "name": "connection",
          "required": true,
          "schema": { "type": "string" }
        }
      ],
      "post": {
        "operationId": "updateConsent",
        "description": "Requires consents:write on the connection and documented WhatsApp opt-in for grants. Set withdraw to true to remove consent for this connection. A workspace opt-out cannot be overridden here.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["to"],
                "properties": {
                  "to": { "type": "string", "pattern": "^\\+[1-9]\\d{6,14}$" },
                  "source": {
                    "type": "string",
                    "maxLength": 240,
                    "description": "Where permission was granted. Required when recording consent."
                  },
                  "withdraw": { "type": "boolean", "default": false }
                }
              }
            }
          }
        },
        "responses": {
          "200": { "description": "Consent updated" },
          "400": {
            "description": "Invalid recipient or missing permission source"
          },
          "403": { "description": "Credential lacks consents:write scope" },
          "404": { "description": "Connection unavailable to the credential" },
          "409": { "description": "Recipient previously opted out" }
        }
      }
    },
    "/connections/{connection}/availability": {
      "parameters": [
        {
          "in": "path",
          "name": "connection",
          "required": true,
          "schema": { "type": "string" }
        }
      ],
      "get": {
        "operationId": "getSenderAvailability",
        "description": "Requires connections:read on the connection. Returns the current sending hold and estimated next eligibility in Unix seconds. Capacity can change as other sends are queued.",
        "responses": {
          "200": {
            "description": "Current sending availability",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "connection_id": { "type": "string" },
                    "status": { "type": "string" },
                    "state": {
                      "enum": [
                        "ready",
                        "paced",
                        "paused",
                        "offline",
                        "busy",
                        "review_required"
                      ]
                    },
                    "reason": { "type": ["string", "null"] },
                    "next_eligible_at": { "type": ["integer", "null"] },
                    "timezone": { "type": ["string", "null"] },
                    "daily_limit": { "type": ["integer", "null"] },
                    "used_today": { "type": ["integer", "null"] },
                    "remaining_today": { "type": ["integer", "null"] }
                  }
                }
              }
            }
          },
          "403": { "description": "Credential lacks connections:read scope" },
          "404": { "description": "Connection unavailable to the credential" }
        }
      }
    },
    "/connections/{connection}/events": {
      "parameters": [
        {
          "in": "path",
          "name": "connection",
          "required": true,
          "schema": { "type": "string" }
        },
        {
          "in": "query",
          "name": "after",
          "schema": { "type": "integer", "minimum": 0, "default": 0 }
        },
        {
          "in": "query",
          "name": "limit",
          "schema": {
            "type": "integer",
            "minimum": 1,
            "maximum": 100,
            "default": 50
          }
        }
      ],
      "get": {
        "operationId": "replayConnectionEvents",
        "description": "Requires events:read on the connection. Recover up to seven days of events in ingest order. Supply the returned cursor as after and deduplicate using the event ID if webhooks are also enabled.",
        "responses": {
          "200": {
            "description": "Ordered event page and resume cursor",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "events": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "id": { "type": "string" },
                          "type": {
                            "enum": [
                              "connection.status",
                              "message.received",
                              "message.status"
                            ]
                          },
                          "connection_id": { "type": "string" },
                          "timestamp": { "type": "integer" },
                          "cursor": { "type": "integer" },
                          "data": { "type": "object" }
                        }
                      }
                    },
                    "cursor": { "type": "integer" },
                    "has_more": { "type": "boolean" },
                    "retention_days": { "type": "integer" }
                  }
                }
              }
            }
          },
          "400": { "description": "Invalid cursor or limit" },
          "403": { "description": "Credential lacks events:read scope" },
          "404": { "description": "Connection unavailable to the credential" }
        }
      }
    },
    "/connections": {
      "get": {
        "operationId": "listConnections",
        "description": "Requires connections:read. Status and observed are the last engine observation.",
        "responses": {
          "200": {
            "description": "Authorized connections",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/Connection"
                  }
                }
              }
            }
          },
          "400": {
            "description": "Invalid input",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Invalid credential",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Insufficient scope or consent",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Resource unavailable",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Connection offline or idempotency conflict",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "413": {
            "description": "Body too large",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "415": {
            "description": "Unsupported content type",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Rate or storage limit reached",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "503": {
            "description": "Service unavailable",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/connections/{connection}/messages": {
      "post": {
        "operationId": "sendMessage",
        "description": "Requires messages:send, active recipient consent and an online paired number. Retry the same request with the same idempotency key. A successful HTTP response indicates queue acceptance. Queued messages respect sender limits and have a 45-day queue deadline.",
        "parameters": [
          {
            "name": "connection",
            "in": "path",
            "required": true,
            "description": "Connection ID issued by Switchline",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": true,
            "description": "Unique per connection, at most 128 characters",
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Message"
              }
            }
          }
        },
        "responses": {
          "202": {
            "description": "Accepted operation",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Accepted"
                }
              }
            }
          },
          "400": {
            "description": "Invalid input",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Invalid credential",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Insufficient scope or consent",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Resource unavailable",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Connection offline or idempotency conflict",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "413": {
            "description": "Body too large",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "415": {
            "description": "Unsupported content type",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Rate or storage limit reached",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "503": {
            "description": "Service unavailable",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/connections/{connection}/media": {
      "post": {
        "operationId": "uploadMedia",
        "description": "Requires media:write. Upload up to 4 MiB. Media belongs to this connection and expires after seven days.",
        "parameters": [
          {
            "name": "connection",
            "in": "path",
            "required": true,
            "description": "Connection ID issued by Switchline",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "X-Filename",
            "in": "header",
            "required": false,
            "description": "Display filename",
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "image/jpeg": {
              "schema": {
                "type": "string",
                "format": "binary"
              }
            },
            "image/png": {
              "schema": {
                "type": "string",
                "format": "binary"
              }
            },
            "image/webp": {
              "schema": {
                "type": "string",
                "format": "binary"
              }
            },
            "video/mp4": {
              "schema": {
                "type": "string",
                "format": "binary"
              }
            },
            "audio/ogg": {
              "schema": {
                "type": "string",
                "format": "binary"
              }
            },
            "audio/mpeg": {
              "schema": {
                "type": "string",
                "format": "binary"
              }
            },
            "application/pdf": {
              "schema": {
                "type": "string",
                "format": "binary"
              }
            },
            "text/plain": {
              "schema": {
                "type": "string",
                "format": "binary"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Stored media",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "id": {
                      "type": "string"
                    },
                    "type": {
                      "type": "string"
                    },
                    "size": {
                      "type": "integer"
                    }
                  },
                  "required": []
                }
              }
            }
          },
          "400": {
            "description": "Invalid input",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Invalid credential",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Insufficient scope or consent",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Resource unavailable",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Connection offline or idempotency conflict",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "413": {
            "description": "Body too large",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "415": {
            "description": "Unsupported content type",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Rate or storage limit reached",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "503": {
            "description": "Service unavailable",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/operations/{id}": {
      "get": {
        "operationId": "getOperation",
        "description": "Requires messages:send and a grant on the original connection. An unknown outcome needs reconciliation before resending.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Operation ID",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Operation status",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Operation"
                }
              }
            }
          },
          "400": {
            "description": "Invalid input",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Invalid credential",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Insufficient scope or consent",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Resource unavailable",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Connection offline or idempotency conflict",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "413": {
            "description": "Body too large",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "415": {
            "description": "Unsupported content type",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Rate or storage limit reached",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "503": {
            "description": "Service unavailable",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearer": {
        "type": "http",
        "scheme": "bearer"
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "string"
          }
        },
        "required": ["error"]
      },
      "Connection": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "status": {
            "type": "string"
          },
          "observed": {
            "type": "integer"
          }
        },
        "required": []
      },
      "Accepted": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "state": {
            "type": "string"
          },
          "expires_at": {
            "type": ["integer", "null"],
            "description": "Queue deadline in Unix seconds. Present for message sends."
          }
        },
        "required": []
      },
      "Operation": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "connection_id": {
            "type": "string"
          },
          "operation": {
            "type": "string"
          },
          "state": {
            "enum": [
              "queued",
              "running",
              "done",
              "failed",
              "unknown",
              "expired"
            ]
          },
          "updated": {
            "type": "integer"
          },
          "expires_at": {
            "type": "integer",
            "description": "Queue deadline in Unix seconds for a message send."
          },
          "result": {
            "type": ["object", "null"],
            "properties": {
              "message_id": {
                "type": "string"
              },
              "error": {
                "type": "string"
              }
            }
          }
        },
        "required": []
      },
      "Message": {
        "type": "object",
        "properties": {
          "to": {
            "type": "string",
            "pattern": "^\\+[1-9]\\d{6,14}$"
          },
          "type": {
            "enum": ["text", "image", "audio", "video", "document"],
            "default": "text"
          },
          "text": {
            "type": "string",
            "maxLength": 4096,
            "description": "Required for text messages"
          },
          "media_id": {
            "type": "string",
            "description": "Required for media messages"
          },
          "caption": {
            "type": "string",
            "maxLength": 1024
          },
          "reply_to": {
            "type": "string"
          }
        },
        "required": ["to"]
      }
    }
  }
}
