{
  "openapi": "3.1.0",
  "info": {
    "title": "Chat Live Developer API",
    "version": "1.0.0",
    "description": "Admin-issued scoped bearer keys. Server-to-server only; no unrestricted browser CORS or cross-origin WebSocket. API credentials and signing secrets are not self-service."
  },
  "servers": [
    {
      "url": "https://chat.egytag.com"
    }
  ],
  "security": [
    {
      "bearerAuth": []
    }
  ],
  "tags": [
    {
      "name": "Rooms"
    },
    {
      "name": "Messages"
    },
    {
      "name": "Webhooks"
    }
  ],
  "paths": {
    "/api/developer/v1/rooms": {
      "get": {
        "tags": [
          "Rooms"
        ],
        "summary": "List rooms in the API key allowlist",
        "x-required-scopes": [
          "rooms:read"
        ],
        "responses": {
          "200": {
            "description": "Allowed public rooms",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean",
                      "const": true
                    },
                    "rooms": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Room"
                      }
                    }
                  },
                  "required": [
                    "ok",
                    "rooms"
                  ]
                }
              }
            }
          },
          "401": {
            "description": "Missing, expired or revoked bearer token",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Insufficient scope or origin not allowed"
          },
          "404": {
            "description": "Room not found or not in token allowlist"
          },
          "429": {
            "description": "Rate limited"
          }
        }
      }
    },
    "/api/developer/v1/rooms/{slug}/messages": {
      "parameters": [
        {
          "name": "slug",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string",
            "pattern": "^[a-z0-9-]{1,80}$"
          },
          "description": "An allowed public room slug"
        }
      ],
      "get": {
        "tags": [
          "Messages"
        ],
        "summary": "Read recent messages from an allowed room",
        "x-required-scopes": [
          "messages:read"
        ],
        "parameters": [
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 50,
              "default": 20
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Ordered message list",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean",
                      "const": true
                    },
                    "messages": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Message"
                      }
                    }
                  },
                  "required": [
                    "ok",
                    "messages"
                  ]
                }
              }
            }
          },
          "401": {
            "description": "Missing, expired or revoked bearer token",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Insufficient scope or origin not allowed"
          },
          "404": {
            "description": "Room not found or not in token allowlist"
          },
          "429": {
            "description": "Rate limited"
          }
        }
      },
      "post": {
        "tags": [
          "Messages"
        ],
        "summary": "Publish an integration-labelled message",
        "x-required-scopes": [
          "messages:write"
        ],
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string",
              "minLength": 8,
              "maxLength": 100
            },
            "description": "Unique request ID retained for 30 days"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "text": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 2000
                  }
                },
                "required": [
                  "text"
                ],
                "additionalProperties": false
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "New message created",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    },
                    "room": {
                      "type": "string"
                    },
                    "id": {
                      "type": "integer"
                    },
                    "replayed": {
                      "type": "boolean"
                    }
                  }
                }
              }
            }
          },
          "200": {
            "description": "Prior message returned for the same idempotency key"
          },
          "409": {
            "description": "Idempotency key reused with different text"
          },
          "401": {
            "description": "Missing, expired or revoked bearer token",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Insufficient scope or origin not allowed"
          },
          "404": {
            "description": "Room not found or not in token allowlist"
          },
          "429": {
            "description": "Rate limited"
          }
        }
      }
    },
    "/api/developer/v1/webhooks": {
      "get": {
        "tags": [
          "Webhooks"
        ],
        "summary": "List webhooks owned by this API key",
        "x-required-scopes": [
          "webhooks:manage"
        ],
        "responses": {
          "200": {
            "description": "Scoped webhooks",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean",
                      "const": true
                    },
                    "webhooks": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Webhook"
                      }
                    }
                  },
                  "required": [
                    "ok",
                    "webhooks"
                  ]
                }
              }
            }
          },
          "401": {
            "description": "Missing, expired or revoked bearer token",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Insufficient scope or origin not allowed"
          },
          "404": {
            "description": "Room not found or not in token allowlist"
          },
          "429": {
            "description": "Rate limited"
          }
        }
      },
      "post": {
        "tags": [
          "Webhooks"
        ],
        "summary": "Create a signed outbound webhook (secret displayed once)",
        "x-required-scopes": [
          "webhooks:manage"
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "url": {
                    "type": "string",
                    "format": "uri",
                    "description": "Public HTTPS destination on port 443"
                  },
                  "events": {
                    "type": "array",
                    "minItems": 1,
                    "uniqueItems": true,
                    "items": {
                      "type": "string",
                      "enum": [
                        "message.created",
                        "room.presence"
                      ]
                    }
                  }
                },
                "required": [
                  "url",
                  "events"
                ]
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Webhook created: secret is returned only in this response",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    },
                    "webhook": {
                      "$ref": "#/components/schemas/Webhook"
                    },
                    "secret": {
                      "type": "string",
                      "description": "HMAC-SHA256 secret; store in a secret manager"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Invalid webhook options or missing read scope"
          },
          "401": {
            "description": "Missing/invalid bearer token"
          },
          "403": {
            "description": "Insufficient scope"
          }
        }
      }
    },
    "/api/developer/v1/webhooks/{id}": {
      "parameters": [
        {
          "name": "id",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string",
            "pattern": "^[a-f0-9]{24}$"
          }
        }
      ],
      "delete": {
        "tags": [
          "Webhooks"
        ],
        "summary": "Disable a webhook for this key",
        "x-required-scopes": [
          "webhooks:manage"
        ],
        "responses": {
          "200": {
            "description": "Webhook disabled or already inactive"
          },
          "401": {
            "description": "Invalid bearer token"
          },
          "403": {
            "description": "Insufficient scope"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "Opaque API key"
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "properties": {
          "ok": {
            "type": "boolean",
            "const": false
          },
          "error": {
            "type": "string"
          }
        },
        "required": [
          "error"
        ]
      },
      "Room": {
        "type": "object",
        "properties": {
          "slug": {
            "type": "string"
          },
          "title": {
            "type": "string"
          },
          "description": {
            "type": "string"
          },
          "online": {
            "type": "integer"
          },
          "category": {
            "type": "string"
          }
        },
        "required": [
          "slug",
          "title",
          "online"
        ]
      },
      "Message": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "room": {
            "type": "string"
          },
          "nickname": {
            "type": "string"
          },
          "kind": {
            "type": "string"
          },
          "body": {
            "type": "string"
          },
          "created": {
            "type": "integer",
            "description": "Unix timestamp in milliseconds"
          }
        },
        "required": [
          "id",
          "room",
          "nickname",
          "kind",
          "body",
          "created"
        ]
      },
      "Webhook": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "keyId": {
            "type": "string"
          },
          "url": {
            "type": "string",
            "format": "uri"
          },
          "events": {
            "type": "array",
            "items": {
              "type": "string",
              "enum": [
                "message.created",
                "room.presence"
              ]
            }
          },
          "createdAt": {
            "type": "integer"
          }
        }
      }
    }
  },
  "webhooks": {
    "chatLiveEvent": {
      "post": {
        "description": "Outgoing signed event. Verify X-ChatLive-Timestamp + raw body with HMAC-SHA256. Respond 2xx quickly. Up to 5 attempts.",
        "parameters": [
          {
            "in": "header",
            "name": "X-ChatLive-Timestamp",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "in": "header",
            "name": "X-ChatLive-Signature",
            "required": true,
            "schema": {
              "type": "string",
              "pattern": "^sha256=[a-f0-9]{64}$"
            }
          },
          {
            "in": "header",
            "name": "X-ChatLive-Event",
            "required": true,
            "schema": {
              "type": "string",
              "enum": [
                "message.created",
                "room.presence"
              ]
            }
          },
          {
            "in": "header",
            "name": "X-ChatLive-Delivery",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "id": {
                    "type": "string"
                  },
                  "event": {
                    "type": "string"
                  },
                  "occurredAt": {
                    "type": "string",
                    "format": "date-time"
                  },
                  "data": {
                    "type": "object"
                  }
                },
                "required": [
                  "id",
                  "event",
                  "occurredAt",
                  "data"
                ]
              }
            }
          }
        },
        "responses": {
          "204": {
            "description": "Accepted"
          }
        }
      }
    }
  }
}
