Understanding and handling AdMorph API errors
All API errors follow a consistent JSON structure to make error handling straightforward and predictable.
{
"error": "Error Type",
"message": "Human-readable error description",
"details": {
// Optional additional context
}
}
AdMorph API uses standard HTTP status codes to indicate the success or failure of requests:
{
"error": "Bad Request",
"message": "Prompt is required"
}
{
"error": "Unauthorized",
"message": "Invalid API key"
}
{
"error": "Insufficient Credits",
"message": "Not enough credits. Required: 2, Available: 1",
"details": {
"required": 2,
"available": 1
}
}
{
"error": "Not Found",
"message": "Submission not found"
}
{
"error": "Too Many Requests",
"message": "Rate limit exceeded. Maximum 100 requests per hour.",
"retryAfter": 3600
}
{
"error": "Internal Server Error",
"message": "An unexpected error occurred. Please try again."
}
{
"error": "Service Unavailable",
"message": "Service temporarily unavailable. Please try again later."
}
// Python example
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
# Success
submission = response.json()['submission']
elif response.status_code == 402:
# Insufficient credits
print("Not enough credits. Please purchase more.")
elif response.status_code == 429:
# Rate limited
retry_after = response.json().get('retryAfter', 3600)
time.sleep(retry_after)
else:
# Other error
error = response.json()
print(f"Error: {error['message']}")
// Node.js example with exponential backoff
async function makeRequestWithRetry(url, options, maxRetries = 3) {
for (let attempt = 0; attempt < maxRetries; attempt++) {
try {
const response = await axios.post(url, options);
return response.data;
} catch (error) {
const status = error.response?.status;
// Don't retry client errors (except 429)
if (status >= 400 && status < 500 && status !== 429) {
throw error;
}
// Last attempt failed
if (attempt === maxRetries - 1) {
throw error;
}
// Exponential backoff: 1s, 2s, 4s
const delay = Math.pow(2, attempt) * 1000;
await new Promise(resolve => setTimeout(resolve, delay));
}
}
}
Instead of repeatedly polling for status updates (which can trigger rate limits), configure a webhook to receive notifications when submissions complete.
// Check credits before creating submission
const creditsResponse = await fetch(
'https://admorph.net/api/v1/user/credits',
{ headers: { 'X-API-Key': apiKey } }
);
const { credits } = await creditsResponse.json();
if (credits < requiredCredits) {
console.log(`Insufficient credits. You have ${credits}, need ${requiredCredits}`);
// Redirect user to purchase credits
} else {
// Proceed with submission
}
Always log error responses with enough context to debug issues:
If you encounter an error you can't resolve:
When contacting support, please include: