API Usage Best Practices
Error Handling
Always Check Response Status
import requests
API_BASE = "https://api.tomatopy.pizza/v1"
HEADERS = {
"Content-Type": "application/json",
"X-API-Key": "your-api-key",
"X-Session-ID": "your-session-id"
}
# GOOD: Check status and handle errors
response = requests.post(f"{API_BASE}/tomato/acquire", headers=HEADERS, json={
"variety": "San Marzano",
"ripeness": 0.8,
"weight": 150
})
if response.status_code == 200:
tomato = response.json()
elif response.status_code == 400:
print(f"Bad request: {response.json()['message']}")
elif response.status_code == 401:
print("Invalid API key")
else:
print(f"Unexpected error: {response.status_code}")Use raise_for_status()
Create a Helper Function
Checking Response Quality Fields
Validating Pipeline Order
Rule 1: Slice Before Simmering
Rule 2: Assemble Before Baking
Reusing Session IDs Correctly
Do: Use the Same Session for Related Operations
Do Not: Mix Unrelated Workflows in One Session
Do Not: Change Session ID Mid-Workflow
Retry Logic
Best Practices Summary
Next Steps
Last updated
Was this helpful?