# Authentication

Authenticate requests with API keys, follow security practices, and handle auth errors.

All requests to Felo API Platform APIs require authentication using an API Key. This guide explains how to authenticate your requests and handle common authentication issues.

## API Key Authentication

Felo uses API Key authentication for all v2 endpoints. Your API Key identifies your application and provides access to the platform's capabilities.

### How It Works

1. You obtain an API Key from the [Felo API Platform API Keys page](https://openapi.felo.ai/console/api-keys)
2. You include the API Key in the `Authorization` header of every API request
3. The platform validates your API Key and processes your request

### Header Format

Include your API Key in the `Authorization` header using the Bearer authentication scheme:

```
Authorization: Bearer YOUR_API_KEY
```

### Example Request

```bash
curl -X POST https://openapi.felo.ai/v2/chat \
  -H "Authorization: Bearer sk_live_abc123xyz789" \
  -H "Content-Type: application/json" \
  -d '{"query": "What is machine learning?"}'
```

## Obtaining Your API Key

To get your API Key:

1. Open the [Felo API Platform API Keys page](https://openapi.felo.ai/console/api-keys)
2. Sign in with your Felo account if prompted
3. Enter a key name and click **Create Key**
4. Copy the full key when it appears. It is only shown once
5. Store the key securely and use it as `FELO_API_KEY`

::: warning Keep Your API Key Secret
Your API Key provides access to your Felo account and should be kept confidential. Never share it publicly, commit it to version control, or expose it in client-side code.
:::

## Security Best Practices

### 1. Store API Keys Securely

**Don't** hardcode API Keys in your source code:

```python
# ❌ Bad - API Key exposed in code
api_key = "sk_live_abc123xyz789"
```

**Do** use environment variables or secure configuration:

```python
# ✅ Good - API Key from environment
import os
api_key = os.environ.get('FELO_API_KEY')
```

### 2. Use Environment Variables

Store your API Key in environment variables:

**Linux/macOS:**
```bash
export FELO_API_KEY="sk_live_abc123xyz789"
```

**Windows (Command Prompt):**
```cmd
set FELO_API_KEY=sk_live_abc123xyz789
```

**Windows (PowerShell):**
```powershell
$env:FELO_API_KEY="sk_live_abc123xyz789"
```

### 3. Rotate Keys Regularly

Periodically generate new API Keys and revoke old ones to minimize security risks.

### 4. Use Different Keys for Different Environments

Create separate API Keys for development, staging, and production environments.

### 5. Never Expose Keys in Client-Side Code

API Keys should only be used in server-side code. Never include them in:
- Frontend JavaScript code
- Mobile app source code
- Public repositories
- Client-side configuration files

## Authentication Errors

For the full error envelope, rate-limit headers, and retry policy, see [Errors, Limits, and Retries](https://openapi.felo.ai/docs/guides/errors-and-limits.md).

### Common Error Codes

| HTTP | Code | Meaning |
|------|------|---------|
| 401 | `UNAUTHORIZED` | Missing, invalid, expired, or revoked key |
| 429 | `RATE_LIMITED` | Too many requests; see the [Errors, Limits, and Retries](https://openapi.felo.ai/docs/guides/errors-and-limits.md) guide |

A rejected request returns the key status in `message`:

```json
{
  "status": 401,
  "code": "UNAUTHORIZED",
  "message": "API Key is required",
  "request_id": "..."
}
```

`status` is the HTTP status code. A revoked key reports `API Key has been revoked (keyPrefix=...)`. Branch on the HTTP status and `code`, not on the exact wording of `message`.

## Troubleshooting

If authentication fails:

1. Check that the header is exactly `Authorization: Bearer YOUR_API_KEY`.
2. Confirm the key has not been revoked on the [API Keys page](https://openapi.felo.ai/console/api-keys).
3. Confirm the environment is using the key you expect.
4. Generate a new key if the old one was deleted or rotated. A full key is shown only once, at creation.

For a symptom-first checklist that covers authentication and the rest of the platform, see [Troubleshooting](https://openapi.felo.ai/docs/faq/troubleshooting.md).

### Example: Debugging Authentication

```python
import requests

url = "https://openapi.felo.ai/v2/chat"
api_key = "YOUR_API_KEY"

# Correct format
headers = {
    "Authorization": f"Bearer {api_key}",
    "Content-Type": "application/json"
}

response = requests.post(url, headers=headers, json={"query": "test"})

if response.status_code == 401:
    error = response.json()
    print(f"Authentication failed: {error['code']}")
    print(f"Message: {error['message']}")
    print(f"Request ID: {error['request_id']}")
else:
    print("Authentication successful!")
```

## Rate Limiting

Rate limits are enforced **per endpoint**, not globally per API Key. Currently only [`/v2/chat`](https://openapi.felo.ai/docs/api-reference/chat.md#rate-limiting) enforces them; users with an effective paid Felo subscription are exempt. Other `/v2/**` endpoints have no enforced rate limits.

A 429 response includes `X-RateLimit-*` headers and `Retry-After`. Always read effective limits from the response headers, not from documentation.

For the full rate-limit dimensions, headers, and retry policy, see [Errors, Limits, and Retries](https://openapi.felo.ai/docs/guides/errors-and-limits.md).

## Need Help?

If you're experiencing authentication issues:

1. Review this guide carefully
2. Check the [Getting Started](https://openapi.felo.ai/docs/getting-started.md) guide
3. Verify your API Key on the [API Keys page](https://openapi.felo.ai/console/api-keys)
4. Contact support at support@felo.ai with your request ID

## Next Steps

- [Getting Started Guide](https://openapi.felo.ai/docs/getting-started.md) - Learn how to make your first API call
- [Chat API Reference](https://openapi.felo.ai/docs/api-reference/chat.md) - Explore the Chat API documentation
