{
  "openapi": "3.1.0",
  "info": {
    "title": "RagScrape API",
    "version": "2.1.0",
    "summary": "URL → RAG-ready Markdown, chunks, embeddings, llms.txt",
    "description": "One API call turns any public URL into clean, LLM-ready Markdown. Options add heading-aligned chunks, 384-dim embeddings, token slimming and change-aware re-indexing. Includes a same-host docs crawler that generates llms.txt. Free tier: 50 requests/month (POST /register).",
    "termsOfService": "https://rag-scrape-api.owerryking.workers.dev/",
    "contact": {
      "name": "RagScrape",
      "url": "https://rag-scrape-api.owerryking.workers.dev/",
      "email": "Owerryking@gmail.com"
    },
    "license": {
      "name": "MIT",
      "url": "https://github.com/owerryking-beep/rag-scrape"
    }
  },
  "servers": [
    {
      "url": "https://rag-scrape-api.owerryking.workers.dev"
    }
  ],
  "security": [
    {
      "bearerAuth": []
    }
  ],
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "description": "API key from POST /register (rsk_…)"
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "properties": {
          "success": {
            "type": "boolean",
            "const": false
          },
          "error": {
            "type": "object",
            "properties": {
              "code": {
                "type": "string"
              },
              "message": {
                "type": "string"
              },
              "details": {
                "type": "string"
              }
            },
            "required": [
              "code",
              "message"
            ]
          }
        },
        "required": [
          "success",
          "error"
        ]
      }
    }
  },
  "paths": {
    "/scrape": {
      "post": {
        "summary": "URL → Markdown (+chunks, +embeddings, +change detection)",
        "operationId": "scrape",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "url"
                ],
                "properties": {
                  "url": {
                    "type": "string",
                    "description": "http(s) URL to scrape"
                  },
                  "chunk": {
                    "type": "boolean",
                    "description": "Return heading-aligned chunks[]"
                  },
                  "chunkSize": {
                    "type": "number",
                    "default": 4000,
                    "description": "200–16000 max chars per chunk"
                  },
                  "embed": {
                    "type": "boolean",
                    "description": "Also embed every chunk (384-dim). Implies chunk."
                  },
                  "stripLinks": {
                    "type": "boolean",
                    "description": "Unwrap links, keep text"
                  },
                  "stripImages": {
                    "type": "boolean",
                    "description": "Drop images, keep alt text"
                  },
                  "ifNoneHash": {
                    "type": "string",
                    "description": "contentHash from a previous response → unchanged pages return {unchanged:true}"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Scraped content (or unchanged:true short-circuit)",
            "headers": {
              "ETag": {
                "description": "contentHash — send back as ifNoneHash",
                "schema": {
                  "type": "string"
                }
              },
              "X-RateLimit-Remaining": {
                "schema": {
                  "type": "string"
                }
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "success": {
                      "type": "boolean",
                      "const": true
                    },
                    "markdown": {
                      "type": "string"
                    },
                    "metadata": {
                      "type": "object"
                    },
                    "chunks": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "index": {
                            "type": "number"
                          },
                          "content": {
                            "type": "string"
                          },
                          "headingPath": {
                            "type": "array",
                            "items": {
                              "type": "string"
                            }
                          },
                          "charCount": {
                            "type": "number"
                          },
                          "embedding": {
                            "type": "array",
                            "items": {
                              "type": "number"
                            },
                            "description": "384-dim when embed:true"
                          }
                        }
                      }
                    },
                    "contentHash": {
                      "type": "string"
                    },
                    "unchanged": {
                      "type": "boolean"
                    },
                    "embedError": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Bad/missing key",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "402": {
            "description": "Monthly quota exhausted",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Inactive key or blocked URL",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Demo quota exceeded",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/crawl": {
      "post": {
        "summary": "Crawl a docs site → pages[] + llms.txt (≤100 pages, 1 unit/page)",
        "operationId": "crawl",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "url"
                ],
                "properties": {
                  "url": {
                    "type": "string"
                  },
                  "maxPages": {
                    "type": "number",
                    "default": 20,
                    "description": "1–100"
                  },
                  "includePaths": {
                    "type": "array",
                    "items": {
                      "type": "string"
                    }
                  },
                  "excludePaths": {
                    "type": "array",
                    "items": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Crawl result"
          },
          "401": {
            "description": "Bad/missing key"
          },
          "402": {
            "description": "Quota exhausted"
          },
          "403": {
            "description": "Demo key may not crawl"
          }
        }
      }
    },
    "/llms-txt": {
      "post": {
        "summary": "Generate llms.txt for a site (no page payloads)",
        "operationId": "llmsTxt",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "url"
                ],
                "properties": {
                  "url": {
                    "type": "string"
                  },
                  "maxPages": {
                    "type": "number",
                    "default": 20
                  },
                  "includePaths": {
                    "type": "array",
                    "items": {
                      "type": "string"
                    }
                  },
                  "excludePaths": {
                    "type": "array",
                    "items": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "llms.txt content"
          },
          "403": {
            "description": "Demo key may not crawl"
          }
        }
      }
    },
    "/register": {
      "post": {
        "summary": "Register a free API key (50 req/mo, no card)",
        "operationId": "register",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "email"
                ],
                "properties": {
                  "email": {
                    "type": "string",
                    "format": "email"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Key created"
          },
          "400": {
            "description": "Invalid email"
          }
        }
      }
    },
    "/create-checkout": {
      "post": {
        "summary": "Create a hosted subscription checkout ($9 / $19 / $49 per month)",
        "operationId": "createCheckout",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "email"
                ],
                "properties": {
                  "email": {
                    "type": "string",
                    "format": "email"
                  },
                  "apiKey": {
                    "type": "string",
                    "description": "Existing key to upgrade"
                  },
                  "plan": {
                    "type": "string",
                    "enum": [
                      "starter",
                      "pro",
                      "unlimited"
                    ],
                    "default": "pro"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Hosted checkout URL"
          },
          "400": {
            "description": "Invalid plan/email"
          }
        }
      }
    },
    "/crypto/claim": {
      "post": {
        "summary": "Claim a USDC payment (autonomous agent rail, Base network)",
        "operationId": "cryptoClaim",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "txHash"
                ],
                "properties": {
                  "txHash": {
                    "type": "string"
                  },
                  "apiKey": {
                    "type": "string"
                  },
                  "email": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Payment verified on-chain; upgraded API key returned"
          },
          "400": {
            "description": "Failed/underpaid/invalid transaction"
          },
          "404": {
            "description": "Transaction not found/pending"
          },
          "409": {
            "description": "Hash already claimed"
          },
          "503": {
            "description": "USDC rail not configured"
          }
        }
      }
    },
    "/pay": {
      "get": {
        "summary": "USDC payment desk (add ?format=json for machine-readable address + amounts)",
        "operationId": "payDesk",
        "security": [],
        "parameters": [
          {
            "name": "format",
            "in": "query",
            "schema": {
              "type": "string",
              "enum": [
                "json"
              ]
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Address and amounts (or HTML desk)"
          },
          "503": {
            "description": "USDC rail not configured"
          }
        }
      }
    },
    "/health": {
      "get": {
        "summary": "Liveness",
        "operationId": "health",
        "security": [],
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    }
  }
}