> For the complete documentation index, see [llms.txt](https://docs.synthdata.co/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.synthdata.co/getting-started/error-handling-old.md).

# Error Handling (old)

Handle errors gracefully when using Synth API.

## HTTP Status Codes

| Code | Meaning      | Action                                |
| ---- | ------------ | ------------------------------------- |
| 200  | Success      | Process response                      |
| 400  | Bad Request  | Check authentication header           |
| 401  | Unauthorized | Check API key                         |
| 404  | Not Found    | Check parameters or data availability |
| 429  | Rate Limited | Retry after delay                     |
| 500  | Server Error | Retry later                           |

## Error Response Format

Errors return JSON with a `message` field:

```json
{"message": "Unauthorized"}
```

```json
{"message": "missing key in request header"}
```

Some endpoints return a plain string for data errors:

```json
"No prediction available"
```

## Common Errors

<details>

<summary>Missing Authorization Header — Status: 400</summary>

```json
{"message": "missing key in request header"}
```

{% hint style="info" %}
Solution: Add the `Authorization: Apikey YOUR_API_KEY` header.
{% endhint %}

</details>

<details>

<summary>Invalid API Key — Status: 401</summary>

```json
{"message": "Unauthorized"}
```

{% hint style="info" %}
Solution: Verify your API key in the dashboard.
{% endhint %}

</details>

<details>

<summary>No Data Available — Status: 404</summary>

```json
"No prediction available"
```

{% hint style="info" %}
Solution: Check that:

* Asset is valid (`BTC`, `ETH`, `SOL`, `XAU`, `SPYX`, `NVDAX`)
* Required parameters are provided
* For `/v2/prediction/latest`: miner UIDs are valid
  {% endhint %}

</details>

## Best Practices

{% code title="example.py" %}

```python
import requests
import time

def fetch_predictions(asset):
    response = requests.get(
        "https://api.synthdata.co/v2/prediction/best",
        headers={"Authorization": "Apikey YOUR_API_KEY"},
        params={"asset": asset, "time_increment": 300, "time_length": 86400}
    )
    
    if response.status_code == 200:
        return response.json()
    elif response.status_code == 401:
        raise Exception("Invalid API key")
    elif response.status_code == 404:
        raise Exception(f"No data available for {asset}")
    elif response.status_code == 429:
        time.sleep(60)
        return fetch_predictions(asset)  # Retry
    else:
        response.raise_for_status()
```

{% endcode %}
