Quick start
Give a PDF and a schema; get back JSON that fits it.
1. Get an API key
Sign in to the console and create a key. It is shown once; keep it in an environment variable. The same key works for Page Search, and the SDK also reads it from PAGEINDEX_SEARCH_API_KEY if you have it there.
export PAGEINDEX_EXTRACT_API_KEY=pie_live_...Install the Python SDK (source). Any other language can call the API directly, as the curl examples do.
pip install pageindex-extract2. Extract
Describe each field you want, then extract. The Python SDK uploads the file, starts the extraction and waits for it; most documents take a minute or less. Files uploaded to extract from go in your Page Extract collection, made on first use, apart from documents you upload to search; a file uploaded before is not uploaded again.
from pageindex_extract import PageExtract
client = PageExtract() # reads PAGEINDEX_EXTRACT_API_KEY
schema = {
"type": "object",
"properties": {
"company": {"type": "string", "description": "The company's name, as printed on the cover"},
"segments": {
"type": "array",
"description": "Each business segment in the segment results table",
"items": {
"type": "object",
"properties": {
"name": {"type": "string"},
"revenue_usd_millions": {"type": ["number", "null"], "description": "Revenue, in millions of dollars"},
},
},
},
},
}
job = client.extract("report.pdf", schema) # uploads the file, waits for the result
print(job.result){
"company": "Acme Industries plc",
"segments": [
{"name": "Industrial", "revenue_usd_millions": 4120.5},
{"name": "Consumer", "revenue_usd_millions": 2876.0},
{"name": "Services", "revenue_usd_millions": null}
]
}The result fits the schema. A value the document does not give is null; every row of a list comes back, in the order printed, however many there are.
3. Use a Pydantic model
In Python, the schema can be a Pydantic model, and the result parsed back into it.
from pydantic import BaseModel, Field
class Segment(BaseModel):
name: str
revenue_usd_millions: float | None = Field(description="Revenue, in millions of dollars")
class Report(BaseModel):
company: str = Field(description="The company's name, as printed on the cover")
segments: list[Segment] = Field(description="Each business segment in the segment results table")
job = client.extract("report.pdf", Report)
report = job.parse(Report) # a Report
print(report.segments[0].name, job.usage.pages, job.usage.credits)Next
- Writing a schema: descriptions are the instructions.
- Options: the pro tier, some pages only, the page each row came from.
- Jobs: start an extraction and come back for it later.
- API reference: every endpoint, field and error.