> For the complete documentation index, see [llms.txt](https://docs.cortecs.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cortecs.ai/examples/ocr.md).

# OCR \[BETA]

**OCR** extracts text and structured content from documents and images, enabling applications such as document digitization, data extraction, searchable archives, and automated document processing.

To explore available models, visit [cortecs.ai](https://cortecs.ai/serverlessModels?tags=Image-Gen) and filter by the **OCR** tag.

### Example usage

{% tabs %}
{% tab title="Python" %}

```python
import base64
import requests

api_key = "<CORTECS_API_KEY>"
pdf_path = "document.pdf"

with open(pdf_path, "rb") as file:
    encoded_pdf = base64.b64encode(file.read()).decode("utf-8")

response = requests.post(
    "https://api.cortecs.ai/v1/ocr",
    headers={
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json",
    },
    json={
        "model": "mistral-ocr-4.1",
        "document": {
            "type": "document_url",
            "document_url": f"data:application/pdf;base64,{encoded_pdf}",
        },
    },
)

response.raise_for_status()
result = response.json()

for page in result["pages"]:
    print(page["markdown"])
```

{% endtab %}

{% tab title="Node.js" %}

```javascript
import fs from "node:fs";

const apiKey = "<CORTECS_API_KEY>";
const pdf = fs.readFileSync("document.pdf").toString("base64");

const response = await fetch("https://api.cortecs.ai/v1/ocr", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${apiKey}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    model: "mistral-ocr-4.1",
    document: {
      type: "document_url",
      document_url: `data:application/pdf;base64,${pdf}`,
    },
  }),
});

if (!response.ok) {
  throw new Error(`OCR request failed: ${response.status} ${await response.text()}`);
}

const result = await response.json();

for (const page of result.pages) {
  console.log(page.markdown);
}
```

{% endtab %}

{% tab title="Curl" %}

```bash
PDF_BASE64="$(base64 < document.pdf | tr -d '\n')"

curl 'https://api.cortecs.ai/v1/ocr' \
  -H 'Authorization: Bearer <CORTECS_API_KEY>' \
  -H 'Content-Type: application/json' \
  -d "{
    \"model\": \"mistral-ocr-4.1\",
    \"document\": {
      \"type\": \"document_url\",
      \"document_url\": \"data:application/pdf;base64,${PDF_BASE64}\"
    }
  }"
```

{% endtab %}
{% endtabs %}

### OCR with document annotation

{% tabs %}
{% tab title="Python" %}

```python
import base64
import requests

api_key = "<CORTECS_API_KEY>"
pdf_path = "document.pdf"

with open(pdf_path, "rb") as file:
    encoded_pdf = base64.b64encode(file.read()).decode("utf-8")

response = requests.post(
    "https://api.cortecs.ai/v1/ocr",
    headers={
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json",
    },
    json={
        "model": "mistral-ocr-4.1",
        "document": {
            "type": "document_url",
            "document_url": f"data:application/pdf;base64,{encoded_pdf}",
        },
        "document_annotation_prompt": (
            "Provide a concise title and summary for this document."
        ),
        "document_annotation_format": {
            "type": "json_schema",
            "json_schema": {
                "name": "document_summary",
                "strict": True,
                "schema": {
                    "type": "object",
                    "properties": {
                        "title": {"type": "string"},
                        "summary": {"type": "string"},
                    },
                    "required": ["title", "summary"],
                    "additionalProperties": False,
                },
            },
        },
    },
)

response.raise_for_status()
result = response.json()

annotation = json.loads(result["document_annotation"])
print("Title:", annotation["title"])
print("Summary:", annotation["summary"])
```

{% endtab %}

{% tab title="Node.js" %}

```javascript
import fs from "node:fs";

const apiKey = "<CORTECS_API_KEY>";
const pdf = fs.readFileSync("document.pdf").toString("base64");

const response = await fetch("https://api.cortecs.ai/v1/ocr", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${apiKey}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    model: "mistral-ocr-4.1",
    document: {
      type: "document_url",
      document_url: `data:application/pdf;base64,${pdf}`,
    },
    document_annotation_prompt:
      "Provide a concise title and summary for this document.",
    document_annotation_format: {
      type: "json_schema",
      json_schema: {
        name: "document_summary",
        strict: true,
        schema: {
          type: "object",
          properties: {
            title: { type: "string" },
            summary: { type: "string" },
          },
          required: ["title", "summary"],
          additionalProperties: false,
        },
      },
    },
  }),
});

if (!response.ok) {
  throw new Error(`OCR request failed: ${response.status} ${await response.text()}`);
}

const result = await response.json();
const annotation = JSON.parse(result.document_annotation);

console.log("Title:", annotation.title);
console.log("Summary:", annotation.summary);
```

{% endtab %}

{% tab title="Curl" %}

```bash
PDF_BASE64="$(base64 < document.pdf | tr -d '\n')"

curl 'https://api.cortecs.ai/v1/ocr' \
  -H 'Authorization: Bearer <CORTECS_API_KEY>' \
  -H 'Content-Type: application/json' \
  -d "{
    \"model\": \"mistral-ocr-4.1\",
    \"document\": {
      \"type\": \"document_url\",
      \"document_url\": \"data:application/pdf;base64,${PDF_BASE64}\"
    },
    \"document_annotation_prompt\": \"Provide a concise title and summary for this document.\",
    \"document_annotation_format\": {
      \"type\": \"json_schema\",
      \"json_schema\": {
        \"name\": \"document_summary\",
        \"strict\": true,
        \"schema\": {
          \"type\": \"object\",
          \"properties\": {
            \"title\": {
              \"type\": \"string\"
            },
            \"summary\": {
              \"type\": \"string\"
            }
          },
          \"required\": [\"title\", \"summary\"],
          \"additionalProperties\": false
        }
      }
    }
  }"
```

{% endtab %}
{% endtabs %}
