> ## Documentation Index
> Fetch the complete documentation index at: https://docs.fanifyai.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Embeddings

> Turn text into vectors for search, memory, and retrieval with an OpenAI-compatible route.

An embeddings model turns a text into a vector of numbers. Texts with close meanings get close vectors: compare them to search a knowledge base, retrieve memories, or group similar content.

`POST /api/v1/hub/embeddings` computes one or more embeddings synchronously, with the OpenAI Embeddings contract. A client built with an OpenAI SDK only needs to change its base URL to `https://fanifyai.com/api/v1/hub`.

## Compute embeddings

```bash cURL theme={null}
curl https://fanifyai.com/api/v1/hub/embeddings \
  -H "Authorization: Bearer $FANIFY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "text-embedding-3-small",
    "input": ["The fan likes hiking.", "The fan works as a nurse."],
    "encoding_format": "float"
  }'
```

```python Python theme={null}
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["FANIFY_API_KEY"],
    base_url="https://fanifyai.com/api/v1/hub",
)

result = client.embeddings.create(
    model="text-embedding-3-small",
    input=["The fan likes hiking.", "The fan works as a nurse."],
)
vectors = [item.embedding for item in result.data]
```

```csharp .NET theme={null}
using System.ClientModel;
using OpenAI;
using OpenAI.Embeddings;

var client = new EmbeddingClient(
    "text-embedding-3-small",
    new ApiKeyCredential(Environment.GetEnvironmentVariable("FANIFY_API_KEY")!),
    new OpenAIClientOptions { Endpoint = new Uri("https://fanifyai.com/api/v1/hub") });

OpenAIEmbedding embedding = await client.GenerateEmbeddingAsync("The fan likes hiking.");
ReadOnlyMemory<float> vector = embedding.ToFloats();
```

Find embeddings models in `GET /api/v1/hub/models`: their `output_modalities` contain `embeddings`. `text-embedding-3-small` returns vectors of 1,536 dimensions. Call an embeddings model through this route only; the other routes, including `POST /api/v1/hub/generations`, reject it with `400 invalid_input`, and this route rejects chat, decision, transcription, and media models the same way.

## Request

| Field | Required | Content |
| - | - | - |
| `model` | yes | Embeddings model `id` from the catalog |
| `input` | yes | A non-empty string, or an array of 1 to 2,048 non-empty items |
| `encoding_format` | no | `float` (array of numbers) or `base64` |
| `dimensions` | no | Number of dimensions of the output vectors, for models that support it |
| `input_type` | no | Purpose of the input for models that support it, for example `search_query` or `search_document` |
| `user` | no | Identifier of your end user |

An `input` array can contain strings, arrays of tokens, or arrays of strings: each element gives one embedding. The Hub checks only `model`, `input`, and `stream`; the other fields are forwarded unchanged and the model validates them. `provider` is ignored: the Hub chooses the route. `stream` is refused: embeddings are not streamed.

### Batches

Send up to 2,048 texts in one `input` array. A batch is one call: one generation, one charge, and one vector per text in `data`, identified by `index`. Batching many short texts is faster than sending them one by one.

### `float` or `base64`

With `float`, each `embedding` is an array of numbers. With `base64`, it is a string that encodes the same values as little-endian 32-bit floats: the response is about four times smaller. Without `encoding_format`, the model's default applies. The official OpenAI SDKs request `base64` and decode it for you, so your code always reads numbers.

To decode `base64` yourself:

```python theme={null}
import base64
import numpy as np

vector = np.frombuffer(base64.b64decode(item["embedding"]), dtype="<f4")
```

## Response

```json theme={null}
{
  "id": "0192f3a4-8c1e-7b2a-9f10-2b6d4e0c9a11",
  "object": "list",
  "model": "text-embedding-3-small",
  "data": [
    { "object": "embedding", "index": 0, "embedding": [0.0023064255, -0.009327292, 0.0151820565] },
    { "object": "embedding", "index": 1, "embedding": [-0.0112453, 0.0048711, 0.0207634] }
  ],
  "usage": { "prompt_tokens": 14, "total_tokens": 14, "cost": 0.000001 }
}
```

The vectors are shortened in this example: `text-embedding-3-small` returns 1,536 values per vector.

The model's response is forwarded as is, with these changes: `id` is the Hub generation identifier, `model` is the catalog `id`, `usage.cost` is the billed amount in EUR, and provider details are removed. `data` is never modified.

A `200` response also includes:

```text theme={null}
X-Fanify-Generation-Id: 0192f3a4-8c1e-7b2a-9f10-2b6d4e0c9a11
X-Fanify-Billed-Cost: 0.000001
```

`X-Fanify-Billed-Cost` is the billed amount in EUR, always written with a dot as decimal separator. To read it with the Python SDK, use `client.embeddings.with_raw_response.create(...)`, then `raw.headers["X-Fanify-Billed-Cost"]` and `raw.parse()`. With the .NET SDK, read `result.GetRawResponse().Headers`.

Each call is recorded as a generation with `is_llm: true`: it appears in `GET /api/v1/hub/generations` (excluded by `media_only=true`) and triggers the `generation.running` and `generation.completed` webhooks. The Hub never stores vectors: the generation and the webhooks contain only a summary (number of vectors, dimensions, encoding format, usage).

## Vector compatibility

> [!WARNING]
>
> Compare only vectors produced by the same model with the same `dimensions`. Vectors from another model, or from the same model with a different `dimensions`, are not comparable: a similarity computed between them means nothing. Changing the model or `dimensions` requires recomputing every vector already stored in your index.

The same model always gives the same vectors through the Hub as through its original provider: an index built elsewhere with `text-embedding-3-small` at 1,536 dimensions remains usable. A reduced `dimensions` (for example `512`) gives smaller vectors that are incompatible with an existing 1,536-dimension index.

## Idempotency

`Idempotency-Key` is not used on this route: a key you send is ignored, with no replay and no conflict. Embeddings are recomputed on retry; the cost is negligible.

## Errors

Errors use the OpenAI-compatible envelope:

```json theme={null}
{
  "error": {
    "message": "The request input is invalid.",
    "code": "invalid_input",
    "type": "invalid_request_error"
  }
}
```

`error.metadata.provider_name` is present only on a `provider_error`, when the routed model failed. Errors raised by the Hub itself (validation, credits, rate limit, pause) have no `metadata`. An authentication failure returns `401 unauthorized` as Problem Details, before the request is processed.

| HTTP | `code` | Cause |
| - | - | - |
| 400 | `invalid_input` | Invalid JSON; `model` missing; `input` missing, empty, or longer than 2,048 items; `stream` sent; or the model is not an embeddings model |
| 402 | `insufficient_credits` | The available balance does not cover the request |
| 404 | `model_not_found` | Unknown or unavailable model |
| 413 | `payload_too_large` | The request body exceeds the configured limit |
| 422 | `no_route_available` | No executable route |
| 429 | `rate_limited`, `capacity_exhausted` | Request rate limit of the key; no concurrency slot freed in time (see [Errors](https://docs.fanifyai.com/en/hub/errors.md#capacity_exhausted)). Both include `Retry-After` |
| 503 | `hub_paused` | The Hub is not accepting new requests |
| 500 | `internal_error` | Problem on the FanifyHub side; no internal detail is exposed |
| 4xx / 503 | `provider_error` | The routed model failed: its `4xx` status is kept, except `401` and `402` which become `503`; any other failure returns `503` |

Embeddings have their own request rate limit per key, separate from the other Hub routes. A failed request is never billed. See [Errors](https://docs.fanifyai.com/en/hub/errors.md) for the meaning of each code.

## Pricing

Embeddings are billed per input token, at the `input_cost_per_million` rate shown for your account in `GET /api/v1/hub/models`; `output_cost_per_million` is usually `0`. A batch is billed once, from the tokens of all its texts. The billed amount is rounded **up** to six decimal places: a call with a non-zero cost is billed at least €0.000001, never €0.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.