Authentication
Send your Bayle API key as a bearer token:
Authorization: Bearer <your-key>If you don’t have a key yet, get a starter key at chat.bayle.ai/developers.
Show the prompt
Help me set this up to use Bayle AI, an OpenAI-compatible API. Full guide: https://docs.bayle.ai/authentication/
Goal: authenticate every Bayle request in this project with a bearer key read from the environment.
1. Find where this project calls the Bayle API (an OpenAI SDK client, fetch, httpx or curl scripts).
2. Make each call send the header `Authorization: Bearer <key>`, where the key comes from the BAYLE_API_KEY environment variable. With an OpenAI SDK, pass it as `api_key` (Python: `api_key=os.environ["BAYLE_API_KEY"]`; Node: `apiKey: process.env.BAYLE_API_KEY`) and the SDK sends the header for me.
3. Only these endpoints need a key: POST /chat/completions, /embeddings, /images/generations, /video/queue, /video/retrieve and /video/complete. GET /models and POST /video/quote are open, so do not require a key for them.
4. Handle failures by branching on `error.code` in the JSON error body, never on the message text (it is localized):
- 401 invalid_api_key: the key is missing or wrong. Nothing was charged. Tell me to check BAYLE_API_KEY.
- insufficient_quota: the key is fine but the account has no balance left.
- usd_spend_limit_exceeded or secondary_spend_limit_exceeded: the key hit a spending limit set on it, even if the account has balance.
Show a clear message for each; do not retry a 401.
5. Keep the key out of source control, client-side code and logs. If the project has a .env file, make sure it is git-ignored.
6. Test with a request that must fail and one that must pass:
curl -s -o /dev/null -w "%{http_code}\n" https://api.bayle.ai/chat/completions -H "Content-Type: application/json" -d '{"model":"MODEL_ID","messages":[{"role":"user","content":"hi"}]}'
(expect 401 without a key), then the same request with `-H "Authorization: Bearer $BAYLE_API_KEY"` (expect 200). Take MODEL_ID from GET /models?type=text.
Rules:
1. The base URL is https://api.bayle.ai exactly as written. Append no path to it.
2. Read the key from the BAYLE_API_KEY environment variable. Never write the key itself into a file you create, a command, a commit or the chat. Where the tool keeps the key in its own settings screen, tell me where to paste it. If BAYLE_API_KEY is not set, stop and ask me to create a key at https://chat.bayle.ai/developers and export it.
3. Take model ids only from GET https://api.bayle.ai/models (no key needed). Never guess one.
4. Finish with one real test request, show me the result and list the files you changed.Which endpoints need a key
Section titled “Which endpoints need a key”Two endpoints are open, so you can check connectivity and price a job before you hold a credential. Everything that generates something requires one.
| Endpoint | Key required |
|---|---|
GET /models |
No |
POST /video/quote |
No |
POST /chat/completions |
Yes |
POST /embeddings |
Yes |
POST /images/generations |
Yes |
POST /video/queue |
Yes |
POST /video/retrieve |
Yes |
POST /video/complete |
Yes |
When authentication fails
Section titled “When authentication fails”A missing or invalid credential returns 401 with the standard
error envelope, and the request is rejected before any generation work
begins, so nothing is charged.
{ "error": { "message": "Your Bayle API key is missing or could not be verified.", "type": "authentication_error", "param": null, "code": "invalid_api_key" }}Branch on error.code rather than on the message text, which is localized. See
Response language.
Two other codes are worth telling apart from an invalid key, because the fix is different:
insufficient_quotameans the key is fine but the account has no balance left.usd_spend_limit_exceededandsecondary_spend_limit_exceededmean the key has hit a spending limit configured on it, even though the account may still have balance.
Using it with an OpenAI SDK
Section titled “Using it with an OpenAI SDK”An OpenAI client sends exactly this header, so passing your Bayle key as the SDK’s api_key is
all that’s needed. See OpenAI compatibility.