{
  "openapi": "3.1.0",
  "info": {
    "title": "BudgetThing Import API",
    "version": "1.0.0",
    "description": "Submit structured expense and income candidates to the BudgetThing import inbox."
  },
  "servers": [
    {
      "url": "https://ceaseless-mandrill-733.convex.site/v1"
    }
  ],
  "components": {
    "securitySchemes": {
      "BudgetThingApiKey": {
        "type": "http",
        "scheme": "bearer"
      }
    },
    "schemas": {
      "ImportItem": {
        "type": "object",
        "additionalProperties": false,
        "required": ["externalId", "type", "amountCents", "currencyCode", "occurredAt"],
        "properties": {
          "externalId": { "type": "string", "maxLength": 256 },
          "type": { "type": "string", "enum": ["expense", "income"] },
          "amountCents": { "type": "integer", "minimum": 1 },
          "currencyCode": { "type": "string", "minLength": 3, "maxLength": 3 },
          "occurredAt": { "type": "string", "format": "date-time" },
          "merchantName": { "type": "string", "maxLength": 120 },
          "note": { "type": "string", "maxLength": 200 },
          "accountId": { "type": "string" },
          "categoryId": { "type": "string" }
        }
      },
      "CreateImportsRequest": {
        "type": "object",
        "additionalProperties": false,
        "required": ["source", "items"],
        "properties": {
          "source": { "type": "string", "maxLength": 80 },
          "items": {
            "type": "array",
            "minItems": 1,
            "maxItems": 50,
            "items": { "$ref": "#/components/schemas/ImportItem" }
          }
        }
      }
    }
  },
  "paths": {
    "/health": {
      "get": {
        "operationId": "getHealth",
        "summary": "Check API health",
        "responses": {
          "200": { "description": "Healthy" }
        }
      }
    },
    "/import/metadata": {
      "get": {
        "operationId": "getImportMetadata",
        "summary": "List safe account and category metadata",
        "security": [{ "BudgetThingApiKey": [] }],
        "responses": {
          "200": { "description": "Safe metadata" },
          "401": { "description": "Unauthorized" }
        }
      }
    },
    "/imports": {
      "post": {
        "operationId": "createImports",
        "summary": "Create import inbox items",
        "description": "Creates pending import inbox items only. The user must confirm them in the app.",
        "x-openai-isConsequential": true,
        "security": [{ "BudgetThingApiKey": [] }],
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": true,
            "schema": { "type": "string" }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/CreateImportsRequest" }
            }
          }
        },
        "responses": {
          "200": { "description": "Queued for review" },
          "400": { "description": "Invalid request" },
          "401": { "description": "Unauthorized" },
          "409": { "description": "Idempotency conflict" },
          "429": { "description": "Rate limited" }
        }
      }
    },
    "/imports/status": {
      "get": {
        "operationId": "getImportStatus",
        "summary": "Check a submitted import item",
        "security": [{ "BudgetThingApiKey": [] }],
        "parameters": [
          {
            "name": "source",
            "in": "query",
            "required": true,
            "schema": { "type": "string" }
          },
          {
            "name": "externalId",
            "in": "query",
            "required": true,
            "schema": { "type": "string" }
          }
        ],
        "responses": {
          "200": { "description": "Import status" },
          "401": { "description": "Unauthorized" },
          "404": { "description": "Not found" }
        }
      }
    }
  }
}
