Skip to main content

Upload a document and consume its result

Use the temporary Files API to transport a local PDF, then analyze it with a service publishing primex_document_analysis. This example prints extracted text and a table count; it does not ask a chat model to interpret the result.

Prepare​

Choose a document-analysis service in Documents Setup. Check that its operation is Document analysis, not OCR-only extraction, and review supported PDF limits. Save a document you may process as invoice.pdf.

Shell
python -m pip install requests
export PRIMEX_API_KEY=YOUR_API_KEY
export PRIMEX_DOCUMENT_MODEL=YOUR_LIVE_DOCUMENT_ANALYSIS_MODEL

Save as extract_document.py and run python extract_document.py:

Python
import os
from pathlib import Path
import requests

gateway = "https://gateway.primexos.com"
headers = {"Authorization": f"Bearer {os.environ['PRIMEX_API_KEY']}"}
source = Path("invoice.pdf")
authorization = requests.post(
f"{gateway}/v1/primex/files/upload-authorizations", headers=headers, timeout=30,
json={"purpose": "DOCUMENT_INPUT", "content_type": "application/pdf",
"byte_size": source.stat().st_size},
)
authorization.raise_for_status()
upload = authorization.json()
with source.open("rb") as file:
# The upload URL has its own authorization. Do not send the Primex key to it.
stored = requests.request(
upload["method"], upload["upload_url"], headers=upload["headers"],
data=file, timeout=60,
)
stored.raise_for_status()
finalized = requests.post(
f"{gateway}/v1/primex/files/{upload['file_id']}/finalize",
headers=headers, timeout=30,
)
finalized.raise_for_status()
file = finalized.json()
result = requests.post(
f"{gateway}/v1/documents/analyze", headers=headers, timeout=120,
json={"model": os.environ["PRIMEX_DOCUMENT_MODEL"], "file_id": file["file_id"]},
)
result.raise_for_status()
analysis = result.json()
print("File expires:", file["expires_at"])
print("Pages:", analysis["pages"])
print(analysis["content"])
print("Tables:", len(analysis.get("tables", [])))

Expect the finalized file's expiry, analyzed page count, extracted text, and number of tables. Structured documents, tables, and keyValuePairs vary by model and input. Validate required invoice fields in your application rather than assuming every PDF returns the same schema.

OCR alternative and production handling​

For OCR-only extraction, choose an OCR offering and send model and file_id to /v1/ocr; consume its text and pages fields. Do not read content from an OCR response. The DOCUMENT_INPUT purpose supports compatible OCR consumers too.

The optional locale is an Azure recognition hint, not a translation target. Omission allows detection; Mistral OCR rejects this Azure-only hint. Document translation is a separate DOCX workflow.

Files are account-scoped temporary inputs. Finalization verifies the actual size and MIME type; use the returned expiry and re-upload after expiration. There is no manual delete endpoint in this temporary transport. Upload URLs and Claude file IDs are not interchangeable. See Files.

Analysis is billed by the selected service's measured pages. A failed upload must stop the workflow before analysis. A later application error does not reverse a completed analysis charge. Inspect its record in Usage; see Billing and Errors & limits before retrying an uncertain outcome.