Python examples

Script for parsing KYC sessions

Page through sessions and write them to a file.

Before you start

  • A current Python release and the requests package.
  • Put the API address that came with your access into the environment variable API_BASE_URL. The samples never hard-code an address.
  • Keep tokens and passwords out of the script and out of a repository. The samples read them from environment variables.

What it does

The script reads sessions with filter sessions, one page at a time, inside a date window, and writes one JSON object per line. It stops when the last page is read.

Keep each date window to a year or less. For a longer period, run the script once per window.

The script

import json
import os
import sys

import requests

API = os.environ["API_BASE_URL"]
TOKEN = os.environ["API_TOKEN"]            # a permanent API token
PAGE_SIZE = 100


def fetch_page(page: int, start: str, end: str, status: str | None) -> dict:
    filters = {"range": {"start": start, "end": end, "sortOrder": "ASC"}}
    if status:
        filters["status"] = status
    reply = requests.post(
        f"{API}/kyc/sessions/filter",
        json={"token": TOKEN, "page": page, "pageSize": PAGE_SIZE, "filters": filters},
        timeout=60,
    )
    body = reply.json()
    if body.get("status") != "ok":
        sys.exit(f"export stopped on page {page}: status {body.get('status')}")
    return body["results"]


def export(start: str, end: str, status: str | None, out_path: str) -> int:
    written = 0
    page = 1
    with open(out_path, "w", encoding="utf-8") as out:
        while True:
            results = fetch_page(page, start, end, status)
            for session in results["sessions"]:
                out.write(json.dumps(session, ensure_ascii=False) + "\n")
                written += 1
            if page >= results["pageInfo"]["total"]:      # "total" is the number of pages
                return written
            page += 1


if __name__ == "__main__":
    count = export("2026-01-01", "2026-01-31", None, "sessions.jsonl")
    print(f"{count} sessions written")

Notes on the answer

  • results.pageInfo.total is the number of pages. results.pageInfo.totalItems is the number of sessions that match.
  • A session without the permissions to see its document text, checks or images arrives with the string Access denied in place of those keys. See filter sessions.
  • For a very large period, use the export mode of the same call and fetch the archive with download logs archive.
  • Treat the output file as personal data. Store it accordingly.

Questions while integrating?

Write to us with the details and we will help.

Talk to us