import requests
import time
response = requests.post(
"https://api.monid.ai/v1/run",
headers={
"Authorization": "Bearer monid_live_...",
"Content-Type": "application/json",
},
json={
"provider": "apify",
"endpoint": "/apidojo/tweet-scraper",
"input": {
"searchTerms": ["AI"],
"maxItems": 10,
},
},
)
data = response.json()
if response.status_code == 202:
# Async provider — poll for results
run_id = data["runId"]
while True:
result = requests.get(
f"https://api.monid.ai/v1/runs/{run_id}",
headers={"Authorization": "Bearer monid_live_..."},
).json()
if result["status"] in ("COMPLETED", "FAILED", "BLOCKED", "STOPPED", "TIMED_OUT"):
break
time.sleep(5)
else:
# Sync provider — run completed immediately.
# The HTTP status mirrors the provider's status (200, 404, 429, ...).
result = data
if result["status"] == "FAILED":
print("Infrastructure error")
elif result["status"] in ("BLOCKED", "STOPPED", "TIMED_OUT"):
print(f"Run {result['status'].lower()}: {result.get('reason', 'no output returned')}")
elif result.get("providerResponse", {}).get("httpStatus", 200) >= 400:
print(f"Provider error: HTTP {result['providerResponse']['httpStatus']}")
else:
print(f"Success: {result.get('output')}")