{"openapi":"3.1.0","info":{"title":"Signature Detection API","version":"1.0.0","summary":"Detect handwritten signatures in an image or a PDF.","description":"HTTP API implemented by `POST /api/detect`.\n\nSend a JSON object with exactly one of `imageUrl`, `imageBase64`, `pdfUrl`, or `pdfBase64`. A PDF placed in an image field is detected from the `%PDF` header or `Content-Type`. A body that is not valid JSON yields 400.\n\nImages cost 1 credit. PDFs cost 1 credit per page: a 5-page PDF costs 5 credits. The balance is checked against the full page count before any page is rasterized or sent to the model. If the balance is short, the API returns 402 and does not process or charge any page. A repeated document is cached as a whole file and costs 0 credits (`cached: true`, `creditsUsed: 0`).\n\nImages accept an optional `threshold` from 0 to 1 (default 0.1). PDFs use 0.1. Authenticate with the `x-api-key` header. A signed-in Clerk browser session is also accepted when that header is omitted. If both are present, the API key is used.","contact":{"name":"Signature Detector","url":"https://signature-detector.com/docs"}},"externalDocs":{"description":"API documentation","url":"https://signature-detector.com/docs"},"servers":[{"url":"https://signature-detector.com","description":"Production"}],"tags":[{"name":"Detection","description":"Signature detection"}],"paths":{"/api/detect":{"post":{"operationId":"detectSignatures","tags":["Detection"],"summary":"Detect signatures in an image or PDF","description":"Detect signatures in exactly one image or PDF source. An image costs 1 credit. A PDF costs 1 credit per page. A cached document costs 0 credits. A PDF balance check returns 402 before any page is processed when the account cannot cover every page.","security":[{"apiKey":[]},{"clerkSession":[]}],"requestBody":{"required":true,"content":{"application/json":{"schema":{"$ref":"#/components/schemas/DetectionRequest"},"examples":{"imageUrl":{"summary":"Public image URL","value":{"imageUrl":"https://example.com/document.jpg"}},"imageBase64":{"summary":"Base64-encoded image","value":{"imageBase64":"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="}},"pdfUrl":{"summary":"PDF URL. Costs 1 credit per page.","value":{"pdfUrl":"https://example.com/contract.pdf"}},"pdfBase64":{"summary":"Base64-encoded PDF. Costs 1 credit per page.","value":{"pdfBase64":"JVBERi0xLjEKJeLjz9MKMSAwIG9iago8PC9UeXBlL0NhdGFsb2c+PgplbmRvYmoK"}}}}}},"responses":{"200":{"description":"Detection succeeded. Image results use `result`. PDF results put `pages`, `count`, and `creditsUsed` at the top level. `detectionId` is omitted when the result was computed but could not be stored. `cached` is present only for a cache hit, and is then true.","content":{"application/json":{"schema":{"oneOf":[{"$ref":"#/components/schemas/DetectionSuccess"},{"$ref":"#/components/schemas/PdfDetectionSuccess"}]},"examples":{"fresh":{"summary":"Fresh detection","value":{"success":true,"result":{"detections":[{"bbox":{"x1":321.23,"y1":373.39,"x2":647.21,"y2":501.68},"confidence":0.192,"class":0,"class_name":"signature"}],"count":1,"confidenceThreshold":0.1},"creditsRemaining":9999,"detectionId":"clxxxx1234567890"}},"cached":{"summary":"Cached detection (no credit spent)","value":{"success":true,"result":{"detections":[{"bbox":{"x1":321.23,"y1":373.39,"x2":647.21,"y2":501.68},"confidence":0.192,"class":0,"class_name":"signature"}],"count":1,"confidenceThreshold":0.1},"creditsRemaining":9999,"detectionId":"clxxxx1234567890","cached":true}},"pdf":{"summary":"Two-page PDF. creditsUsed equals the page count.","value":{"success":true,"documentType":"pdf","signed":true,"count":1,"pageCount":2,"confidenceThreshold":0.1,"pages":[{"page":1,"width":1275,"height":1650,"detections":[{"bbox":{"x1":120.5,"y1":980.2,"x2":460.8,"y2":1104},"confidence":0.91,"class":0,"class_name":"signature"}],"count":1},{"page":2,"width":1275,"height":1650,"detections":[],"count":0}],"creditsUsed":2,"creditsRemaining":98,"detectionId":"clxxxxpdf"}}}}}},"400":{"description":"The body is not valid JSON, does not contain exactly one source field, or the image or PDF cannot be read. A PDF over the 20-page limit is also 400. No credits are charged.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/BadRequestError"},"examples":{"missingImage":{"summary":"Neither image source was provided","value":{"error":"Either imageUrl or imageBase64 is required"}},"bothImages":{"summary":"Both image sources were provided","value":{"error":"Cannot provide both imageUrl and imageBase64. Please provide only one."}},"invalidUrl":{"summary":"imageUrl is not an absolute URL","value":{"error":"Invalid image URL format"}},"invalidBase64":{"summary":"imageBase64 is empty or not a string","value":{"error":"Invalid imageBase64 format"}},"invalidJson":{"summary":"Body is not JSON","value":{"error":"Request body must be valid JSON."}},"tooManyPages":{"summary":"PDF has more than 20 pages","value":{"error":"This PDF has 21 pages, which exceeds the maximum of 20 pages.","code":"too_many_pages","pageCount":21,"maxPages":20}}}}}},"401":{"description":"The API key is missing or unknown, or the request has neither an API key nor a signed-in Clerk session.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/UnauthorizedError"},"examples":{"invalidApiKey":{"summary":"Unknown or inactive API key","value":{"error":"Invalid API key"}},"noSession":{"summary":"No API key and no Clerk session","value":{"error":"Unauthorized. Please sign in to use the API."}}}}}},"402":{"description":"The account cannot pay for this request. An image needs at least 1 credit. A PDF needs at least one credit per page. The PDF check happens before rasterizing or calling the model, and nothing is charged.","content":{"application/json":{"schema":{"oneOf":[{"$ref":"#/components/schemas/InsufficientCreditsError"},{"$ref":"#/components/schemas/PdfInsufficientCreditsError"}]},"examples":{"noCredits":{"summary":"Image request with an empty balance","value":{"error":"Insufficient credits. Please purchase more credits to continue.","credits":0,"needsPurchase":true}},"pdf":{"summary":"5-page PDF with only 4 credits","value":{"error":"Insufficient credits. This PDF has 5 pages and costs 5 credits (1 per page). You have 4.","code":"insufficient_credits","credits":4,"creditsRequired":5,"pageCount":5,"needsPurchase":true}}}}}},"413":{"description":"The decoded PDF is larger than 20 MB. No credits are charged.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/PdfTooLargeError"},"examples":{"tooLarge":{"value":{"error":"This PDF exceeds the maximum file size of 20 MB.","code":"pdf_too_large","maxBytes":20971520}}}}}},"500":{"description":"The server could not complete the request. A detection-service failure other than a non-OK HTTP status, or an unexpected error, uses this status. When the detection service fails this way, credits deducted for the attempt are refunded. A body that is not JSON is 400, not 500.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ServerError"},"examples":{"account":{"summary":"Clerk user could not be created","value":{"error":"Failed to create user account. Please try again."}},"invalidServiceResponse":{"summary":"Detection service failed or returned invalid JSON","value":{"error":"Invalid response from detection service"}},"unexpected":{"summary":"Unexpected error","value":{"error":"An unexpected error occurred. Please try again later.","details":"database down"}}}}}},"503":{"description":"The detection service responded with a non-OK HTTP status. The credit deducted for the attempt is refunded.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ServiceUnavailableError"},"examples":{"unavailable":{"value":{"error":"Failed to connect to detection service. Please try again."}}}}}}}}}},"components":{"securitySchemes":{"apiKey":{"type":"apiKey","in":"header","name":"x-api-key","description":"API key created in the dashboard. Keys use the prefix `sd_`."},"clerkSession":{"type":"apiKey","in":"cookie","name":"__session","description":"Clerk session cookie used by the signed-in web app when `x-api-key` is omitted. Programmatic clients should send `x-api-key` instead."}},"schemas":{"DetectionRequest":{"type":"object","additionalProperties":true,"description":"Exactly one of `imageUrl`, `imageBase64`, `pdfUrl`, or `pdfBase64` must be present. A body that includes more than one of those fields matches more than one alternative below, so it is invalid. An empty string is treated as omitted. `threshold` is optional and applies to images.","properties":{"imageUrl":{"type":"string","format":"uri","minLength":1,"description":"Absolute URL of an image. A PDF at this URL is detected automatically. An empty string is treated as omitted."},"imageBase64":{"type":"string","minLength":1,"description":"Image payload, raw base64 or a data URL. PDF magic bytes (`%PDF`) are detected automatically. An empty string is treated as omitted."},"pdfUrl":{"type":"string","format":"uri","minLength":1,"description":"Absolute http(s) URL of a PDF. Costs 1 credit per page."},"pdfBase64":{"type":"string","minLength":1,"description":"PDF payload, raw base64 or a data:application/pdf;base64 URL. Costs 1 credit per page."},"threshold":{"type":"number","minimum":0,"maximum":1,"description":"Optional image confidence cutoff. Defaults to 0.1. PDFs use 0.1."}},"oneOf":[{"required":["imageUrl"]},{"required":["imageBase64"]},{"required":["pdfUrl"]},{"required":["pdfBase64"]}]},"BoundingBox":{"type":"object","additionalProperties":true,"required":["x1","y1","x2","y2"],"properties":{"x1":{"type":"number","description":"Left edge, in image pixels."},"y1":{"type":"number","description":"Top edge, in image pixels."},"x2":{"type":"number","description":"Right edge, in image pixels."},"y2":{"type":"number","description":"Bottom edge, in image pixels."}}},"SignatureDetection":{"type":"object","additionalProperties":true,"required":["bbox","confidence","class","class_name"],"properties":{"bbox":{"$ref":"#/components/schemas/BoundingBox"},"confidence":{"type":"number","description":"Model confidence for this detection, from 0 to 1."},"class":{"type":"integer","description":"Numeric class id. `0` is a signature."},"class_name":{"type":"string","description":"Class label. Signature detections use `signature`.","examples":["signature"]}}},"DetectionResult":{"type":"object","additionalProperties":true,"description":"Detection payload returned by the detection service and stored for later cache hits. The server forwards this object without reshaping it.","required":["detections","count","confidenceThreshold"],"properties":{"detections":{"type":"array","items":{"$ref":"#/components/schemas/SignatureDetection"}},"count":{"type":"integer","minimum":0,"description":"Number of detections. Matches `detections.length` for responses produced by the detection service."},"confidenceThreshold":{"type":"number","description":"Threshold applied by the server. Always 0.1 for detections created by this API."}}},"DetectionSuccess":{"type":"object","additionalProperties":false,"required":["success","result","creditsRemaining"],"properties":{"success":{"type":"boolean","const":true},"result":{"$ref":"#/components/schemas/DetectionResult"},"creditsRemaining":{"type":"integer","description":"Credit balance after this request. Unchanged when `cached` is true."},"detectionId":{"type":"string","description":"Id of the stored detection row. Omitted when storage fails after a successful detection."},"cached":{"type":"boolean","const":true,"description":"Present and true when the result was read from cache. Omitted for a fresh detection."}}},"BadRequestError":{"type":"object","additionalProperties":false,"required":["error"],"description":"Image requests use the historical messages (`Either imageUrl or imageBase64 is required`, `Cannot provide both imageUrl and imageBase64. Please provide only one.`, `Invalid image URL format`, `Invalid imageBase64 format`, `Request body must be valid JSON.`). PDF requests add `code` (`invalid_pdf`, `encrypted_pdf`, `empty_pdf`, `too_many_pages`, `invalid_pdf_url`, `pdf_fetch_failed`, `invalid_body`, `missing_input`) and, for the page limit, `pageCount` and `maxPages`.","properties":{"error":{"type":"string"},"code":{"type":"string"},"pageCount":{"type":"integer","minimum":0},"maxPages":{"type":"integer"}}},"PdfPage":{"type":"object","additionalProperties":false,"required":["page","width","height","detections","count"],"properties":{"page":{"type":"integer","minimum":1,"description":"1-based page number."},"width":{"type":"integer","minimum":1,"description":"Raster width in pixels."},"height":{"type":"integer","minimum":1,"description":"Raster height in pixels."},"detections":{"type":"array","items":{"$ref":"#/components/schemas/SignatureDetection"}},"count":{"type":"integer","minimum":0,"description":"Signatures on this page."}}},"PdfDetectionSuccess":{"type":"object","additionalProperties":false,"required":["success","documentType","signed","count","pageCount","confidenceThreshold","pages","creditsUsed","creditsRemaining"],"properties":{"success":{"type":"boolean","const":true},"documentType":{"type":"string","const":"pdf"},"signed":{"type":"boolean","description":"True when count, across every page, is greater than zero."},"count":{"type":"integer","minimum":0,"description":"Signatures on all pages."},"pageCount":{"type":"integer","minimum":1},"confidenceThreshold":{"type":"number","const":0.1},"pages":{"type":"array","items":{"$ref":"#/components/schemas/PdfPage"}},"creditsUsed":{"type":"integer","minimum":0,"description":"Credits charged for this request. Equal to pageCount for a fresh PDF, and 0 when cached."},"creditsRemaining":{"type":"integer"},"detectionId":{"type":"string"},"cached":{"type":"boolean","const":true}}},"PdfInsufficientCreditsError":{"type":"object","additionalProperties":false,"required":["error","code","credits","creditsRequired","pageCount","needsPurchase"],"properties":{"error":{"type":"string"},"code":{"type":"string","const":"insufficient_credits"},"credits":{"type":"integer","description":"Current credit balance."},"creditsRequired":{"type":"integer","minimum":1,"description":"Page count. One credit per page."},"pageCount":{"type":"integer","minimum":1},"needsPurchase":{"type":"boolean","const":true}}},"PdfTooLargeError":{"type":"object","additionalProperties":false,"required":["error","code","maxBytes"],"properties":{"error":{"type":"string"},"code":{"type":"string","const":"pdf_too_large"},"maxBytes":{"type":"integer","const":20971520}}},"UnauthorizedError":{"type":"object","additionalProperties":false,"required":["error"],"properties":{"error":{"type":"string","enum":["Invalid API key","Unauthorized. Please sign in to use the API."]}}},"InsufficientCreditsError":{"type":"object","additionalProperties":false,"required":["error","credits","needsPurchase"],"properties":{"error":{"type":"string","const":"Insufficient credits. Please purchase more credits to continue."},"credits":{"type":"integer","description":"Current credit balance."},"needsPurchase":{"type":"boolean","const":true}}},"ServerError":{"oneOf":[{"$ref":"#/components/schemas/AccountCreationError"},{"$ref":"#/components/schemas/InvalidServiceResponseError"},{"$ref":"#/components/schemas/UnexpectedError"}]},"AccountCreationError":{"type":"object","additionalProperties":false,"required":["error"],"properties":{"error":{"type":"string","const":"Failed to create user account. Please try again."}}},"InvalidServiceResponseError":{"type":"object","additionalProperties":false,"required":["error"],"properties":{"error":{"type":"string","const":"Invalid response from detection service"}}},"UnexpectedError":{"type":"object","additionalProperties":false,"required":["error","details"],"properties":{"error":{"type":"string","const":"An unexpected error occurred. Please try again later."},"details":{"type":"string","description":"Exception message. For a body that is not JSON, this is the parser error."}}},"ServiceUnavailableError":{"type":"object","additionalProperties":false,"required":["error"],"properties":{"error":{"type":"string","const":"Failed to connect to detection service. Please try again."}}}}}}