{
  "openapi": "3.1.0",
  "info": {
    "title": "bgly Background Removal API",
    "version": "1.0.0",
    "description": "Create background-removal tasks and poll for cutout results."
  },
  "servers": [{ "url": "https://bgly.net/api/v1" }],
  "security": [{ "bearerAuth": [] }],
  "paths": {
    "/bg/removals": {
      "post": {
        "summary": "Create a background removal",
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": false,
            "schema": { "type": "string", "maxLength": 128 }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/CreateBgRemoval" }
            }
          }
        },
        "responses": {
          "202": {
            "description": "Removal accepted",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/BgRemoval" }
              }
            }
          },
          "400": { "$ref": "#/components/responses/ApiError" },
          "401": { "$ref": "#/components/responses/ApiError" },
          "402": { "$ref": "#/components/responses/ApiError" },
          "403": { "$ref": "#/components/responses/ApiError" },
          "409": { "$ref": "#/components/responses/ApiError" },
          "429": { "$ref": "#/components/responses/ApiError" }
        }
      }
    },
    "/bg/removals/{id}": {
      "get": {
        "summary": "Get a background removal",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": { "type": "string" }
          }
        ],
        "responses": {
          "200": {
            "description": "Current removal state",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/BgRemoval" }
              }
            }
          },
          "401": { "$ref": "#/components/responses/ApiError" },
          "404": { "$ref": "#/components/responses/ApiError" },
          "429": { "$ref": "#/components/responses/ApiError" }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": { "type": "http", "scheme": "bearer" }
    },
    "schemas": {
      "CreateBgRemoval": {
        "type": "object",
        "required": ["image_url"],
        "properties": {
          "model": { "type": "string", "const": "bg-remover" },
          "image_url": { "type": "string", "format": "uri" },
          "subject": {
            "type": "string",
            "enum": ["auto", "product", "portrait", "hair"],
            "default": "auto"
          },
          "precision": {
            "type": "string",
            "enum": ["standard", "high", "hd"],
            "default": "standard"
          },
          "output_format": {
            "type": "string",
            "enum": ["png", "webp"],
            "default": "png"
          }
        }
      },
      "BgRemoval": {
        "type": "object",
        "required": ["id", "object", "model", "status"],
        "properties": {
          "id": { "type": "string" },
          "object": { "type": "string", "const": "bg.removal" },
          "model": { "type": "string", "const": "bg-remover" },
          "status": {
            "type": "string",
            "enum": ["queued", "processing", "succeeded", "failed", "canceled"]
          },
          "created_at": { "type": ["string", "null"], "format": "date-time" },
          "updated_at": { "type": ["string", "null"], "format": "date-time" },
          "input": { "type": ["object", "null"] },
          "output": { "type": ["object", "null"] },
          "usage": { "type": ["object", "null"] },
          "error": { "type": ["object", "null"] }
        }
      },
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "object",
            "required": ["code", "message", "request_id"],
            "properties": {
              "code": { "type": "string" },
              "message": { "type": "string" },
              "request_id": { "type": "string" }
            }
          }
        }
      }
    },
    "responses": {
      "ApiError": {
        "description": "API error",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" }
          }
        }
      }
    }
  }
}
