import requestsimport osimport timeheaders = {"X-Avala-Api-Key": os.environ["AVALA_API_KEY"]}response = requests.get( "https://api.avala.ai/api/v1/datasets/nonexistent/", headers=headers,)if response.status_code == 200: data = response.json()elif response.status_code == 401: print("Invalid API key. Check your AVALA_API_KEY.")elif response.status_code == 404: print("Resource not found.")elif response.status_code == 429: retry_after = int(response.headers.get("Retry-After", 60)) print(f"Rate limited. Retrying in {retry_after}s...") time.sleep(retry_after)else: print(f"Error {response.status_code}: {response.json().get('detail', 'Unknown error')}")
When you receive a 429 response, always check the Retry-After header for the number of seconds to wait before retrying. The X-RateLimit-Remaining header on every response tells you how many requests you have left.
File uploads use presigned URLs rather than direct multipart uploads to the API. The general flow is:
Request a presigned upload URL from the API
Upload the file directly to cloud storage using the presigned URL
Confirm the upload with the API
# Step 1: Request presigned URLUPLOAD_INFO=$(curl -s -X POST "https://api.avala.ai/api/v1/datasets/manual-upload/file-upload-url/" \ -H "X-Avala-Api-Key: $AVALA_API_KEY" \ -H "Content-Type: application/json" \ -d '{"file_path_in_dataset": "image001.jpg", "dataset_name": "my-dataset"}')PRESIGNED_URL=$(echo $UPLOAD_INFO | jq -r '.url')# Step 2: Upload to cloud storage using the presigned POST fields# All fields from the response must be included as form datacurl -X POST "$PRESIGNED_URL" \ $(echo $UPLOAD_INFO | jq -r '.fields | to_entries[] | "-F \(.key)=\(.value)"') \ -F "file=@./image001.jpg"