Skip to Content
NotebooksPDFPhone scan

Phone scan

Download notebook (.ipynb)

Open a phone-scan session and poll it with the Indox SDK. The phone normally uploads the pages; here the notebook plays the phone with the session’s pair token, then finalizes and downloads the assembled PDF.

Config

Add your settings to the repo .env (local) or Colab Secrets — this cell reads them with get(...): INDOX_API_KEY (your ak_… key) and INDOX_BASE_URL (https://indox.org; the SDK adds /api/v1/… itself). A missing key stops here with its name.

import json, logging, re, sys, tempfile from pathlib import Path REPO = Path.cwd().parents[1] sys.path[:0] = [str(REPO), str(REPO / "languages" / "python")] try: from google.colab import userdata get = userdata.get except ImportError: from tools.env import require_env as get from indox_client import Indox logging.basicConfig(format="→ %(message)s", force=True) logging.getLogger("urllib3.connectionpool").setLevel(logging.DEBUG) def _mask(record): record.msg = re.sub(r"/(public|sign)/[^/ ]+", r"/\1/***", record.getMessage()) record.args = () return True logging.getLogger("urllib3.connectionpool").addFilter(_mask) def show(data, limit=2500): print(json.dumps(data, indent=2, default=str)[:limit]) client = Indox(api_key=get("INDOX_API_KEY"), base_url=get("INDOX_BASE_URL")) start_credits = client.billing.credits_balance()["credit_amount"] SAMPLES = REPO / "colab" out = Path(tempfile.mkdtemp()) import time img = SAMPLES / "image" / "samples" / "sample.jpg"
scan = client.pdf.create_scan_session() pair = {scan["pair_token_header"]: scan.pop("pair_token")} show(scan) assert scan["status"] == "pending"
polled = client.pdf.get_scan_session(scan["id"]) polled.pop("pair_token") show(polled) assert polled["pages"] == 0
with img.open("rb") as fh: page = client.pdf.upload_scan_pair_page(files={"file": (img.name, fh)}, headers=pair) show(page) assert page["order_index"] == 0
final = client.pdf.finalize_scan_session(scan["id"]) show(final) for _ in range(60): done = client.pdf.get_scan_session(scan["id"]) if done["status"] in ("completed", "failed"): break time.sleep(1) assert done["status"] == "completed", done["status"]
scan_pdf = client.pdf.download_scan_session(scan["id"], out / "scan.pdf") show({"status": done["status"], "pages": done["pages"], "bytes": scan_pdf.stat().st_size}) assert scan_pdf.read_bytes()[:5] == b"%PDF-"

Usage

now = client.billing.credits_balance()["credit_amount"] print(f"credits used by this notebook: {start_credits - now} (balance {now})") show(client.billing.usage_by_feature())