# API reference

> The base URL, authentication header, versioning, app attribution and response headers, and every endpoint of the Lykia API.

The Lykia API is an OpenAI-compatible REST API. Request and response bodies are JSON, and streamed responses arrive as Server-Sent Events. This section documents each endpoint field by field; for concepts and walkthroughs, start with the [Guides](https://www.getlykia.com/en/docs/guides/authentication).

## Base URL

All endpoint paths are relative to:

```text
https://api.getlykia.com/v1
```

So a `POST /chat/completions` request goes to `https://api.getlykia.com/v1/chat/completions`.

## Authentication

Send your project's API key in the `Authorization` header of every request:

```http
Authorization: Bearer <LYKIA_API_KEY>
```

Keys start with `lyk-v1-` and belong to exactly one project, so each request is handled with that project's settings, budget and logs. A missing, invalid, disabled, archived or expired key gets a `401`; a key whose project has been archived gets a `403`. Creating keys, spending limits and storing keys safely are covered in [Authentication and API keys](https://www.getlykia.com/en/docs/guides/authentication).

Requests with a body send JSON with `Content-Type: application/json`. The body has a size limit set by Lykia; a larger body gets `413`. Each key also has a per-minute request limit; see [Limits](https://www.getlykia.com/en/docs/guides/limits).

## Versioning

The version is part of the base URL (`/v1`), and this reference describes v1. New fields may show up in responses over time, so write your client to ignore fields it does not recognise; that way additions never break it.

## App attribution headers

Two optional headers tell Lykia which application sent a request:

| Header         | Meaning                                                            |
| -------------- | ------------------------------------------------------------------ |
| `HTTP-Referer` | Your application's URL, for example `https://example.com`          |
| `X-Title`      | Your application's display name, for example `Example Support Bot` |

Lykia stores both values with the request record. When several apps share a project, this is how you tell their traffic apart in the logs and in the `app` field of [`GET /generation`](https://www.getlykia.com/en/docs/api-reference/generation). Leaving them out has no effect on the request itself.

**cURL**

```bash
curl https://api.getlykia.com/v1/chat/completions \
  -H "Authorization: Bearer $LYKIA_API_KEY" \
  -H "Content-Type: application/json" \
  -H "HTTP-Referer: https://example.com" \
  -H "X-Title: Example Support Bot" \
  -d '{
    "model": "openai/gpt-4o-mini",
    "messages": [{ "role": "user", "content": "Hello!" }]
  }'
```

**Python**

```python
import os

from openai import OpenAI

client = OpenAI(
    base_url="https://api.getlykia.com/v1",
    api_key=os.environ["LYKIA_API_KEY"],
    default_headers={
        "HTTP-Referer": "https://example.com",
        "X-Title": "Example Support Bot",
    },
)

completion = client.chat.completions.create(
    model="openai/gpt-4o-mini",
    messages=[{"role": "user", "content": "Hello!"}],
)
print(completion.choices[0].message.content)
```

**TypeScript**

```typescript
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.getlykia.com/v1",
  apiKey: process.env.LYKIA_API_KEY,
  defaultHeaders: {
    "HTTP-Referer": "https://example.com",
    "X-Title": "Example Support Bot",
  },
});

const completion = await client.chat.completions.create({
  model: "openai/gpt-4o-mini",
  messages: [{ role: "user", content: "Hello!" }],
});
console.log(completion.choices[0].message.content);
```

## Errors

A failed request returns an HTTP status code and a body like this:

```json
{
  "error": {
    "message": "Insufficient credits. Add credits to your balance, or add your own provider key (BYOK) to this project.",
    "type": "invalid_request_error",
    "code": "insufficient_credits",
    "status": 402,
    "metadata": {
      "reason": "insufficient_credits"
    }
  }
}
```

- `message`: a human-readable explanation in English. Don't branch on its wording.
- `type`: the OpenAI-style error type, such as `invalid_request_error`, `authentication_error` or `rate_limit_error`.
- `code`: a stable machine code as a string, such as `insufficient_credits`, `invalid_api_key` or `preset_not_found`. Branch on this.
- `status`: the HTTP status code as a number.
- `metadata`: always has `reason`, the stable error class (`invalid_request`, `unauthorized`, `insufficient_credits`…); `provider` when a provider caused the error, and `retry_after` in seconds when it is worth waiting.

Every status code, and which ones are worth retrying, is listed under [Errors](https://www.getlykia.com/en/docs/guides/errors).

## Response headers

| Header                 | When                                                                                                      | Meaning                                                                                                                                                                                                                                                           |
| ---------------------- | --------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `X-Generation-Id`      | Every response, including errors and early rejections such as `401`, `413`, `429` and an invalid body     | An id for the request. On `POST /chat/completions` it is the same as the `id` field of the response. A record you can fetch with [`GET /generation`](https://www.getlykia.com/en/docs/api-reference/generation) exists only for requests that reached a provider. |
| `X-Max-Tokens-Applied` | `POST /chat/completions`, when Lykia set or lowered the output limit                                      | The output limit Lykia sent to the provider, so that the request's cost fits what is available. See [Output limit](https://www.getlykia.com/en/docs/api-reference/chat-completions#output-limit).                                                                 |
| `X-Lykia-Cost`         | Image, speech and transcription responses, and the content of a completed video                           | The request's cost in USD, the same as `usage.cost`.                                                                                                                                                                                                              |
| `X-Lykia-Preset`       | `POST /chat/completions`, `POST /embeddings` and the media endpoints, when `model` was a preset (`@name`) | The preset's name. The response's `model` field is the real model. See [Model presets](https://www.getlykia.com/en/docs/guides/model-presets).                                                                                                                    |
| `X-Lykia-Reasoning`    | `POST /chat/completions`, value `dropped`                                                                 | Lykia turned thinking off for this request because an assistant message in a tool loop came back without its `reasoning_details`. See [Reasoning and prompt caching](https://www.getlykia.com/en/docs/guides/reasoning-and-caching).                              |
| `Retry-After`          | `429` from the per-key rate limit                                                                         | Seconds to wait before sending again.                                                                                                                                                                                                                             |

Responses are sent with `Cache-Control: no-store`. The API answers cross-origin requests from any origin and exposes the headers above to browser code, but keep API keys out of browsers: send requests from your own server.

## Endpoints

- [POST /chat/completions](https://www.getlykia.com/en/docs/api-reference/chat-completions): Generate a chat response, with streaming, tool calls, structured output, model fallbacks and provider preferences.
- [POST /embeddings](https://www.getlykia.com/en/docs/api-reference/embeddings): Turn text into vectors for search, clustering and retrieval.
- [POST /images/generations](https://www.getlykia.com/en/docs/api-reference/images-generations): Generate images, or edit images you send.
- [POST /videos](https://www.getlykia.com/en/docs/api-reference/videos-create): Start a video job; follow it with [GET /videos/{id}](https://www.getlykia.com/en/docs/api-reference/videos-get) and download it with [GET /videos/{id}/content](https://www.getlykia.com/en/docs/api-reference/videos-content).
- [POST /audio/speech](https://www.getlykia.com/en/docs/api-reference/audio-speech): Turn text into speech, with ready-made or cloned [voices](https://www.getlykia.com/en/docs/api-reference/audio-voices-list).
- [POST /audio/transcriptions](https://www.getlykia.com/en/docs/api-reference/audio-transcriptions): Turn audio into text, timestamps or subtitles.
- [GET /models](https://www.getlykia.com/en/docs/api-reference/models): The model catalog: ids, context length, supported parameters and prices.
- [GET /providers](https://www.getlykia.com/en/docs/api-reference/providers): The providers and links to their policies.
- [GET /key](https://www.getlykia.com/en/docs/api-reference/key): The label, spending limit and remaining amount of the key you are using.
- [GET /credits](https://www.getlykia.com/en/docs/api-reference/credits): Your company's credit balance, total usage, and what is reserved and available.
- [GET /generation](https://www.getlykia.com/en/docs/api-reference/generation): The record of a single request: cost, tokens, provider and timing.
- [GET /generations](https://www.getlykia.com/en/docs/api-reference/generations): The project's request records, newest first, filtered by user, preset, model or metadata.
