{
  "openapi": "3.0.3",
  "info": {
    "title": "WebRTC Signaling Server API",
    "description": "A lightweight real-time peer-to-peer WebRTC signaling server powered by Cloudflare Workers and Durable Objects. Uses Server-Sent Events (SSE) for downstream messages and HTTP POST endpoints for outbound messages.",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "/",
      "description": "Current Host"
    }
  ],
  "paths": {
    "/api/connect": {
      "get": {
        "summary": "Connect to Room Event Stream (SSE)",
        "description": "Establishes a persistent Server-Sent Events connection to listen for roster changes, incoming signaling messages, and session authentication assignments. The connection is held open, emitting heartbeat keepalive events every 10 seconds.",
        "parameters": [
          {
            "name": "room",
            "in": "query",
            "required": true,
            "description": "The logical room name to connect to.",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "session_id",
            "in": "query",
            "required": false,
            "description": "An existing client session UUID for reconnection.",
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          },
          {
            "name": "token",
            "in": "query",
            "required": false,
            "description": "The secure session token associated with session_id. Required for reconnection.",
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          },
          {
            "name": "X-Session-Id",
            "in": "header",
            "required": false,
            "description": "An existing client session UUID for reconnection (alternative to query param).",
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          },
          {
            "name": "X-Token",
            "in": "header",
            "required": false,
            "description": "The secure session token associated with session_id (alternative to query param).",
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          },
          {
            "name": "Authorization",
            "in": "header",
            "required": false,
            "description": "The bearer session token (alternative format: Bearer <token>).",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Server-Sent Events connection successfully established.",
            "headers": {
              "Content-Type": {
                "schema": {
                  "type": "string",
                  "example": "text/event-stream"
                }
              },
              "Cache-Control": {
                "schema": {
                  "type": "string",
                  "example": "no-cache, no-transform"
                }
              },
              "Connection": {
                "schema": {
                  "type": "string",
                  "example": "keep-alive"
                }
              }
            }
          },
          "400": {
            "description": "Missing required room parameter.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/signal": {
      "post": {
        "summary": "Send Signaling Payload",
        "description": "Sends a WebRTC signaling payload (SDP offer/answer, ICE candidates, or custom signal packet) to a specific target peer in the room. The server validates the sender session token and forwards the signal to the recipient's SSE stream in-memory.",
        "parameters": [
          {
            "name": "room",
            "in": "query",
            "required": true,
            "description": "The logical room name.",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "X-Token",
            "in": "header",
            "required": false,
            "description": "Your private session token.",
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          },
          {
            "name": "Authorization",
            "in": "header",
            "required": false,
            "description": "Your private session token as a bearer token (format: Bearer <token>).",
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["sender", "to", "token", "signal"],
                "properties": {
                  "sender": {
                    "type": "string",
                    "format": "uuid",
                    "description": "Your UUID client ID."
                  },
                  "to": {
                    "type": "string",
                    "format": "uuid",
                    "description": "The recipient peer's UUID ID."
                  },
                  "token": {
                    "type": "string",
                    "format": "uuid",
                    "description": "Your private session token."
                  },
                  "signal": {
                    "type": "object",
                    "description": "WebRTC SDP / ICE Candidate or custom action object."
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Signal dispatched successfully.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "success": {
                      "type": "boolean",
                      "example": true
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Missing required body parameters.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "411": {
            "description": "Length Required: Content-Length header is missing.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string",
                      "example": "Length Required: Content-Length header is missing"
                    }
                  }
                }
              }
            }
          },
          "413": {
            "description": "Payload Too Large: request body exceeds 100KB limit.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string",
                      "example": "Payload Too Large: Content-Length exceeds 100KB limit"
                    }
                  }
                }
              }
            }
          },
          "403": {
            "description": "Unauthorized: invalid session token or session expired.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "429": {
            "description": "Too Many Requests: signaling rate limit exceeded.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string",
                      "example": "Too Many Requests: signaling rate limit exceeded"
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/leave": {
      "post": {
        "summary": "Gracefully Leave Room",
        "description": "Instructs the server to immediately close your session, prune your connection, and notify all other room peers of your departure via roster broadcast.",
        "parameters": [
          {
            "name": "room",
            "in": "query",
            "required": true,
            "description": "The logical room name.",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "X-Token",
            "in": "header",
            "required": false,
            "description": "Your private session token.",
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          },
          {
            "name": "Authorization",
            "in": "header",
            "required": false,
            "description": "Your private session token as a bearer token (format: Bearer <token>).",
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["sender", "token"],
                "properties": {
                  "sender": {
                    "type": "string",
                    "format": "uuid",
                    "description": "Your UUID client ID."
                  },
                  "token": {
                    "type": "string",
                    "format": "uuid",
                    "description": "Your private session token."
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Successfully unregistered session.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "success": {
                      "type": "boolean",
                      "example": true
                    }
                  }
                }
              }
            }
          },
          "403": {
            "description": "Unauthorized: invalid session token.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "411": {
            "description": "Length Required: Content-Length header is missing.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string",
                      "example": "Length Required: Content-Length header is missing"
                    }
                  }
                }
              }
            }
          },
          "413": {
            "description": "Payload Too Large: request body exceeds 100KB limit.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string",
                      "example": "Payload Too Large: Content-Length exceeds 100KB limit"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
