PDF Text Extraction in Python by URL: Page by Page, $0.008 per Call
Extract the text of a public PDF from Python without installing a PDF library: POST the URL to /v1/pdf and get the text page by page, up to 50 pages and 10 MB per call. $0.008 USDC on Base per call over x402, charged only when text comes back.
Steps
- Install the x402 client. pip install "x402[requests,evm]" and export a Base wallet key holding a little USDC as AGENT_KEY.
- Pick the pages. pages is optional: a list like [1, 2] or a string like "1-3,7", up to 50 pages. Without it you get up to 50 pages from page 1.
- Call /v1/pdf. POST {"url": "<pdf>", "pages": "1-3"}. The response has page_count, pages_returned, title, author, truncated, text and pages (page, text).
- Handle the free failures. Not charged: 413 (over 10 MB), 415 (not a PDF), 422 (no declared size, password-protected, no text layer, or no requested page exists) and 502.
Code
curl: unpaid call shows the price (HTTP 402, $0.008)
curl -i -G https://api.switch402.com/v1/pdf -H 'User-Agent: my-agent/1.0' \
--data-urlencode 'url=https://example.com/report.pdf' --data-urlencode 'pages=1-3'Python: PDF to text, page by page
# pip install "x402[requests,evm]"
import os
from eth_account import Account
from x402 import x402ClientSync
from x402.http.clients import x402_requests
from x402.mechanisms.evm import EthAccountSigner
from x402.mechanisms.evm.exact import ExactEvmScheme
client = x402ClientSync()
client.register("eip155:8453", ExactEvmScheme(signer=EthAccountSigner(Account.from_key(os.environ["AGENT_KEY"]))))
session = x402_requests(client)
session.headers["User-Agent"] = "my-agent/1.0"
r = session.post("https://api.switch402.com/v1/pdf", json={"url": "https://example.com/report.pdf", "pages": "1-3"}) # $0.008
if r.status_code != 200:
raise SystemExit(f"HTTP {r.status_code}: not charged")
doc = r.json()
print(doc["page_count"], "pages in the file,", doc["pages_returned"], "returned")
for p in doc["pages"]:
print(f"--- page {p['page']} ---")
print(p["text"][:500])You pay only for the pages you actually fetch: $0.008 per standard page on /v1/scrape, $0.003 per lite call on /v1/scrape/lite, in USDC on Base and charged only on success. No subscription or unused credits; optional prepaid packs and a $10 monthly plan.
FAQ
- Does it do OCR on scanned PDFs?
- No. It reads the text layer; a scanned PDF with no text layer returns 422 and is not charged.
- How long can the PDF be?
- Up to 10 MB and 50 pages per call, with up to 500,000 characters of text (truncated: true when cut). For longer files, call again with the next pages.
- Why did a valid PDF return 422?
- The server hosting it did not declare the file size (no Content-Length or Content-Range). Those calls are not charged.
Related
- API docs
- Use cases
- Pricing calculators
- All guides
- Scrape a website to markdown in Python
- Add web scraping to Claude Desktop (MCP)
- Add web scraping to Cursor (MCP)
- Pay per request with x402 and USDC
- Web search MCP in Claude Code
- Web scraping in n8n, pay per use
- LangChain web scraper tool
- CrewAI web scraping tool