{
  "openapi": "3.1.0",
  "info": {
    "title": "Timestral API",
    "version": "1.0.0",
    "description": "CSV in, probabilistic time-series forecast out. One credit per successful forecast call; failed calls are refunded.",
    "contact": {
      "name": "Timestral support",
      "email": "contact@timestral.org",
      "url": "https://timestral.org/api-reference"
    }
  },
  "servers": [
    {
      "url": "https://timestral.org",
      "description": "Production"
    }
  ],
  "security": [
    {
      "bearerAuth": []
    }
  ],
  "paths": {
    "/api/forecast": {
      "post": {
        "operationId": "createForecast",
        "summary": "Forecast a CSV series",
        "description": "Synchronous zero-shot forecast. Returns a median and 80% band for each step, plus a link to the full 21-quantile CSV. At least 8 rows, at most 8 numeric columns, 2 MB maximum body.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["csv"],
                "properties": {
                  "csv": {
                    "type": "string",
                    "description": "CSV text with an optional date column and 1–8 numeric columns.",
                    "maxLength": 2097152
                  },
                  "steps": {
                    "type": "integer",
                    "minimum": 1,
                    "maximum": 1024,
                    "default": 12,
                    "description": "Forecast horizon in steps."
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Forecast completed",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ForecastEnvelope"
                }
              }
            }
          },
          "400": {
            "description": "Invalid CSV, horizon, or credit balance",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorEnvelope"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API key",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorEnvelope"
                }
              }
            }
          },
          "429": {
            "description": "Rate limited",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorEnvelope"
                }
              }
            }
          }
        }
      }
    },
    "/api/forecast/email": {
      "post": {
        "operationId": "emailForecast",
        "summary": "Email a browser-generated forecast CSV",
        "description": "Stores the CSV in R2 and emails the link. This is the no-account delivery path used by the browser studio; it is not a general-purpose email API.",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["email", "csv"],
                "properties": {
                  "email": {
                    "type": "string",
                    "format": "email",
                    "maxLength": 254
                  },
                  "csv": {
                    "type": "string",
                    "maxLength": 2097152
                  },
                  "filename": {
                    "type": "string",
                    "maxLength": 120
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Email accepted",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/OkEnvelope"
                }
              }
            }
          },
          "400": {
            "description": "Invalid email, CSV, or provider configuration",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorEnvelope"
                }
              }
            }
          }
        }
      }
    },
    "/api/health": {
      "get": {
        "operationId": "health",
        "summary": "Liveness and build information",
        "security": [],
        "responses": {
          "200": {
            "description": "Worker is ready",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "status": { "type": "string" },
                    "commitSha": { "type": "string" },
                    "checks": { "type": "object" }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/apikeys": {
      "get": {
        "operationId": "listApiKeys",
        "summary": "List API keys for the signed-in account",
        "security": [{ "sessionCookie": [] }],
        "parameters": [
          { "name": "page", "in": "query", "schema": { "type": "integer", "minimum": 1, "default": 1 } },
          { "name": "pageSize", "in": "query", "schema": { "type": "integer", "minimum": 1, "maximum": 100, "default": 20 } },
          { "name": "search", "in": "query", "schema": { "type": "string" } }
        ],
        "responses": {
          "200": {
            "description": "Paginated keys",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "code": { "type": "integer", "const": 0 },
                    "message": { "type": "string" },
                    "data": {
                      "type": "object",
                      "properties": {
                        "items": { "type": "array", "items": { "$ref": "#/components/schemas/ApiKey" } },
                        "total": { "type": "integer" }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      },
      "post": {
        "operationId": "createApiKey",
        "summary": "Create an API key",
        "security": [{ "sessionCookie": [] }],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["title"],
                "properties": { "title": { "type": "string", "maxLength": 120 } }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Key created; the raw key is returned once",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "code": { "type": "integer", "const": 0 },
                    "message": { "type": "string" },
                    "data": {
                      "type": "object",
                      "properties": {
                        "id": { "type": "string" },
                        "key": { "type": "string" },
                        "title": { "type": "string" }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      },
      "delete": {
        "operationId": "deleteApiKey",
        "summary": "Soft-delete an API key",
        "security": [{ "sessionCookie": [] }],
        "parameters": [
          { "name": "id", "in": "query", "required": true, "schema": { "type": "string" } }
        ],
        "responses": {
          "200": {
            "description": "Deleted",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/OkEnvelope" }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "sk_...",
        "description": "Create a key at https://timestral.org/settings/apikeys."
      },
      "sessionCookie": {
        "type": "apiKey",
        "in": "cookie",
        "name": "better-auth.session_token",
        "description": "Browser session; used by the account UI, not by server-to-server clients."
      }
    },
    "schemas": {
      "ForecastRow": {
        "type": "object",
        "required": ["time", "column", "low", "median", "high"],
        "properties": {
          "time": { "type": "string" },
          "column": { "type": "string" },
          "low": { "type": "number" },
          "median": { "type": "number" },
          "high": { "type": "number" }
        }
      },
      "ForecastResult": {
        "type": "object",
        "required": ["taskId", "columns", "rows", "took"],
        "properties": {
          "taskId": { "type": "string" },
          "columns": { "type": "array", "items": { "type": "string" } },
          "rows": { "type": "array", "items": { "$ref": "#/components/schemas/ForecastRow" } },
          "took": { "type": "string" },
          "fileUrl": { "type": "string", "format": "uri" }
        }
      },
      "ForecastEnvelope": {
        "type": "object",
        "required": ["code", "message", "data"],
        "properties": {
          "code": { "type": "integer", "const": 0 },
          "message": { "type": "string", "const": "ok" },
          "data": { "$ref": "#/components/schemas/ForecastResult" }
        }
      },
      "ApiKey": {
        "type": "object",
        "properties": {
          "id": { "type": "string" },
          "keyPrefix": { "type": "string" },
          "key": { "type": ["string", "null"] },
          "title": { "type": "string" },
          "status": { "type": "string" },
          "lastUsedAt": { "type": ["string", "null"], "format": "date-time" },
          "createdAt": { "type": "string", "format": "date-time" }
        }
      },
      "ErrorEnvelope": {
        "type": "object",
        "required": ["code", "message"],
        "properties": {
          "code": { "type": "integer", "const": -1 },
          "message": { "type": "string" }
        }
      },
      "OkEnvelope": {
        "type": "object",
        "required": ["code", "message"],
        "properties": {
          "code": { "type": "integer", "const": 0 },
          "message": { "type": "string" }
        }
      }
    }
  }
}
