import requests
import time
API_KEY = "YOUR_API_KEY"
BASE_URL = "https://api.anyformat.ai"
headers = {"x-api-key": API_KEY}
# 1. Create a workflow
workflow = requests.post(
f"{BASE_URL}/workflows/",
headers={**headers, "Content-Type": "application/json"},
json={
"name": "Invoice Extraction",
"fields": [
{"name": "invoice_number", "description": "Invoice ID", "data_type": "string"},
{"name": "total", "description": "Total amount", "data_type": "float"}
]
}
).json()
# 2. Run extraction
with open("invoice.pdf", "rb") as f:
job = requests.post(
f"{BASE_URL}/workflows/{workflow['id']}/run/",
headers=headers,
files={"file": f}
).json()
# 3. Poll for results
while True:
result = requests.get(
f"{BASE_URL}/jobs/{job['extraction_id']}/",
headers=headers
).json()
if result["status"] == "processed":
print(result["results"])
break
time.sleep(5)