# Authentication

> Where to get a FlyMy.AI API key and how to send it. The same X-API-KEY header authenticates the Models API and the Agents API.

To use the FlyMyAI API, you need to authenticate your requests using your API key. You can find your API key in your [profile settings](https://app.flymy.ai/profile).

## Python SDK

The Python SDK handles authentication automatically when you initialize the client with your API key:

```python
from flymyai import client

# Initialize the client with your API key
fma_client = client(apikey="fly-***")

# Now you can make authenticated requests
response = fma_client.predict(
    model="flymyai/flux-dev",
    payload={
        "prompt": "A beautiful sunset over mountains",
        "height": 1024,
        "width": 1024
    }
)
```

## cURL

When making direct API calls using cURL, you need to include your API key in the request headers:

```bash
curl -X POST https://api.flymy.ai/api/v1/flymyai/flux-schnell/predict \
  -H "x-api-key: fly-***" \
  -F "prompt=Funny cat with stupid dog" \
  -F "height=1024" \
  -F "width=1024" \
  -F "num_inference_steps=4" \
  -F "guidance_scale=0" \
  -F "seed=1654"
```

## API Key Security

- Keep your API key secure and never share it publicly
- Don't commit your API key to version control
- Use environment variables or secure secret management systems to store your API key
- Rotate your API key if you suspect it has been compromised

## Rate Limits

API requests are subject to rate limits based on your subscription plan. You can check your current usage and limits in your [profile settings](https://app.flymy.ai/profile).

## Error Handling

If authentication fails, you'll receive a 401 Unauthorized response. Here's how to handle authentication errors:

```python
from flymyai import client, FlyMyAIPredictException

try:
    fma_client = client(apikey="invalid-key")
    response = fma_client.predict(...)
except FlyMyAIPredictException as e:
    if e.status_code == 401:
        print("Authentication failed. Please check your API key.")
    else:
        print(f"An error occurred: {e}")
```

```bash
# Example of an authentication error response
{
  "error": {
    "code": "unauthorized",
    "message": "Invalid API key"
  }
}
```
