Python SDK quickstart
Python SDK quickstart
Section titled “Python SDK quickstart”Install the software development kit (SDK) from the Python Package Index (PyPI):
pip install nextpdfCreate 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.
Use environment variables
Section titled “Use environment variables”The command-line interface (CLI) and agent workflows can read connection settings from environment variables:
export NEXTPDF_BASE_URL=http://localhost:8080export NEXTPDF_API_KEY=your-keyIn Windows PowerShell:
$env:NEXTPDF_BASE_URL = "http://localhost:8080"$env:NEXTPDF_API_KEY = "your-key"Handle common errors
Section titled “Handle common errors”Catch SDK and API exceptions when you call extraction methods:
from nextpdf import NextPDFfrom 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.