# Error Handling

HTTP status codes, error response format, and common error examples.

Source: https://docs.walterwrites.ai/error-handling

## Error Response Format

All API errors return a JSON object with an `error` field (string). Some errors include a `code` field for programmatic handling and a `details` object with additional context.

```json
{
  "error": "Human-readable error message",
  "code": "machine_readable_code",
  "details": { ... }
}
```

| Field     | Type   | Present   | Description                                           |
| --------- | ------ | --------- | ----------------------------------------------------- |
| `error`   | string | Always    | Human-readable error description                      |
| `code`    | string | Sometimes | Machine-readable error code for programmatic handling |
| `details` | object | Sometimes | Additional context about the error                    |

## HTTP Status Codes

| Status Code | Meaning               | Description                                                                                       |
| ----------- | --------------------- | ------------------------------------------------------------------------------------------------- |
| `200`       | OK                    | Request successful                                                                                |
| `202`       | Accepted              | Async job accepted for processing                                                                 |
| `400`       | Bad Request           | Invalid request parameters or payload, text outside an endpoint's limits, or a processing failure |
| `401`       | Unauthorized          | Missing or invalid API key                                                                        |
| `403`       | Forbidden             | API key lacks required scope, or insufficient credits                                             |
| `404`       | Not Found             | Resource not found (e.g., expired async job)                                                      |
| `409`       | Conflict              | Idempotency key reused with different content, or another billing operation in progress           |
| `429`       | Too Many Requests     | Rate limit exceeded                                                                               |
| `500`       | Internal Server Error | Unexpected server error                                                                           |
| `503`       | Service Unavailable   | AI services temporarily unavailable                                                               |

## Common Errors

### Missing or Invalid API Key

**Status:** `401`

```json
{
  "error": "Authentication credentials were not provided."
}
```

Returned when the `X-API-Key` header is missing, or the key is invalid, revoked, or expired.

### Missing Required Scope

**Status:** `403`

```json
{
  "error": "API key does not have required scope: Humanizer"
}
```

### API Key Limit Reached

**Status:** `403`

```json
{
  "error": "API key limit reached",
  "code": "key_limit_reached",
  "details": {
    "active_keys": 5,
    "max_keys": 5,
    "plan": "1M"
  }
}
```

### Invalid Request Payload

**Status:** `400`

```json
{
  "error": "Invalid payload",
  "code": "invalid_payload",
  "details": {
    "content": ["This field is required."]
  }
}
```

### Insufficient Credits

**Status:** `403`

```json
{
  "error": "Insufficient credits",
  "code": "insufficient_credits",
  "details": {
    "credits": 50,
    "required": 120
  }
}
```

### Credits Exhausted (Paid Plan)

**Status:** `403`

```json
{
  "error": "Credits exhausted. Credits reset on next billing cycle.",
  "code": "credits_exhausted",
  "details": {
    "plan": "1M",
    "credits": 0
  }
}
```

### Trial Expired

**Status:** `403`

```json
{
  "error": "Trial expired, please subscribe",
  "code": "trial_expired"
}
```

### Rate Limit Exceeded

**Status:** `429`

The response includes a `Retry-After` header indicating how many seconds to wait.

### Idempotency Key Reuse

**Status:** `409`

Returned by the [Plagiarism Checker](https://docs.walterwrites.ai/api-reference/plagiarism-checker.md) and [Grammar Checker](https://docs.walterwrites.ai/api-reference/grammar-checker.md) when an `Idempotency-Key` is repeated with different text within the replay window.

```json
{
  "error": "This Idempotency-Key was already used with different text.",
  "code": "idempotency_key_reuse"
}
```

### Minimum Words Not Met

**Status:** `400`

```json
{
  "error": "The request must contain at least 5 words.",
  "code": "min_words_not_met",
  "details": {
    "limit": 5,
    "actual": 3
  }
}
```

### Word Limit Exceeded

**Status:** `400`

Returned when the input exceeds the endpoint's word cap (25,000 words for plagiarism scans, 10,000 for grammar corrections).

```json
{
  "error": "The request must contain no more than 25,000 words.",
  "code": "max_words_exceeded",
  "details": {
    "limit": 25000,
    "actual": 27140
  }
}
```

### Text Length Out of Range

**Status:** `400`

Returned by the [Humanizer](https://docs.walterwrites.ai/api-reference/humanizer.md) and [AI Detector](https://docs.walterwrites.ai/api-reference/detector.md) when the text is outside the 50 to 2,000 word range. Nothing is charged.

```json
{
  "error": "Error checking for AI content. The request must contain at least 50 words.",
  "code": "service_error"
}
```

```json
{
  "error": "The request must contain no more than 2,000 words",
  "code": "service_error"
}
```

### Insufficient Content

**Status:** `400`

Returned by the [Plagiarism Checker](https://docs.walterwrites.ai/api-reference/plagiarism-checker.md) when the text has no complete sentences to scan.

```json
{
  "error": "The text does not contain enough complete sentences to scan.",
  "code": "insufficient_content"
}
```

### Invalid Audio

**Status:** `400`

Returned by the [AI Voice Detector](https://docs.walterwrites.ai/api-reference/voice-detector.md) when the upload cannot be read as audio (corrupt file or unsupported codec). Nothing is charged.

```json
{
  "error": "The file could not be read as audio. Please upload a valid, non-corrupt audio file (wav, flac, ogg, mp3, m4a, aac).",
  "code": "invalid_audio"
}
```

### Audio Too Long

**Status:** `400`

Returned by the [AI Voice Detector](https://docs.walterwrites.ai/api-reference/voice-detector.md) when the audio exceeds the 10-minute limit. Nothing is charged.

```json
{
  "error": "The audio must be at most 600 seconds long.",
  "code": "audio_too_long",
  "details": {
    "limit_seconds": 600,
    "actual_seconds": 612.4
  }
}
```

### Too Many Pending Jobs

**Status:** `429`

```json
{
  "error": "Too many pending jobs. Wait for existing jobs to complete.",
  "code": "too_many_pending_jobs",
  "details": {
    "pending": 5,
    "limit": 5
  }
}
```

### Processing Failed

**Status:** `400`

Returned by the [Humanizer](https://docs.walterwrites.ai/api-reference/humanizer.md) and [AI Detector](https://docs.walterwrites.ai/api-reference/detector.md) when the text could not be processed. Credits are refunded automatically.

```json
{
  "error": "Error checking for AI content. All detector services failed.",
  "code": "service_error"
}
```

```json
{
  "error": "Unable to process the input text. All humanizer services failed.",
  "code": "service_error"
}
```

### Service Unavailable

**Status:** `503`

Returned by the [Plagiarism Checker](https://docs.walterwrites.ai/api-reference/plagiarism-checker.md), [Grammar Checker](https://docs.walterwrites.ai/api-reference/grammar-checker.md), [AI Image Detector](https://docs.walterwrites.ai/api-reference/image-detector.md), and [AI Voice Detector](https://docs.walterwrites.ai/api-reference/voice-detector.md) when the service is temporarily unavailable. Credits are refunded automatically. Retry shortly, after the `Retry-After` header when present.

```json
{
  "error": "Plagiarism checker service is temporarily unavailable",
  "code": "service_unavailable"
}
```

## Rate Limits

Rate limits are applied at multiple levels:

| Scope              | Default Limit         |
| ------------------ | --------------------- |
| Per IP (burst)     | 60 requests / minute  |
| Per IP (sustained) | 3,000 requests / hour |
| Per user (global)  | 60 requests / minute  |
| Humanizer (sync)   | 3 requests / minute   |
| Detector (sync)    | 10 requests / minute  |
| API key creation   | 10 requests / minute  |

> **Note:** Rate limits may vary by plan. Higher-tier plans may have higher limits.

Async requests are not subject to the per-minute rate limit — they are constrained by credits and the pending jobs limit (varies by plan, see [Async Jobs](https://docs.walterwrites.ai/api-reference/async-jobs.md)).

## Best Practices

* Always check the HTTP status code before parsing the response body.
* Use the `code` field (when present) for programmatic error handling instead of parsing the `error` string.
* Implement retry logic with exponential backoff for `429` and `5xx` errors, and for a `400` [Processing Failed](#processing-failed) error.
* Do not retry other `400` errors, or `401` and `403` errors — these indicate a problem with the request that must be fixed.
* Log error responses for debugging.
