> ## Documentation Index
> Fetch the complete documentation index at: https://docs.usepatchwork.co/llms.txt
> Use this file to discover all available pages before exploring further.

# Extract structured data from a document

> Send a document and a schema; get typed fields back. Asynchronous: returns `202` with a request id by default. Pass `?wait` to hold for a quick result.

Don't know the document type? Send `template: "auto"` and we match it against the schema types in your workspace, then fill the one that fits.

Charged per page: 4 credits a page to extract, plus 1 credit a page when you use `auto`. An `auto` call that matches nothing is charged the 1 credit a page for the read. Unreadable or encrypted documents and the `202` accept are not billed.




## OpenAPI

````yaml /openapi.yaml post /v1/extract
openapi: 3.1.0
info:
  title: Patchwork API
  version: 1.0.0
  description: >
    Public data-plane API. Two products today: Identify (bare account
    resolution) and Loom (threads, runs, and outcomes). Every response is a JSON
    envelope — `status: success` with `data`, or `status: error` with `error`.
  contact:
    url: https://usepatchwork.co
servers:
  - url: https://api.usepatchwork.co
security:
  - bearerAuth: []
tags:
  - name: Identify
    description: Resolve an account or wallet to the institution and rails behind it.
  - name: Extract
    description: >-
      Turn a document into structured data, and manage the schema types that
      shape it.
  - name: Loom
    description: Threads, messages, runs, and outcomes for an embedded agent.
  - name: Files
    description: Upload bytes once, get a durable handle, reference it from any product.
  - name: Voice
    description: Transcribe an utterance, speak a reply, list the available voices.
  - name: MCP
    description: >-
      The grants your users hold when they reach an agent from their own AI
      client.
paths:
  /v1/extract:
    post:
      tags:
        - Extract
      summary: Extract structured data from a document
      description: >
        Send a document and a schema; get typed fields back. Asynchronous:
        returns `202` with a request id by default. Pass `?wait` to hold for a
        quick result.


        Don't know the document type? Send `template: "auto"` and we match it
        against the schema types in your workspace, then fill the one that fits.


        Charged per page: 4 credits a page to extract, plus 1 credit a page when
        you use `auto`. An `auto` call that matches nothing is charged the 1
        credit a page for the read. Unreadable or encrypted documents and the
        `202` accept are not billed.
      operationId: extract
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
        - in: query
          name: wait
          required: false
          schema:
            type: string
          description: >
            Omit for an immediate `202`. `true`/`1` holds until done or the 25s
            deadline; a number holds `min(n, 25s)`. Finished within the hold →
            `200` with the result; otherwise `202`.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ExtractRequest'
            examples:
              template:
                summary: Against a schema type
                value:
                  document:
                    media_type: application/pdf
                    content: <base64>
                  template: sch_8f2c...
              auto:
                summary: Let us match the document type
                value:
                  document:
                    media_type: application/pdf
                    content: <base64>
                  template: auto
              inline:
                summary: Inline schema
                value:
                  document:
                    media_type: image/png
                    content: <base64>
                  schema:
                    type: object
                    additionalProperties: false
                    properties:
                      total:
                        type: number
                        description: Grand total
                    required:
                      - total
      responses:
        '200':
          description: Completed within the wait deadline.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/EnvelopeExtractResult'
        '202':
          description: Accepted. Work continues off the request.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/EnvelopeExtractAccepted'
        '400':
          description: Bad document, schema, or parameter.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '401':
          description: Missing or invalid API key.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '402':
          description: Workspace is out of credits.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '429':
          description: Rate limited.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
components:
  parameters:
    IdempotencyKey:
      name: Idempotency-Key
      in: header
      required: false
      description: >
        Makes this POST safe to retry. The first call does the work and its
        response is stored; a retry with the same key returns that stored
        response with `Idempotency-Replayed: true`, without charging again. Same
        key with a different body is rejected. Keys expire after 24 hours.
      schema:
        type: string
        maxLength: 255
  schemas:
    ExtractRequest:
      type: object
      required:
        - document
      description: >-
        Pass `template`, `schema`, or both — an inline `schema` overlays the
        type.
      properties:
        document:
          $ref: '#/components/schemas/Document'
        template:
          type: string
          description: >
            A saved schema type id (`sch_…`) to fill against, or `auto` to let
            us pick. With `auto` we read the document, match it against the
            schema types in your workspace, fill the one that fits, and return
            the match and our confidence. Nothing fits → `result` is
            `unmatched`.
        schema:
          type: object
          description: >-
            An inline restricted JSON Schema. Sent alone, or as an overlay
            merged onto `template`.
    EnvelopeExtractResult:
      type: object
      required:
        - status
        - data
      properties:
        status:
          type: string
          const: success
        data:
          $ref: '#/components/schemas/ExtractResult'
    EnvelopeExtractAccepted:
      type: object
      required:
        - status
        - data
      properties:
        status:
          type: string
          const: success
        data:
          $ref: '#/components/schemas/RequestAccepted'
    Error:
      type: object
      required:
        - status
        - error
      properties:
        status:
          type: string
          const: error
        error:
          type: object
          required:
            - message
            - code
          properties:
            message:
              type: string
              description: Human-readable explanation.
            code:
              type: string
              description: Stable machine-readable code.
            details:
              description: Optional extra context.
    Document:
      type: object
      required:
        - media_type
        - content
      properties:
        media_type:
          type: string
          enum:
            - application/pdf
            - image/jpeg
            - image/png
            - image/webp
        content:
          type: string
          description: >-
            Base64-encoded document bytes. At most 10 MiB; PDFs at most 15
            pages.
    ExtractResult:
      type: object
      required:
        - result
        - document
      properties:
        result:
          type: string
          enum:
            - extracted
            - unreadable
            - unmatched
          description: >
            `extracted` — fields are below. `unreadable` — a legal file we will
            not stand behind. `unmatched` — only from `template: "auto"`: the
            document was readable but no schema type in your workspace fits it.
        document:
          type: object
          properties:
            media_type:
              type: string
            pages:
              type: integer
              description: Page count. Images are 1. Billing is charged per page.
        template:
          type: string
          description: >-
            Schema type id used, when one was. Absent when `result` is
            `unmatched`.
        match:
          type: object
          description: >
            Present only when you sent `template: "auto"`. How we chose the
            schema type, and what else it could have been.
          properties:
            name:
              type: string
              description: >-
                Name of the matched schema type. Absent when `result` is
                `unmatched`.
            confidence:
              type: string
              enum:
                - high
                - medium
                - low
              description: >
                How sure we are of this match. A level, not a score — route on
                `low` rather than on a number we would have to keep stable.
            alternatives:
              type: array
              description: The schema types we considered and rejected, best first.
              items:
                type: object
                properties:
                  template:
                    type: string
                  name:
                    type: string
                  confidence:
                    type: string
                    enum:
                      - high
                      - medium
                      - low
        fields:
          type: object
          description: >-
            Extracted values, shaped by your schema. Present when `result` is
            `extracted`.
        missing:
          type: array
          items:
            type: string
          description: >-
            Declared fields that could not be filled. Present when `result` is
            `extracted`.
        extracted_at:
          type: string
          format: date-time
          description: Present when `result` is `extracted`.
    RequestAccepted:
      type: object
      required:
        - id
        - product
        - state
      properties:
        id:
          type: string
          description: Request id (`req_…`).
        product:
          type: string
        state:
          type: string
          const: pending
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: >
        Identify accepts a workspace API key (`sk_…`). Loom accepts either a
        short-lived session token (direct) or a workspace API key (relay).

````

This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.