{
  "openapi": "3.1.0",
  "info": {
    "title": "Modern Saray Machine API",
    "version": "1.0.0",
    "description": "Published external API subset. Server-side Bearer API keys only. Browser Origin requests are rejected. Key access is not a provider-independent agent identity or delegation certificate. Existing API origin remains compatible during brand migration."
  },
  "servers": [
    {
      "url": "https://modernsaray.com/api",
      "description": "Existing verified API origin; no hostname cutover implied"
    }
  ],
  "externalDocs": {
    "url": "https://modernsaray.com/docs.html"
  },
  "paths": {
    "/v1/models": {
      "get": {
        "operationId": "listModels",
        "summary": "List accessible model IDs",
        "security": [
          {
            "ApiKeyBearer": []
          }
        ],
        "responses": {
          "200": {
            "description": "Successful response. Fields depend on the operation.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "additionalProperties": true
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API key"
          },
          "403": {
            "description": "Scope, IP or browser-origin restriction"
          },
          "402": {
            "description": "Insufficient API wallet balance"
          },
          "409": {
            "description": "Account or operation conflict"
          },
          "422": {
            "description": "Invalid request"
          },
          "429": {
            "description": "Rate limit exceeded"
          },
          "500": {
            "description": "Service error"
          }
        }
      }
    },
    "/v1/chat/completions": {
      "post": {
        "operationId": "createChatCompletion",
        "summary": "Create a chat completion",
        "security": [
          {
            "ApiKeyBearer": []
          }
        ],
        "responses": {
          "200": {
            "description": "Successful response. Fields depend on the operation.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "additionalProperties": true
                }
              },
              "text/event-stream": {
                "schema": {
                  "type": "string"
                },
                "example": "data: {\"choices\":[{\"delta\":{\"content\":\"Hello\"}}]}\n\ndata: [DONE]\n\n"
              }
            }
          },
          "401": {
            "description": "Missing or invalid API key"
          },
          "403": {
            "description": "Scope, IP or browser-origin restriction"
          },
          "402": {
            "description": "Insufficient API wallet balance"
          },
          "409": {
            "description": "Account or operation conflict"
          },
          "422": {
            "description": "Invalid request"
          },
          "429": {
            "description": "Rate limit exceeded"
          },
          "500": {
            "description": "Service error"
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ChatCompletionRequest"
              }
            }
          }
        },
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": false,
            "description": "Reuse for retries of the same logical completion, not for different operations.",
            "schema": {
              "type": "string"
            }
          }
        ]
      }
    },
    "/v1/external/chat": {
      "post": {
        "operationId": "createExternalChat",
        "summary": "Send a message with optional conversation session",
        "security": [
          {
            "ApiKeyBearer": []
          }
        ],
        "responses": {
          "200": {
            "description": "Successful response. Fields depend on the operation.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "additionalProperties": true
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API key"
          },
          "403": {
            "description": "Scope, IP or browser-origin restriction"
          },
          "402": {
            "description": "Insufficient API wallet balance"
          },
          "409": {
            "description": "Account or operation conflict"
          },
          "422": {
            "description": "Invalid request"
          },
          "429": {
            "description": "Rate limit exceeded"
          },
          "500": {
            "description": "Service error"
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ExternalChatRequest"
              }
            }
          }
        }
      }
    },
    "/v1/external/me": {
      "get": {
        "operationId": "getApiAccount",
        "summary": "Inspect the account associated with the API key; not Agent Passport",
        "security": [
          {
            "ApiKeyBearer": []
          }
        ],
        "responses": {
          "200": {
            "description": "Successful response. Fields depend on the operation.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "additionalProperties": true
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API key"
          },
          "403": {
            "description": "Scope, IP or browser-origin restriction"
          },
          "402": {
            "description": "Insufficient API wallet balance"
          },
          "409": {
            "description": "Account or operation conflict"
          },
          "422": {
            "description": "Invalid request"
          },
          "429": {
            "description": "Rate limit exceeded"
          },
          "500": {
            "description": "Service error"
          }
        }
      }
    },
    "/v1/external/usage": {
      "get": {
        "operationId": "getApiUsage",
        "summary": "Inspect private API usage and wallet status",
        "security": [
          {
            "ApiKeyBearer": []
          }
        ],
        "responses": {
          "200": {
            "description": "Successful response. Fields depend on the operation.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "additionalProperties": true
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API key"
          },
          "403": {
            "description": "Scope, IP or browser-origin restriction"
          },
          "402": {
            "description": "Insufficient API wallet balance"
          },
          "409": {
            "description": "Account or operation conflict"
          },
          "422": {
            "description": "Invalid request"
          },
          "429": {
            "description": "Rate limit exceeded"
          },
          "500": {
            "description": "Service error"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "ApiKeyBearer": {
        "type": "http",
        "scheme": "bearer",
        "description": "API key from the authenticated developer portal. Keep it server-side."
      }
    },
    "schemas": {
      "ChatMessage": {
        "type": "object",
        "required": [
          "role",
          "content"
        ],
        "properties": {
          "role": {
            "type": "string",
            "enum": [
              "system",
              "developer",
              "user",
              "assistant",
              "tool"
            ]
          },
          "content": {
            "type": "string"
          }
        }
      },
      "ChatCompletionRequest": {
        "type": "object",
        "required": [
          "messages"
        ],
        "properties": {
          "model": {
            "type": "string",
            "description": "Select an accessible model ID from GET /v1/models."
          },
          "messages": {
            "type": "array",
            "minItems": 1,
            "maxItems": 100,
            "items": {
              "$ref": "#/components/schemas/ChatMessage"
            }
          },
          "stream": {
            "type": "boolean",
            "default": false
          },
          "temperature": {
            "type": [
              "number",
              "null"
            ]
          },
          "max_tokens": {
            "type": [
              "integer",
              "null"
            ]
          },
          "user": {
            "type": [
              "string",
              "null"
            ],
            "maxLength": 200
          }
        }
      },
      "ExternalChatRequest": {
        "type": "object",
        "required": [
          "message"
        ],
        "properties": {
          "message": {
            "type": "string",
            "minLength": 1,
            "maxLength": 12000
          },
          "session_id": {
            "type": [
              "string",
              "null"
            ],
            "maxLength": 200
          }
        }
      }
    }
  }
}
