{
  "openapi": "3.0.3",
  "info": {
    "title": "User Flow Library Ingest API",
    "description": "Endpoints used by coding agents to build a UFL project: device auth, project creation, route ingestion, organization, and share links. Auth: Bearer ufl_mcp_ token.",
    "version": "1.0.0"
  },
  "servers": [
    { "url": "https://www.userflowlibrary.com", "description": "Web app + API" },
    { "url": "https://mcp.userflowlibrary.com", "description": "MCP auth server" }
  ],
  "paths": {
    "/api/account/usage": {
      "get": {
        "summary": "Check account-wide AI allowance and confirmed billing entitlement",
        "security": [{ "bearerAuth": [] }],
        "parameters": [{
          "name": "projectId", "in": "query", "required": false,
          "schema": { "type": "string", "format": "uuid" },
          "description": "Authorized project for the upgrade return link"
        }],
        "responses": {
          "200": { "description": "allowed, usedCount, maxCount, remainingCount, plan, scope, upgradeUrl, upgradeAvailable, status, retryAfterSeconds. Paid entitlement comes from Clerk, not a checkout redirect." },
          "401": { "description": "Real account authentication required" },
          "503": { "description": "Billing or usage verification unavailable; do not render" }
        }
      },
      "post": {
        "summary": "Reserve screen-generation attempts for hosted MCP rendering",
        "security": [{ "bearerAuth": [] }],
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": {
            "type": "object", "required": ["count"],
            "properties": { "count": { "type": "integer", "minimum": 1, "maximum": 40 } }
          } } }
        },
        "responses": {
          "200": { "description": "Allowance reserved for the authenticated account; failed or uncertain attempts retain their reservation" },
          "402": { "description": "AI_QUOTA_EXCEEDED with upgradeUrl, or BILLING_SETUP_REQUIRED when no supported paid plan exists. No provider work is authorized." },
          "503": { "description": "Billing or usage unavailable" }
        }
      }
    },
    "/device_authorization": {
      "post": {
        "summary": "Start OAuth device flow",
        "servers": [{ "url": "https://mcp.userflowlibrary.com" }],
        "responses": { "200": { "description": "device_code, user_code, verification_uri_complete, interval" } }
      }
    },
    "/token": {
      "post": {
        "summary": "Poll for access token (device_code grant)",
        "servers": [{ "url": "https://mcp.userflowlibrary.com" }],
        "requestBody": {
          "required": true,
          "content": { "application/x-www-form-urlencoded": { "schema": { "type": "object", "properties": { "grant_type": { "type": "string", "enum": ["urn:ietf:params:oauth:grant-type:device_code"] }, "device_code": { "type": "string" } }, "required": ["grant_type", "device_code"] } } }
        },
        "responses": { "200": { "description": "{ access_token: ufl_mcp_… } or { error: authorization_pending }" } }
      }
    },
    "/api/projects": {
      "post": {
        "summary": "Create a project",
        "security": [{ "bearerAuth": [] }],
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": { "type": "object", "properties": { "name": { "type": "string" }, "platform_type": { "type": "string", "default": "web" } }, "required": ["name"] } } }
        },
        "responses": { "200": { "description": "Created project including id" } }
      }
    },
    "/api/projects/{id}/ingest-screens": {
      "post": {
        "summary": "Render agent-supplied routes into screens (≤40 routes/call)",
        "security": [{ "bearerAuth": [] }],
        "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "string", "format": "uuid" } }],
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": {
            "type": "object",
            "properties": {
              "routes": {
                "type": "array", "minItems": 1, "maxItems": 40,
                "items": {
                  "type": "object",
                  "properties": {
                    "path": { "type": "string" },
                    "title": { "type": "string" },
                    "source": { "type": "string", "description": "Verbatim route component source, ≤150KB" },
                    "sourceFile": { "type": "string" },
                    "framework": { "type": "string" },
                    "components": { "type": "object", "additionalProperties": { "type": "string" } },
                    "styles": { "type": "object", "additionalProperties": { "type": "string" } },
                    "content": { "type": "object", "additionalProperties": { "type": "string" } },
                    "assets": { "type": "object", "additionalProperties": { "type": "string" } }
                  },
                  "required": ["path", "title", "source"]
                }
              },
              "flowId": { "type": "string", "format": "uuid", "description": "Existing flow in this project; omit for a fresh import flow" },
              "flowName": { "type": "string" }
            },
            "required": ["routes"]
          } } }
        },
        "responses": {
          "200": { "description": "{ success, flowId, screens[], renderedCount, failedCount, projectUrl }" },
          "402": { "description": "AI quota exceeded (free tier: 25 renders)" }
        }
      }
    },
    "/api/projects/{id}/organize": {
      "post": {
        "summary": "Group rendered screens into named user journeys",
        "security": [{ "bearerAuth": [] }],
        "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "string", "format": "uuid" } }],
        "responses": { "200": { "description": "Journeys + mermaid graphs" } }
      }
    },
    "/api/projects/{id}/share": {
      "get": {
        "summary": "Mint or reuse a public share link",
        "security": [{ "bearerAuth": [] }],
        "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "string", "format": "uuid" } }],
        "responses": { "200": { "description": "{ shareUrl }" } }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": { "type": "http", "scheme": "bearer", "description": "ufl_mcp_ token from the device flow" }
    }
  }
}
