{
  "openapi": "3.1.0",
  "info": {
    "title": "Backant Runtime & Application API",
    "version": "2.0.0",
    "summary": "Machine-readable HTTP API contract for Backant Application Auth, Managed Cloudflare R2 Storage, Live Query SSE, and Platform Health.",
    "description": "Official OpenAPI 3.1 specification for Backant's standard runtime endpoints consumed by @backant-app/auth-sdk, @backant-app/live-sdk, and custom client applications. Custom project routes defined in a Backant project are mounted under /{serverId}/{env}/... alongside these built-in endpoints.",
    "contact": {
      "name": "Backant",
      "url": "https://backant.app",
      "email": "security@backant.app"
    },
    "license": {
      "name": "MIT",
      "url": "https://github.com/backant-app/backant-cli/blob/main/LICENSE"
    }
  },
  "externalDocs": {
    "description": "Backant Developer Documentation & llms-full.txt",
    "url": "https://backant.app/docs/"
  },
  "servers": [
    {
      "url": "https://api.backant.app",
      "description": "Backant Cloud (Production)"
    },
    {
      "url": "https://staging-api.backant.app",
      "description": "Backant Cloud (Staging)"
    }
  ],
  "tags": [
    { "name": "Health", "description": "Platform liveness and database readiness" },
    { "name": "Application Auth", "description": "End-user authentication, MFA, OAuth, and organizations (consumed by @backant-app/auth-sdk)" },
    { "name": "Managed Storage", "description": "Cloudflare R2 presigned single-part and multipart uploads, signed downloads, and asset management" },
    { "name": "Live Query", "description": "Real-time Server-Sent Events (SSE) subscriptions and typed queries (consumed by @backant-app/live-sdk)" }
  ],
  "components": {
    "securitySchemes": {
      "BearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "JWT",
        "description": "Application user access token returned by /{serverId}/{env}/auth/login, register, oauth/exchange, or magic-link/consume."
      },
      "OrganizationHeader": {
        "type": "apiKey",
        "in": "header",
        "name": "x-organization-id",
        "description": "Optional active organization ID for multi-tenant SaaS queries and storage."
      }
    },
    "parameters": {
      "serverId": {
        "name": "serverId",
        "in": "path",
        "required": true,
        "schema": { "type": "string" },
        "description": "Backant server identifier."
      },
      "env": {
        "name": "env",
        "in": "path",
        "required": true,
        "schema": { "type": "string", "enum": ["dev", "staging", "prod"] },
        "description": "Application environment."
      }
    },
    "schemas": {
      "ApiEnvelope": {
        "type": "object",
        "required": ["success"],
        "properties": {
          "success": { "type": "boolean" },
          "data": {},
          "error": { "type": "string" },
          "message": { "type": "string" }
        }
      },
      "ErrorEnvelope": {
        "type": "object",
        "required": ["success", "error"],
        "properties": {
          "success": { "type": "boolean", "const": false },
          "error": { "type": "string" },
          "code": { "type": "string" },
          "message": { "type": "string" },
          "traceId": { "type": "string", "description": "Correlates with `backant logs list --trace <traceId>`." }
        }
      },
      "StorageAsset": {
        "type": "object",
        "required": ["id", "type", "mimeType", "size", "visibility", "status", "env", "storageProvider", "servePath"],
        "properties": {
          "id": { "type": "string" },
          "type": { "type": "string", "enum": ["audio", "image", "video", "file", "custom"] },
          "mimeType": { "type": "string" },
          "size": { "type": "integer", "minimum": 0 },
          "checksum": { "type": "string" },
          "visibility": { "type": "string", "enum": ["public", "private", "restricted"] },
          "status": { "type": "string" },
          "createdAt": { "type": "integer" },
          "createdBy": { "type": "string" },
          "env": { "type": "string" },
          "metadata": { "type": "object", "additionalProperties": true },
          "storageProvider": { "type": "string" },
          "servePath": { "type": "string" }
        }
      }
    }
  },
  "paths": {
    "/health": {
      "get": {
        "tags": ["Health"],
        "summary": "Platform readiness check",
        "description": "Verifies API process health and live MongoDB connectivity.",
        "responses": {
          "200": {
            "description": "Service is healthy",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ApiEnvelope" } } }
          },
          "503": { "description": "Service unavailable" }
        }
      }
    },
    "/{serverId}/{env}/auth/register": {
      "post": {
        "tags": ["Application Auth"],
        "summary": "Register a new application user",
        "parameters": [
          { "$ref": "#/components/parameters/serverId" },
          { "$ref": "#/components/parameters/env" }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["email", "password"],
                "properties": {
                  "email": { "type": "string", "format": "email" },
                  "password": { "type": "string", "minLength": 8 }
                }
              }
            }
          }
        },
        "responses": {
          "200": { "description": "User registered", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ApiEnvelope" } } } },
          "400": { "description": "Validation error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorEnvelope" } } } }
        }
      }
    },
    "/{serverId}/{env}/auth/login": {
      "post": {
        "tags": ["Application Auth"],
        "summary": "Authenticate with email and password",
        "description": "Returns a JWT access token, or an MFA challenge/enrollment token when MFA is enforced.",
        "parameters": [
          { "$ref": "#/components/parameters/serverId" },
          { "$ref": "#/components/parameters/env" }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["email", "password"],
                "properties": {
                  "email": { "type": "string", "format": "email" },
                  "password": { "type": "string" }
                }
              }
            }
          }
        },
        "responses": {
          "200": { "description": "Authenticated or MFA challenge issued", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ApiEnvelope" } } } },
          "401": { "description": "Invalid credentials", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorEnvelope" } } } }
        }
      }
    },
    "/{serverId}/{env}/auth/me": {
      "get": {
        "tags": ["Application Auth"],
        "summary": "Get current authenticated application user",
        "security": [{ "BearerAuth": [] }],
        "parameters": [
          { "$ref": "#/components/parameters/serverId" },
          { "$ref": "#/components/parameters/env" }
        ],
        "responses": {
          "200": { "description": "Current user profile", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ApiEnvelope" } } } },
          "401": { "description": "Unauthenticated" }
        }
      }
    },
    "/{serverId}/{env}/auth/logout": {
      "post": {
        "tags": ["Application Auth"],
        "summary": "Revoke current session",
        "security": [{ "BearerAuth": [] }],
        "parameters": [
          { "$ref": "#/components/parameters/serverId" },
          { "$ref": "#/components/parameters/env" }
        ],
        "responses": { "204": { "description": "Logged out" } }
      }
    },
    "/{serverId}/{env}/auth/logout-all": {
      "post": {
        "tags": ["Application Auth"],
        "summary": "Revoke all active sessions for the current user",
        "security": [{ "BearerAuth": [] }],
        "parameters": [
          { "$ref": "#/components/parameters/serverId" },
          { "$ref": "#/components/parameters/env" }
        ],
        "responses": { "204": { "description": "All sessions revoked" } }
      }
    },
    "/{serverId}/{env}/auth/oauth/{provider}/start": {
      "get": {
        "tags": ["Application Auth"],
        "summary": "Start browser OAuth flow",
        "parameters": [
          { "$ref": "#/components/parameters/serverId" },
          { "$ref": "#/components/parameters/env" },
          { "name": "provider", "in": "path", "required": true, "schema": { "type": "string", "enum": ["google", "github"] } }
        ],
        "responses": { "302": { "description": "Redirect to OAuth provider" } }
      }
    },
    "/{serverId}/{env}/auth/oauth/exchange": {
      "post": {
        "tags": ["Application Auth"],
        "summary": "Exchange one-time OAuth callback code for a session token",
        "parameters": [
          { "$ref": "#/components/parameters/serverId" },
          { "$ref": "#/components/parameters/env" }
        ],
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": { "type": "object", "required": ["code"], "properties": { "code": { "type": "string" } } } } }
        },
        "responses": { "200": { "description": "Session token issued" } }
      }
    },
    "/{serverId}/{env}/auth/mfa/enroll": {
      "post": {
        "tags": ["Application Auth"],
        "summary": "Begin TOTP MFA enrollment",
        "security": [{ "BearerAuth": [] }],
        "parameters": [
          { "$ref": "#/components/parameters/serverId" },
          { "$ref": "#/components/parameters/env" }
        ],
        "responses": { "200": { "description": "Returns TOTP secret and otpauthUrl" } }
      }
    },
    "/{serverId}/{env}/auth/mfa/verify-enroll": {
      "post": {
        "tags": ["Application Auth"],
        "summary": "Verify TOTP code to finalize MFA enrollment and issue recovery codes",
        "security": [{ "BearerAuth": [] }],
        "parameters": [
          { "$ref": "#/components/parameters/serverId" },
          { "$ref": "#/components/parameters/env" }
        ],
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": { "type": "object", "required": ["code"], "properties": { "code": { "type": "string" }, "mfaEnrollmentToken": { "type": "string" } } } } }
        },
        "responses": { "200": { "description": "MFA enrolled" } }
      }
    },
    "/{serverId}/{env}/auth/mfa/challenge": {
      "post": {
        "tags": ["Application Auth"],
        "summary": "Complete an MFA login challenge with TOTP or recovery code",
        "parameters": [
          { "$ref": "#/components/parameters/serverId" },
          { "$ref": "#/components/parameters/env" }
        ],
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": { "type": "object", "required": ["challengeToken"], "properties": { "challengeToken": { "type": "string" }, "code": { "type": "string" }, "recoveryCode": { "type": "string" } } } } }
        },
        "responses": { "200": { "description": "Session token issued" } }
      }
    },
    "/{serverId}/{env}/auth/organizations": {
      "get": {
        "tags": ["Application Auth"],
        "summary": "List organizations and roles for the authenticated user",
        "security": [{ "BearerAuth": [] }],
        "parameters": [
          { "$ref": "#/components/parameters/serverId" },
          { "$ref": "#/components/parameters/env" }
        ],
        "responses": { "200": { "description": "Organization memberships" } }
      }
    },
    "/{serverId}/{env}/auth/invitations/{invitationId}/accept": {
      "post": {
        "tags": ["Application Auth"],
        "summary": "Accept an organization invitation",
        "security": [{ "BearerAuth": [] }],
        "parameters": [
          { "$ref": "#/components/parameters/serverId" },
          { "$ref": "#/components/parameters/env" },
          { "name": "invitationId", "in": "path", "required": true, "schema": { "type": "string" } }
        ],
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": { "type": "object", "required": ["token"], "properties": { "token": { "type": "string" } } } } }
        },
        "responses": { "200": { "description": "Invitation accepted" } }
      }
    },
    "/{serverId}/{env}/storage/uploads/presign": {
      "post": {
        "tags": ["Managed Storage"],
        "summary": "Create a single-part presigned Cloudflare R2 PUT upload URL",
        "description": "Requires an active application user session (401 otherwise). For no-auth apps, issue presigned URLs from a server Action via ctx.assets.getUploadUrl().",
        "security": [{ "BearerAuth": [], "OrganizationHeader": [] }],
        "parameters": [
          { "$ref": "#/components/parameters/serverId" },
          { "$ref": "#/components/parameters/env" }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["filename", "mimeType", "size"],
                "properties": {
                  "filename": { "type": "string" },
                  "mimeType": { "type": "string" },
                  "size": { "type": "integer", "minimum": 1 },
                  "visibility": { "type": "string", "enum": ["public", "private", "restricted"], "default": "private" },
                  "type": { "type": "string", "enum": ["audio", "image", "video", "file", "custom"] },
                  "metadata": { "type": "object", "additionalProperties": true }
                }
              }
            }
          }
        },
        "responses": {
          "200": { "description": "Presigned upload descriptor returned" },
          "401": { "description": "Authentication required for storage operation" }
        }
      }
    },
    "/{serverId}/{env}/storage/uploads/{uploadId}/complete": {
      "post": {
        "tags": ["Managed Storage"],
        "summary": "Finalize a presigned upload and persist the AssetRecord",
        "security": [{ "BearerAuth": [], "OrganizationHeader": [] }],
        "parameters": [
          { "$ref": "#/components/parameters/serverId" },
          { "$ref": "#/components/parameters/env" },
          { "name": "uploadId", "in": "path", "required": true, "schema": { "type": "string" } }
        ],
        "responses": { "200": { "description": "Finalized StorageAsset" } }
      }
    },
    "/{serverId}/{env}/storage/uploads/multipart/init": {
      "post": {
        "tags": ["Managed Storage"],
        "summary": "Initialize a chunked multipart upload (files > 10 MB, up to 1 GB)",
        "security": [{ "BearerAuth": [], "OrganizationHeader": [] }],
        "parameters": [
          { "$ref": "#/components/parameters/serverId" },
          { "$ref": "#/components/parameters/env" }
        ],
        "responses": { "200": { "description": "Multipart upload session initialized" } }
      }
    },
    "/{serverId}/{env}/storage/assets": {
      "get": {
        "tags": ["Managed Storage"],
        "summary": "List stored assets for the current server environment",
        "security": [{ "BearerAuth": [], "OrganizationHeader": [] }],
        "parameters": [
          { "$ref": "#/components/parameters/serverId" },
          { "$ref": "#/components/parameters/env" },
          { "name": "type", "in": "query", "schema": { "type": "string" } },
          { "name": "visibility", "in": "query", "schema": { "type": "string" } },
          { "name": "limit", "in": "query", "schema": { "type": "integer", "default": 50 } },
          { "name": "skip", "in": "query", "schema": { "type": "integer", "default": 0 } }
        ],
        "responses": { "200": { "description": "Paginated StorageAsset list" } }
      }
    },
    "/{serverId}/{env}/storage/assets/{assetId}/signed-url": {
      "post": {
        "tags": ["Managed Storage"],
        "summary": "Generate a short-lived signed download URL for an asset",
        "security": [{ "BearerAuth": [], "OrganizationHeader": [] }],
        "parameters": [
          { "$ref": "#/components/parameters/serverId" },
          { "$ref": "#/components/parameters/env" },
          { "name": "assetId", "in": "path", "required": true, "schema": { "type": "string" } }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "download": { "type": "boolean", "default": false },
                  "expiresInSeconds": { "type": "integer", "minimum": 1 }
                }
              }
            }
          }
        },
        "responses": { "200": { "description": "Signed URL returned" } }
      }
    },
    "/{serverId}/{env}/storage/assets/{assetId}": {
      "get": {
        "tags": ["Managed Storage"],
        "summary": "Get asset metadata",
        "security": [{ "BearerAuth": [], "OrganizationHeader": [] }],
        "parameters": [
          { "$ref": "#/components/parameters/serverId" },
          { "$ref": "#/components/parameters/env" },
          { "name": "assetId", "in": "path", "required": true, "schema": { "type": "string" } }
        ],
        "responses": { "200": { "description": "StorageAsset record" } }
      },
      "delete": {
        "tags": ["Managed Storage"],
        "summary": "Delete an asset from R2 and metadata store",
        "security": [{ "BearerAuth": [], "OrganizationHeader": [] }],
        "parameters": [
          { "$ref": "#/components/parameters/serverId" },
          { "$ref": "#/components/parameters/env" },
          { "name": "assetId", "in": "path", "required": true, "schema": { "type": "string" } }
        ],
        "responses": { "200": { "description": "Asset deleted" } }
      }
    },
    "/api/live-query/fetch": {
      "post": {
        "tags": ["Live Query"],
        "summary": "Execute a one-shot typed Live Query",
        "security": [{ "BearerAuth": [], "OrganizationHeader": [] }],
        "parameters": [
          { "name": "serverId", "in": "query", "required": true, "schema": { "type": "string" } },
          { "name": "env", "in": "query", "schema": { "type": "string", "default": "dev" } }
        ],
        "responses": { "200": { "description": "Query result set" } }
      }
    },
    "/api/live-query/subscribe": {
      "post": {
        "tags": ["Live Query"],
        "summary": "Open a real-time Server-Sent Events (SSE) Live Query stream",
        "security": [{ "BearerAuth": [], "OrganizationHeader": [] }],
        "parameters": [
          { "name": "serverId", "in": "query", "required": true, "schema": { "type": "string" } },
          { "name": "env", "in": "query", "schema": { "type": "string", "default": "dev" } }
        ],
        "responses": {
          "200": {
            "description": "text/event-stream SSE subscription",
            "content": { "text/event-stream": { "schema": { "type": "string" } } }
          }
        }
      }
    },
    "/api/live-query/write": {
      "post": {
        "tags": ["Live Query"],
        "summary": "Perform a policy-checked Live Query mutation",
        "security": [{ "BearerAuth": [], "OrganizationHeader": [] }],
        "parameters": [
          { "name": "serverId", "in": "query", "required": true, "schema": { "type": "string" } },
          { "name": "env", "in": "query", "schema": { "type": "string", "default": "dev" } }
        ],
        "responses": { "200": { "description": "Mutation applied and broadcast to subscribers" } }
      }
    }
  }
}
