Docs
Example: Python
title: "Python Example"
Python Example
This example scores a CSV file using the Profile Scoring API with Python's requests library.
Prerequisites
pip install requests pandas
Full example
import requests
import pandas as pd
import os
API_KEY = os.environ["AUSYNTH_API_KEY"]
SCORE_URL = "https://api.ausynth.com/api/score"
# Upload the file
with open("my_customers.csv", "rb") as f:
response = requests.post(
SCORE_URL,
headers={"Authorization": f"Bearer {API_KEY}"},
files={"file": ("my_customers.csv", f, "text/csv")},
)
response.raise_for_status()
result = response.json()
# Check the response
if result["status"] == "complete":
# Small file - results are ready
print(f"Scored {result['rows_scored']} rows, charged {result['credits_charged']} credits")
# Download the scored CSV
download = requests.get(result["download_url"])
with open("scored_output.csv", "wb") as out:
out.write(download.content)
# Load and inspect
df = pd.read_csv("scored_output.csv")
print(df[["id", "profile_name", "profile_confidence"]].head())
print(df["profile_name"].value_counts())
elif result["status"] == "processing":
# Large file - poll for completion
import time
poll_url = f"https://api.ausynth.com{result['poll_url']}"
while True:
time.sleep(5)
poll = requests.get(poll_url, headers={"Authorization": f"Bearer {API_KEY}"})
poll_result = poll.json()
if poll_result["status"] == "complete":
download = requests.get(poll_result["download_url"])
with open("scored_output.csv", "wb") as out:
out.write(download.content)
print(f"Scored {poll_result['rows_scored']} rows")
break
print(f"Still processing... ({poll_result.get('estimated_completion_seconds', '?')}s remaining)")
Checking data quality flags
The response includes a data_quality object that reports any values the API did not recognise:
result = response.json()
dq = result.get("data_quality", {})
if dq.get("rows_with_invalid_values", 0) > 0:
print(f"Warning: {dq['rows_with_invalid_values']} rows had unrecognised values")
for detail in dq["invalid_value_details"][:5]:
print(f" Row {detail['row']}: {detail['field']}='{detail['value']}' -> mapped to Not stated")
Handling errors
response = requests.post(SCORE_URL, headers={"Authorization": f"Bearer {API_KEY}"}, files={"file": f})
if response.status_code == 401:
print("Invalid API key. Check AUSYNTH_API_KEY.")
elif response.status_code == 402:
err = response.json()
print(f"Need {err['credits_required']} credits, have {err['credits_available']}")
elif response.status_code == 429:
err = response.json()
print(f"Rate limited. Retry in {err['retry_after_seconds']} seconds.")
elif response.status_code >= 400:
print(f"Error {response.status_code}: {response.json()}")
Using environment variables for the key
Set the key in your shell before running the script:
export AUSYNTH_API_KEY="ask_live_abc123def456"
python score_my_data.py
Never hard-code the key in your script or commit it to version control.