{
  "openapi": "3.1.0",
  "info": {
    "title": "Owlbee public widget API",
    "version": "1.0.0",
    "summary": "Chat with one Owlbee chatbot by its public embed code.",
    "description": "The public embed/widget API: the endpoints the Owlbee chat widget calls from a customer's website to talk to one chatbot.\n\n- **This is not an account API.** Creating chatbots and managing billing happen only in the Owlbee web app, signed in as the account owner. An owner can read conversations and analytics and make some changes through the account MCP server, with an API key: https://owlbee.ai/docs/mcp-account.md\n\n- **There is no authentication on these paths.** No API keys, tokens or OAuth. The only \"key\" is a chatbot's embed code, which is public: it sits in the HTML of every website that embeds the bot. It is not a secret and grants nothing beyond chatting with that one bot.\n\n- **Every reply is billed to the bot's owner** (their monthly AI token allowance), so be considerate with automated traffic.\n\n- **CORS:** every path under `/api/` accepts cross-origin requests from any origin; the `Access-Control-Allow-Origin` header echoes the caller's `Origin`.\n\nThe human-readable guide, with the booking flow step by step, is https://owlbee.ai/docs/chat-api.md",
    "contact": {
      "name": "Owlbee support",
      "email": "support@owlbee.ai",
      "url": "https://owlbee.ai/contact"
    },
    "termsOfService": "https://owlbee.ai/terms"
  },
  "externalDocs": {
    "description": "The public widget chat API guide",
    "url": "https://owlbee.ai/docs/chat-api.md"
  },
  "servers": [
    {
      "url": "https://owlbee.ai"
    }
  ],
  "security": [],
  "tags": [
    {
      "name": "Chat",
      "description": "Talk to a chatbot, including booking a call."
    },
    {
      "name": "Usage",
      "description": "Record where a chatbot is embedded."
    },
    {
      "name": "Status",
      "description": "Service health."
    }
  ],
  "paths": {
    "/api/chat/{embedCode}": {
      "post": {
        "operationId": "sendChatMessage",
        "tags": [
          "Chat"
        ],
        "summary": "Send a chat turn or a widget/booking action to one chatbot",
        "description": "One endpoint for chat, the widget handshake (widget_config, widget_hello), closing a conversation (end_conversation) and the booking flow (schedule_*). The monthly token limit and an ended trial are not errors: they return 200 with limit_reached or trial_expired set.",
        "parameters": [
          {
            "name": "embedCode",
            "in": "path",
            "required": true,
            "description": "The chatbot's embed code, shown under Get Embed Code on the chatbot's page. It is public (it sits in the HTML of every site that embeds the bot) and is not a secret.",
            "schema": {
              "type": "string",
              "format": "uuid",
              "maxLength": 36
            },
            "example": "0b6f3c1e-1111-2222-3333-444455556666"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ChatRequest"
              },
              "examples": {
                "chat": {
                  "value": {
                    "message": "What are your opening hours?",
                    "ui": [
                      "calendar"
                    ]
                  }
                },
                "startBooking": {
                  "value": {
                    "action": "schedule_start",
                    "conversation_id": "5f0c2a9e-0000-4000-8000-000000000000",
                    "ui": [
                      "calendar"
                    ]
                  }
                },
                "close": {
                  "value": {
                    "action": "end_conversation",
                    "conversation_id": "5f0c2a9e-0000-4000-8000-000000000000",
                    "resolved": true
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "A chat or booking turn (ChatReply), or the reply to widget_config, widget_hello or end_conversation.",
            "content": {
              "application/json": {
                "schema": {
                  "anyOf": [
                    {
                      "$ref": "#/components/schemas/ChatReply"
                    },
                    {
                      "$ref": "#/components/schemas/WidgetConfigReply"
                    },
                    {
                      "$ref": "#/components/schemas/WidgetHelloReply"
                    },
                    {
                      "$ref": "#/components/schemas/EndConversationReply"
                    }
                  ]
                }
              }
            }
          },
          "400": {
            "description": "Empty JSON body, end_conversation without conversation_id, no message on a plain chat turn, or a bot that is untrained with an empty system prompt.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                },
                "examples": {
                  "e1": {
                    "value": {
                      "error": "No data received"
                    }
                  },
                  "e2": {
                    "value": {
                      "error": "Conversation ID required for end conversation action"
                    }
                  },
                  "e3": {
                    "value": {
                      "error": "Message is required"
                    }
                  },
                  "e4": {
                    "value": {
                      "error": "Chatbot is not trained yet. Please upload documents and train the chatbot first, or set a system prompt."
                    }
                  }
                }
              }
            }
          },
          "404": {
            "description": "No chatbot has this embed code.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                },
                "examples": {
                  "e1": {
                    "value": {
                      "error": "Chatbot not found. Please check the embed code."
                    }
                  }
                }
              }
            }
          },
          "500": {
            "description": "Anything else that fails, including a body that is not JSON or not sent as application/json.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                },
                "examples": {
                  "e1": {
                    "value": {
                      "error": "Sorry, I encountered an error. Please try again."
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/track-usage/{embedCode}": {
      "post": {
        "operationId": "trackUsage",
        "tags": [
          "Usage"
        ],
        "summary": "Record a website that embeds the chatbot",
        "description": "Adds the domain of website_url (or of the Referer header when the JSON body has no website_url) to the chatbot's Used On list; localhost is skipped. Chat requests that send a Referer are tracked the same way automatically, so the widget never needs this.",
        "parameters": [
          {
            "name": "embedCode",
            "in": "path",
            "required": true,
            "description": "The chatbot's embed code, shown under Get Embed Code on the chatbot's page. It is public (it sits in the HTML of every site that embeds the bot) and is not a secret.",
            "schema": {
              "type": "string",
              "format": "uuid",
              "maxLength": 36
            },
            "example": "0b6f3c1e-1111-2222-3333-444455556666"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/TrackUsageRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Tracked.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/TrackUsageReply"
                }
              }
            }
          },
          "400": {
            "description": "No website_url and no Referer.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                },
                "examples": {
                  "e1": {
                    "value": {
                      "error": "No website URL provided"
                    }
                  }
                }
              }
            }
          },
          "404": {
            "description": "No chatbot has this embed code.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                },
                "examples": {
                  "e1": {
                    "value": {
                      "error": "Chatbot not found"
                    }
                  }
                }
              }
            }
          },
          "500": {
            "description": "Anything else that fails, including a body that is not sent as application/json.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/health": {
      "get": {
        "operationId": "getHealth",
        "tags": [
          "Status"
        ],
        "summary": "Service health",
        "description": "Always 200 while the service is up; database says whether it is reachable.",
        "responses": {
          "200": {
            "description": "The service is up.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Health"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "ChatRequest": {
        "type": "object",
        "description": "Every field is optional on its own, but a plain chat turn needs \"message\". An empty object or null is rejected with 400.",
        "properties": {
          "message": {
            "type": "string",
            "description": "What the visitor typed (trimmed). While a booking is in progress it answers the current booking step instead of going to the model."
          },
          "conversation_id": {
            "type": "string",
            "description": "The id from a previous response; send it on every later turn. Omit it on the first turn and the server creates one."
          },
          "action": {
            "type": "string",
            "enum": [
              "widget_config",
              "widget_hello",
              "end_conversation",
              "schedule_start",
              "schedule_slot",
              "schedule_details",
              "schedule_confirm",
              "schedule_change_time",
              "schedule_abort"
            ],
            "description": "Omit for a plain chat turn. The booking actions after schedule_start only apply while a booking is in progress for this conversation_id; otherwise the turn is handled as ordinary chat."
          },
          "ui": {
            "type": "array",
            "items": {
              "type": "string"
            },
            "description": "What the client can draw. [\"calendar\"] means it can render the booking calendar card; without it the time step offers slot buttons instead."
          },
          "value": {
            "type": "string",
            "description": "With schedule_slot: the value of the chosen slot."
          },
          "values": {
            "type": "object",
            "description": "With schedule_details: the submitted contact form.",
            "properties": {
              "name": {
                "type": "string"
              },
              "email": {
                "type": "string"
              },
              "phone": {
                "type": "string"
              }
            }
          },
          "resolved": {
            "type": "boolean",
            "default": false,
            "description": "With end_conversation: whether the visitor's question was answered."
          }
        }
      },
      "ChatReply": {
        "type": "object",
        "description": "A chat or booking turn.",
        "required": [
          "response",
          "response_html",
          "conversation_id",
          "actions"
        ],
        "properties": {
          "response": {
            "type": "string",
            "description": "The reply as plain text."
          },
          "response_html": {
            "type": "string",
            "description": "The same reply as sanitized HTML (a, b, strong, i, em, u, br, p, ul, ol, li, code, span; class, title and href attributes; http(s), mailto and tel links only)."
          },
          "conversation_id": {
            "type": "string"
          },
          "actions": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Action"
            },
            "description": "Buttons and cards to show under the reply."
          },
          "booking": {
            "$ref": "#/components/schemas/BookingState"
          },
          "limit_reached": {
            "type": "boolean",
            "const": true,
            "description": "Present only when the owner has used this month's AI token allowance. The response is a notice, not an answer; booking still works."
          },
          "trial_expired": {
            "type": "boolean",
            "const": true,
            "description": "Present only when the owner's free trial has ended. The bot is off, booking too."
          }
        }
      },
      "WidgetConfigReply": {
        "type": "object",
        "description": "The reply to action widget_config.",
        "required": [
          "open_by_default"
        ],
        "properties": {
          "open_by_default": {
            "type": "boolean",
            "description": "True when the owner wants the chat window opened automatically and the bot can answer."
          }
        },
        "additionalProperties": false
      },
      "WidgetHelloReply": {
        "type": "object",
        "description": "The reply to action widget_hello. The greeting is written by the widget; actions holds the \"Book a call\" button when the bot offers booking.",
        "required": [
          "conversation_id",
          "actions"
        ],
        "properties": {
          "conversation_id": {
            "type": "string"
          },
          "actions": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Button"
            }
          }
        },
        "additionalProperties": false
      },
      "EndConversationReply": {
        "type": "object",
        "description": "The reply to action end_conversation.",
        "required": [
          "success",
          "message",
          "updated",
          "conversation_id"
        ],
        "properties": {
          "success": {
            "type": "boolean",
            "const": true
          },
          "message": {
            "type": "string",
            "example": "Conversation status updated"
          },
          "updated": {
            "type": "integer",
            "minimum": 0,
            "description": "How many stored messages of that conversation were marked."
          },
          "conversation_id": {
            "type": "string"
          }
        },
        "additionalProperties": false
      },
      "BookingState": {
        "type": "object",
        "description": "Only on booking turns. While active is true the next typed message answers the booking step.",
        "required": [
          "active",
          "step"
        ],
        "properties": {
          "active": {
            "type": "boolean"
          },
          "step": {
            "type": [
              "string",
              "null"
            ],
            "enum": [
              "time",
              "details",
              "confirm",
              "done",
              null
            ]
          }
        }
      },
      "Action": {
        "anyOf": [
          {
            "$ref": "#/components/schemas/Button"
          },
          {
            "$ref": "#/components/schemas/CalendarCard"
          },
          {
            "$ref": "#/components/schemas/FormCard"
          }
        ]
      },
      "Button": {
        "type": "object",
        "description": "A button. Send its id as \"action\" (and its value, if any, as \"value\") when pressed.",
        "required": [
          "id",
          "label"
        ],
        "properties": {
          "id": {
            "type": "string",
            "enum": [
              "widget_config",
              "widget_hello",
              "end_conversation",
              "schedule_start",
              "schedule_slot",
              "schedule_details",
              "schedule_confirm",
              "schedule_change_time",
              "schedule_abort"
            ]
          },
          "label": {
            "type": "string"
          },
          "style": {
            "type": "string",
            "enum": [
              "primary",
              "ghost"
            ],
            "description": "Absent on slot buttons."
          },
          "value": {
            "type": "string",
            "description": "Slot buttons only: the slot to send back with schedule_slot."
          }
        }
      },
      "CalendarCard": {
        "type": "object",
        "description": "The booking time step as a calendar, laid out in the business's timezone (Monday first). Send a slot's value with action schedule_slot.",
        "required": [
          "type",
          "id",
          "weeks",
          "secondary"
        ],
        "properties": {
          "type": {
            "const": "calendar"
          },
          "id": {
            "const": "schedule_slot"
          },
          "title": {
            "type": "string"
          },
          "subtitle": {
            "type": "string",
            "description": "The business's timezone."
          },
          "weekdays": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "weeks": {
            "type": "array",
            "items": {
              "type": "array",
              "minItems": 7,
              "maxItems": 7,
              "items": {
                "anyOf": [
                  {
                    "$ref": "#/components/schemas/CalendarDay"
                  },
                  {
                    "type": "null"
                  }
                ]
              },
              "description": "Seven days; null for a day outside the booking window."
            }
          },
          "emptyLabel": {
            "type": "string"
          },
          "secondary": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Button"
            }
          }
        }
      },
      "CalendarDay": {
        "type": "object",
        "required": [
          "date",
          "day",
          "month",
          "label",
          "slots"
        ],
        "properties": {
          "date": {
            "type": "string",
            "format": "date"
          },
          "day": {
            "type": "integer"
          },
          "month": {
            "type": "string"
          },
          "label": {
            "type": "string"
          },
          "slots": {
            "type": "array",
            "items": {
              "type": "object",
              "required": [
                "label",
                "value"
              ],
              "properties": {
                "label": {
                  "type": "string"
                },
                "value": {
                  "type": "string"
                }
              }
            }
          }
        }
      },
      "FormCard": {
        "type": "object",
        "description": "The contact form. Submit it with action schedule_details and the fields as \"values\".",
        "required": [
          "type",
          "id",
          "fields",
          "secondary"
        ],
        "properties": {
          "type": {
            "const": "form"
          },
          "id": {
            "const": "schedule_details"
          },
          "title": {
            "type": "string",
            "description": "The chosen time."
          },
          "subtitle": {
            "type": "string",
            "description": "The business's timezone."
          },
          "fields": {
            "type": "array",
            "items": {
              "type": "object",
              "required": [
                "name",
                "label",
                "type",
                "placeholder",
                "value",
                "error"
              ],
              "properties": {
                "name": {
                  "type": "string",
                  "enum": [
                    "name",
                    "email",
                    "phone"
                  ]
                },
                "label": {
                  "type": "string"
                },
                "type": {
                  "type": "string",
                  "enum": [
                    "text",
                    "email",
                    "tel"
                  ]
                },
                "placeholder": {
                  "type": "string"
                },
                "value": {
                  "type": "string"
                },
                "error": {
                  "type": [
                    "string",
                    "null"
                  ]
                }
              }
            }
          },
          "submitLabel": {
            "type": "string"
          },
          "secondary": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Button"
            }
          }
        }
      },
      "TrackUsageRequest": {
        "type": "object",
        "properties": {
          "website_url": {
            "type": "string",
            "format": "uri",
            "description": "The page the widget is on. Without it the Referer header is used."
          }
        }
      },
      "TrackUsageReply": {
        "type": "object",
        "required": [
          "success",
          "message"
        ],
        "properties": {
          "success": {
            "type": "boolean",
            "const": true
          },
          "message": {
            "type": "string",
            "example": "Usage tracked successfully"
          }
        }
      },
      "Health": {
        "type": "object",
        "required": [
          "status",
          "service",
          "database",
          "storage",
          "timestamp"
        ],
        "properties": {
          "status": {
            "type": "string",
            "const": "healthy"
          },
          "service": {
            "type": "string"
          },
          "database": {
            "type": "string",
            "enum": [
              "connected",
              "error"
            ]
          },
          "storage": {
            "type": "object",
            "description": "File storage configuration snapshot."
          },
          "timestamp": {
            "type": "string",
            "description": "UTC, ISO 8601 without an offset."
          }
        }
      },
      "Error": {
        "type": "object",
        "required": [
          "error"
        ],
        "properties": {
          "error": {
            "type": "string"
          },
          "debug_info": {
            "type": [
              "string",
              "null"
            ],
            "description": "Chat 500s only; null in production."
          }
        }
      }
    }
  }
}