Skip to content

Python SDK quickstart

Install the software development kit (SDK) from the Python Package Index (PyPI):

Terminal window
pip install nextpdf

Create a client for your NextPDF Connect endpoint:

from nextpdf import NextPDF
client = NextPDF(base_url="http://localhost:8080", api_key="your-key")
with open("document.pdf", "rb") as file:
blocks = client.ast.extract_cited_text(file.read())
for block in blocks:
page = block.citation.page_index
confidence = block.citation.confidence
print(f"[page {page}, confidence {confidence:.2f}] {block.text[:100]}")

If your endpoint does not require an application programming interface (API) key, omit api_key.

The command-line interface (CLI) and agent workflows can read connection settings from environment variables:

Terminal window
export NEXTPDF_BASE_URL=http://localhost:8080
export NEXTPDF_API_KEY=your-key

In Windows PowerShell:

Terminal window
$env:NEXTPDF_BASE_URL = "http://localhost:8080"
$env:NEXTPDF_API_KEY = "your-key"

Catch SDK and API exceptions when you call extraction methods:

from nextpdf import NextPDF
from nextpdf.models.errors import NextPDFAPIError, NextPDFError, QuotaExceededError
client = NextPDF(base_url="http://localhost:8080", api_key="your-key")
try:
with open("document.pdf", "rb") as file:
blocks = client.ast.extract_cited_text(file.read())
except QuotaExceededError as error:
print(f"Rate limit hit: {error}")
except NextPDFAPIError as error:
print(f"API error {error.status_code}: {error}")
except NextPDFError as error:
print(f"SDK error: {error}")

For Portable Document Format (PDF) files over 100 MB, use the CLI. This lets results stream without loading every extracted block into memory at once.