SynthForge SynthForge SynthForge IO

Docs · Python

Python

Call the REST API from a script or a notebook with requests. Create a schema, start a dataset job, wait for it, download the zip.

Before you start: you need a personal API key. Create one in the app, then come back.

Same flow with the standard library-friendly requests package (pip install requests):

python
import os
import time
import requests

API = os.environ.get("SF_API_BASE", "https://api.synthforge.io")
KEY = os.environ["SF_API_KEY"]  # sfk_live_… or sfk_dev_…
H = {"Authorization": f"Bearer {KEY}", "Content-Type": "application/json"}

# Who am I?
me = requests.get(f"{API}/api/v1/users/me", headers=H, timeout=30)
me.raise_for_status()
print(me.json()["email"])

# Create schema
schema = {
    "name": "py-demo",
    "schema_data": {
        "tables": [
            {
                "name": "events",
                "columns": [
                    {"name": "id", "field_type": "integer_sequence", "primary_key": True},
                    {"name": "label", "field_type": "word"},
                ],
            }
        ]
    },
}
r = requests.post(f"{API}/api/v1/schemas", headers=H, json=schema, timeout=60)
r.raise_for_status()
schema_id = r.json()["id"]

# Generate
r = requests.post(
    f"{API}/api/v1/datasets",
    headers=H,
    json={
        "schema_id": schema_id,
        "name": "py-demo-run",
        "row_counts": {"events": 500},
        "formats": ["csv"],
        "seed": 7,
    },
    timeout=60,
)
r.raise_for_status()
dataset_id = r.json()["id"]

# Poll
while True:
    st = requests.get(
        f"{API}/api/v1/datasets/{dataset_id}/status", headers=H, timeout=30
    ).json()
    print(st["status"], st.get("progress_percentage"))
    if st["status"] in ("completed", "failed", "cancelled"):
        break
    time.sleep(2)

if st["status"] != "completed":
    raise SystemExit(st)

# Download ZIP
url = requests.get(
    f"{API}/api/v1/datasets/{dataset_id}/download", headers=H, timeout=30
).json()["url"]
zip_bytes = requests.get(url, timeout=120).content
open("dataset.zip", "wb").write(zip_bytes)
print("wrote dataset.zip", len(zip_bytes), "bytes")

Next