Skip to main content

Error Response Format

All errors follow a consistent JSON format:
{
  "error": {
    "code": "error_code",
    "message": "Human-readable description"
  }
}

Error Codes

Authentication Errors

CodeStatusDescription
unauthorized401Invalid or missing API key
forbidden403Valid key but action not permitted (e.g., wrong origin for public key)

Validation Errors

CodeStatusDescription
bad_request400Invalid request parameters
not_found404Resource not found

Limit Errors

CodeStatusDescription
file_too_large413File exceeds your plan’s max file size
storage_limit_exceeded413Storage quota exceeded
rate_limit_exceeded429Too many requests
api_key_limit_exceeded403Maximum API keys reached for your plan

Account Errors

CodeStatusDescription
grace_period403Account is in grace period—uploads disabled

Server Errors

CodeStatusDescription
internal_error500Unexpected server error

Handling Errors

const response = await fetch('https://api.stashfyle.com/v1/upload', {
  method: 'POST',
  headers: { 'Authorization': 'Bearer sk_live_xxx' },
  body: form
});

if (!response.ok) {
  const { error } = await response.json();

  switch (error.code) {
    case 'file_too_large':
      console.error('File is too large. Max size:', error.message);
      break;
    case 'storage_limit_exceeded':
      console.error('Storage full. Upgrade your plan or delete files.');
      break;
    case 'rate_limit_exceeded':
      // Retry after delay
      const retryAfter = response.headers.get('Retry-After');
      await sleep(retryAfter * 1000);
      break;
    default:
      console.error('Upload failed:', error.message);
  }
}

Rate Limit Headers

When rate limited (429), check these headers:
HeaderDescription
X-RateLimit-LimitRequests allowed per window
X-RateLimit-RemainingRequests remaining
X-RateLimit-ResetUnix timestamp when window resets
Retry-AfterSeconds to wait before retrying

Best Practices

Never assume a request succeeded. Check the HTTP status code before processing the response.
For rate limits and server errors, use exponential backoff with jitter to avoid thundering herd.
Log the error code (not just the message) for easier debugging and monitoring.
If you receive grace_period, notify users to update their subscription.