import requests
import time
API_KEY = "YOUR_API_KEY"
BASE_URL = "https://api.sixtyfour.ai"
headers = {
"x-api-key": API_KEY,
"Content-Type": "application/json"
}
start_response = requests.post(
f"{BASE_URL}/search/start-deep-search",
headers=headers,
json={
"query": "VP of Engineering at Series B SaaS startups in New York",
"mode": "people",
"max_results": 500
}
)
start_response.raise_for_status()
task_id = start_response.json()["task_id"]
print(f"Search started: {task_id}")
deadline = time.monotonic() + 20 * 60
while True:
if time.monotonic() > deadline:
raise TimeoutError(f"Search {task_id} did not finish within 20 minutes")
status_response = requests.get(
f"{BASE_URL}/search/status/{task_id}",
headers=headers,
timeout=30,
)
if status_response.status_code in (429, 503):
time.sleep(int(status_response.headers.get("Retry-After", "10")))
continue
status_response.raise_for_status()
status_data = status_response.json()
print(f"Status: {status_data['status']}, {status_data.get('progress_message') or ''}")
if status_data["status"] == "completed":
resource_handle_id = status_data["resource_handle_id"]
print(f"Total results: {status_data['total_results']}")
break
if status_data["status"] != "running":
raise RuntimeError(f"Search {status_data['status']}: {status_data.get('error')}")
time.sleep(10)
download_response = requests.get(
f"{BASE_URL}/search/download",
headers=headers,
params={"resource_handle_id": resource_handle_id}
)
download_response.raise_for_status()
download_url = download_response.json()["url"]
print(f"Download URL: {download_url}")
csv_response = requests.get(
download_url,
headers=headers,
allow_redirects=False
)
if csv_response.status_code != 200:
raise RuntimeError(f"Download failed: {csv_response.status_code}")
with open("search_results.csv", "wb") as f:
f.write(csv_response.content)
print("Results saved to search_results.csv")