{
  "openapi": "3.1.0",
  "info": {
    "title": "Speko Voice Router API",
    "version": "0.1.0",
    "description": "One API key for speech-to-text, language models, text-to-speech, and realtime speech."
  },
  "servers": [{ "url": "https://api.speko.ai" }],
  "security": [{ "bearerAuth": [] }],
  "paths": {
    "/v1/transcribe": {
      "post": {
        "operationId": "transcribeAudio",
        "summary": "Transcribe raw audio with cross-provider routing",
        "parameters": [
          { "$ref": "#/components/parameters/Objective" },
          { "$ref": "#/components/parameters/Language" },
          { "$ref": "#/components/parameters/Allow" },
          { "$ref": "#/components/parameters/Deny" },
          { "$ref": "#/components/parameters/MaxPrice" }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "audio/wav": { "schema": { "type": "string", "format": "binary" } },
            "application/octet-stream": { "schema": { "type": "string", "format": "binary" } }
          }
        },
        "responses": {
          "200": {
            "description": "Normalized transcription",
            "headers": {
              "x-route": { "$ref": "#/components/headers/Route" },
              "x-route-reason": { "$ref": "#/components/headers/RouteReason" },
              "x-speko-failover-count": { "$ref": "#/components/headers/FailoverCount" },
              "x-speko-first-byte-ms": { "$ref": "#/components/headers/FirstByteMs" }
            },
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Transcription" }
              }
            }
          },
          "default": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/v1/synthesize": {
      "post": {
        "operationId": "synthesizeSpeech",
        "summary": "Synthesize raw PCM with cross-provider routing",
        "parameters": [
          { "$ref": "#/components/parameters/Objective" },
          { "$ref": "#/components/parameters/Language" },
          { "$ref": "#/components/parameters/Allow" },
          { "$ref": "#/components/parameters/Deny" },
          { "$ref": "#/components/parameters/MaxPrice" }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["text"],
                "properties": {
                  "text": { "type": "string", "minLength": 1 },
                  "model": { "type": "string", "default": "auto" },
                  "voice": {
                    "type": "string",
                    "description": "Provider voice id. Omit to use the provider default where supported."
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Signed 16-bit mono PCM audio",
            "headers": {
              "x-route": { "$ref": "#/components/headers/Route" },
              "x-route-reason": { "$ref": "#/components/headers/RouteReason" },
              "x-speko-failover-count": { "$ref": "#/components/headers/FailoverCount" },
              "x-speko-first-byte-ms": { "$ref": "#/components/headers/FirstByteMs" }
            },
            "content": {
              "application/octet-stream": {
                "schema": { "type": "string", "format": "binary" }
              }
            }
          },
          "default": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/v1/audio/transcriptions": {
      "post": {
        "operationId": "createTranscription",
        "summary": "Transcribe audio",
        "description": "OpenAI-compatible multipart input. Speko can route it to OpenAI or extract the audio for Deepgram, then returns the same {\"text\":\"...\"} response.",
        "parameters": [
          { "$ref": "#/components/parameters/Objective" },
          { "$ref": "#/components/parameters/Language" },
          { "$ref": "#/components/parameters/Allow" },
          { "$ref": "#/components/parameters/Deny" },
          { "$ref": "#/components/parameters/MaxPrice" }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "multipart/form-data": {
              "schema": {
                "type": "object",
                "required": ["file", "model"],
                "properties": {
                  "file": { "type": "string", "format": "binary" },
                  "model": { "type": "string", "examples": ["auto"] },
                  "language": { "type": "string" }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Transcription",
            "headers": {
              "x-route": { "$ref": "#/components/headers/Route" },
              "x-route-reason": { "$ref": "#/components/headers/RouteReason" },
              "x-speko-failover-count": { "$ref": "#/components/headers/FailoverCount" },
              "x-speko-first-byte-ms": { "$ref": "#/components/headers/FirstByteMs" }
            },
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": { "text": { "type": "string" } }
                }
              }
            }
          },
          "default": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/v1/audio/speech": {
      "post": {
        "operationId": "createSpeech",
        "summary": "Synthesize speech",
        "description": "OpenAI-compatible alias. Routes only to providers that accept this wire format.",
        "parameters": [
          { "$ref": "#/components/parameters/Objective" },
          { "$ref": "#/components/parameters/Language" },
          { "$ref": "#/components/parameters/Allow" },
          { "$ref": "#/components/parameters/Deny" },
          { "$ref": "#/components/parameters/MaxPrice" }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["model", "voice", "input"],
                "properties": {
                  "model": { "type": "string", "examples": ["gpt-4o-mini-tts"] },
                  "voice": { "type": "string" },
                  "input": { "type": "string" },
                  "response_format": {
                    "type": "string",
                    "enum": ["mp3", "opus", "aac", "flac", "wav", "pcm"]
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Audio bytes",
            "headers": {
              "x-route": { "$ref": "#/components/headers/Route" },
              "x-route-reason": { "$ref": "#/components/headers/RouteReason" },
              "x-speko-failover-count": { "$ref": "#/components/headers/FailoverCount" },
              "x-speko-first-byte-ms": { "$ref": "#/components/headers/FirstByteMs" }
            },
            "content": {
              "application/octet-stream": {
                "schema": { "type": "string", "format": "binary" }
              }
            }
          },
          "default": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/v1/chat/completions": {
      "post": {
        "operationId": "createChatCompletion",
        "summary": "Create a chat completion",
        "parameters": [
          { "$ref": "#/components/parameters/Objective" },
          { "$ref": "#/components/parameters/Language" },
          { "$ref": "#/components/parameters/Allow" },
          { "$ref": "#/components/parameters/Deny" },
          { "$ref": "#/components/parameters/MaxPrice" }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["model", "messages"],
                "properties": {
                  "model": {
                    "type": "string",
                    "description": "Use auto for benchmark-led routing, or provide a model id."
                  },
                  "messages": { "type": "array", "items": { "type": "object" } },
                  "stream": { "type": "boolean", "default": false }
                },
                "additionalProperties": true
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "OpenAI-compatible completion or event stream",
            "headers": {
              "x-route": { "$ref": "#/components/headers/Route" },
              "x-route-reason": { "$ref": "#/components/headers/RouteReason" },
              "x-speko-failover-count": { "$ref": "#/components/headers/FailoverCount" },
              "x-speko-first-byte-ms": { "$ref": "#/components/headers/FirstByteMs" }
            }
          },
          "default": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/v1/models": {
      "get": {
        "operationId": "listModels",
        "summary": "List measured models",
        "security": [],
        "responses": {
          "200": {
            "description": "Model catalog",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["object", "data"],
                  "properties": {
                    "object": { "const": "list" },
                    "data": {
                      "type": "array",
                      "items": { "$ref": "#/components/schemas/Model" }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/v1/routing/preview": {
      "get": {
        "operationId": "previewRoute",
        "summary": "Preview a route without upstream traffic",
        "parameters": [
          {
            "name": "stage",
            "in": "query",
            "required": true,
            "schema": { "type": "string", "enum": ["stt", "llm", "tts"] }
          },
          {
            "name": "language",
            "in": "query",
            "schema": { "type": "string", "default": "en" }
          },
          {
            "name": "objective",
            "in": "query",
            "schema": { "$ref": "#/components/schemas/Objective" }
          },
          {
            "name": "allow",
            "in": "query",
            "description": "Comma-separated providers or provider:model ids.",
            "schema": { "type": "string" }
          },
          {
            "name": "deny",
            "in": "query",
            "description": "Comma-separated providers or provider:model ids.",
            "schema": { "type": "string" }
          },
          {
            "name": "max_price",
            "in": "query",
            "schema": { "type": "number", "minimum": 0 }
          }
        ],
        "responses": {
          "200": { "description": "Routing decision" },
          "default": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/v1/realtime/sessions": {
      "post": {
        "operationId": "createRealtimeSession",
        "summary": "Mint a 60-second browser credential",
        "responses": {
          "200": {
            "description": "Ephemeral credential",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["client_secret"],
                  "properties": {
                    "client_secret": {
                      "type": "object",
                      "required": ["value", "expires_in"],
                      "properties": {
                        "value": { "type": "string" },
                        "expires_in": { "type": "integer", "const": 60 }
                      }
                    }
                  }
                }
              }
            }
          },
          "default": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/v1/realtime": {
      "get": {
        "operationId": "connectRealtime",
        "summary": "Open a speech-to-speech WebSocket",
        "description": "Upgrade to WebSocket. Browsers offer speko.realtime.v1 and speko.ephemeral.<credential> as subprotocols.",
        "parameters": [
          {
            "name": "model",
            "in": "query",
            "schema": { "type": "string", "default": "gpt-realtime" }
          }
        ],
        "responses": {
          "101": { "description": "WebSocket accepted" },
          "default": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/v1/transcribe/stream": {
      "get": {
        "operationId": "streamTranscription",
        "summary": "Open a realtime transcription WebSocket",
        "description": "Upgrade to WebSocket, then send {\"language\":\"en\",\"interimResults\":false} as the first text frame. Browsers offer speko.realtime.v1 and speko.ephemeral.<credential> as subprotocols.",
        "responses": {
          "101": { "description": "WebSocket accepted" },
          "default": { "$ref": "#/components/responses/Error" }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "Speko API key"
      }
    },
    "parameters": {
      "Objective": {
        "name": "X-Speko-Objective",
        "in": "header",
        "description": "Routing objective. Defaults to the route policy.",
        "schema": { "$ref": "#/components/schemas/Objective" }
      },
      "Language": {
        "name": "X-Speko-Language",
        "in": "header",
        "description": "BCP 47 language tag used for benchmark selection and provider transcription.",
        "schema": { "type": "string", "default": "en" }
      },
      "Allow": {
        "name": "X-Speko-Allow",
        "in": "header",
        "description": "Comma-separated providers or provider:model ids to allow.",
        "schema": { "type": "string" }
      },
      "Deny": {
        "name": "X-Speko-Deny",
        "in": "header",
        "description": "Comma-separated providers or provider:model ids to exclude.",
        "schema": { "type": "string" }
      },
      "MaxPrice": {
        "name": "X-Speko-Max-Price",
        "in": "header",
        "description": "Maximum benchmark price accepted for the selected stage.",
        "schema": { "type": "number", "minimum": 0 }
      }
    },
    "headers": {
      "Route": {
        "description": "Provider and model that served the response.",
        "schema": { "type": "string" }
      },
      "RouteReason": {
        "description": "Compact explanation of the routing decision.",
        "schema": { "type": "string" }
      },
      "FailoverCount": {
        "description": "Number of failed or circuit-bypassed candidates before the serving route.",
        "schema": { "type": "integer", "minimum": 0 }
      },
      "FirstByteMs": {
        "description": "Router-observed time to the first upstream body frame.",
        "schema": { "type": "integer", "minimum": 0 }
      }
    },
    "schemas": {
      "Transcription": {
        "type": "object",
        "required": ["text"],
        "properties": {
          "text": { "type": "string" }
        }
      },
      "Objective": {
        "type": "string",
        "enum": ["latency", "quality", "cost", "balanced"]
      },
      "Model": {
        "type": "object",
        "required": ["id", "object", "provider", "model", "api", "routable"],
        "properties": {
          "id": { "type": "string" },
          "object": { "const": "model" },
          "provider": { "type": "string" },
          "model": { "type": "string" },
          "api": { "type": "string", "enum": ["stt", "llm", "tts"] },
          "routable": { "type": "boolean" }
        }
      },
      "Error": {
        "type": "object",
        "required": ["error"],
        "properties": {
          "error": {
            "type": "object",
            "required": ["code", "message"],
            "properties": {
              "code": { "type": "string" },
              "message": { "type": "string" },
              "type": { "type": "string" },
              "details": {}
            }
          }
        }
      }
    },
    "responses": {
      "Error": {
        "description": "Error",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" }
          }
        }
      }
    }
  }
}
