{
  "openapi": "3.1.0",
  "info": {
    "title": "AgentMD",
    "version": "0.1.0",
    "description": "Convert PDF, DOCX, HTML, and URLs to clean, LLM-ready markdown. Built for AI agents: headless, per-call pricing, also available as an MCP server at /api/mcp.",
    "license": { "name": "Proprietary" }
  },
  "servers": [{ "url": "https://www.getagentmd.com" }],
  "security": [{ "bearerAuth": [] }],
  "paths": {
    "/api/v1/convert": {
      "post": {
        "operationId": "convertDocument",
        "summary": "Convert a document or URL to markdown",
        "description": "Accepts a remote URL, a base64-encoded document, or a multipart file upload. The source format is detected automatically from magic bytes, MIME type, and filename.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "oneOf": [
                  { "$ref": "#/components/schemas/UrlRequest" },
                  { "$ref": "#/components/schemas/Base64Request" }
                ]
              },
              "examples": {
                "url": {
                  "summary": "Convert a URL",
                  "value": { "url": "https://example.com/report.pdf" }
                },
                "base64": {
                  "summary": "Convert base64 bytes",
                  "value": { "base64": "JVBERi0xLjQK...", "filename": "report.pdf" }
                }
              }
            },
            "multipart/form-data": {
              "schema": {
                "type": "object",
                "required": ["file"],
                "properties": {
                  "file": {
                    "type": "string",
                    "format": "binary",
                    "description": "The document to convert (max 25 MB)"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Conversion succeeded",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ConvertResult" }
              }
            }
          },
          "400": { "$ref": "#/components/responses/Error" },
          "401": { "$ref": "#/components/responses/Error" },
          "402": { "$ref": "#/components/responses/Error" },
          "413": { "$ref": "#/components/responses/Error" },
          "415": { "$ref": "#/components/responses/Error" },
          "422": { "$ref": "#/components/responses/Error" },
          "502": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/api/v1/keys/free": {
      "post": {
        "operationId": "createFreeKey",
        "summary": "Issue a free API key (no card required)",
        "description": "Self-serve key issuance for the free tier: 50 conversions per email, no payment method needed. The plaintext key is returned only at creation. When the quota is exhausted, conversion requests return HTTP 402 with an upgrade link.",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["email"],
                "properties": {
                  "email": { "type": "string", "format": "email" }
                }
              },
              "examples": {
                "signup": { "value": { "email": "you@example.com" } }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Key issued (shown only once)",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["api_key", "quota"],
                  "properties": {
                    "api_key": { "type": "string", "description": "The API key, e.g. amd_free_..." },
                    "quota": { "type": "integer", "description": "Free conversions included" },
                    "note": { "type": "string" },
                    "upgrade_url": { "type": "string" },
                    "docs": { "type": "string" }
                  }
                }
              }
            }
          },
          "400": { "$ref": "#/components/responses/Error" },
          "409": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/api/mcp": {
      "post": {
        "operationId": "mcpEndpoint",
        "summary": "MCP server (streamable HTTP)",
        "description": "Model Context Protocol endpoint exposing the tools convert_url_to_markdown and convert_document_to_markdown. Speaks JSON-RPC 2.0 over streamable HTTP; use an MCP client rather than calling directly.",
        "responses": {
          "200": { "description": "JSON-RPC response or SSE event stream" },
          "401": { "$ref": "#/components/responses/Error" }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "description": "API key issued at https://www.getagentmd.com (free tier: POST /api/v1/keys/free). Send as: Authorization: Bearer <key>"
      }
    },
    "schemas": {
      "UrlRequest": {
        "type": "object",
        "required": ["url"],
        "properties": {
          "url": {
            "type": "string",
            "format": "uri",
            "description": "http(s) URL of the page or document to convert"
          }
        }
      },
      "Base64Request": {
        "type": "object",
        "required": ["base64"],
        "properties": {
          "base64": { "type": "string", "description": "Base64-encoded file contents" },
          "filename": {
            "type": "string",
            "description": "Original filename; improves format detection"
          }
        }
      },
      "ConvertResult": {
        "type": "object",
        "required": ["markdown", "meta", "warnings"],
        "properties": {
          "markdown": { "type": "string", "description": "The converted markdown" },
          "meta": {
            "type": "object",
            "required": ["sourceFormat", "characters", "approxTokens", "durationMs"],
            "properties": {
              "sourceFormat": {
                "type": "string",
                "enum": ["pdf", "docx", "html", "markdown", "text"]
              },
              "pages": { "type": "integer", "description": "Page count (PDF only)" },
              "url": { "type": "string", "description": "Source URL, when converted from a URL" },
              "title": { "type": "string", "description": "Detected document title" },
              "characters": { "type": "integer" },
              "approxTokens": { "type": "integer", "description": "Rough token estimate (characters / 4)" },
              "durationMs": { "type": "integer" }
            }
          },
          "warnings": {
            "type": "array",
            "items": { "type": "string" },
            "description": "Non-fatal issues, e.g. a scanned PDF with no extractable text"
          }
        }
      },
      "Error": {
        "type": "object",
        "required": ["error"],
        "properties": { "error": { "type": "string" } }
      }
    },
    "responses": {
      "Error": {
        "description": "Request failed",
        "content": {
          "application/json": { "schema": { "$ref": "#/components/schemas/Error" } }
        }
      }
    }
  }
}
