Resources
Mar 14, 20263 min read

Fetch and export insights to CSV

A minimal Python script that pulls the latest completed insights and writes them to a CSV file.

Overview

This script fetches completed insights from SynthLink and writes them to a CSV file using httpx and Python's built-in csv module.

The script

export_insights.py
import httpx
import csv
import os

API_KEY = os.environ["SYNTHLINK_KEY"]
BASE_URL = "https://synth-link.com/api/v1"

def fetch_insights(limit: int = 100) -> list[dict]:
    resp = httpx.get(
        f"{BASE_URL}/insights",
        headers={"X-SYNTHLINK-KEY": API_KEY},
        params={"status": "completed", "limit": limit},
    )
    resp.raise_for_status()
    return resp.json()

def export_to_csv(insights: list[dict], path: str = "insights.csv") -> None:
    if not insights:
        print("No insights to export.")
        return

    fieldnames = ["id", "document_id", "llm_summary", "category", "created_at"]

    with open(path, "w", newline="") as f:
        writer = csv.DictWriter(f, fieldnames=fieldnames, extrasaction="ignore")
        writer.writeheader()
        writer.writerows(insights)

    print(f"Exported {len(insights)} insights to {path}")

if __name__ == "__main__":
    insights = fetch_insights()
    export_to_csv(insights)

Running the script

terminal
SYNTHLINK_KEY=sk_live_your_key python export_insights.py