# Read every contact created after a date, page by page. Python 3.9+ with requests.
import os
import sys

import requests

base = os.environ.get("RELATIONSAI_API_BASE", "https://voice-api.relationsai.com/v1")
filters = {"limit": 100, "created_after": os.environ.get("CREATED_AFTER", "2026-01-01T00:00:00.000Z")}
headers = {"Authorization": f"Bearer {os.environ['RELATIONSAI_API_KEY']}"}

cursor = None
total = 0
while True:
    # The cursor only works with the same filters that produced it.
    params = {**filters, "cursor": cursor} if cursor else filters
    r = requests.get(f"{base}/contacts", params=params, headers=headers, timeout=30)
    page = r.json()
    if not r.ok:
        err = page["error"]
        sys.exit(f"{err['code']}: {err['message']} (request {err['request_id']})")
    for c in page["data"]:
        print(f"{c['id']} {c['phone_number']} consent={c['consent_granted']} do_not_call={c['do_not_call']}")
    total += len(page["data"])
    if not page["has_more"]:
        break
    cursor = page["next_cursor"]
print(f"{total} contacts")
