OpenAI-Compatible Embeddings API with AI Gateway
Generate vector embeddings from input text using the OpenAI-compatible /embeddings endpoint, for semantic search, similarity matching, and retrieval-augmented generation (RAG).
For an overview of embedding models and the AI SDK (embed, embedMany), see Embeddings. This page covers the OpenAI-compatible REST endpoint.
POST /embeddings
import OpenAI from 'openai';
const apiKey = process.env.AI_GATEWAY_API_KEY || process.env.VERCEL_OIDC_TOKEN;
const openai = new OpenAI({
apiKey,
baseURL: 'https://ai-gateway.vercel.sh/v1',
});
const response = await openai.embeddings.create({
model: 'openai/text-embedding-3-small',
input: 'Sunny day at the beach',
});
console.log(response.data[0].embedding);import os
from openai import OpenAI
api_key = os.getenv("AI_GATEWAY_API_KEY") or os.getenv("VERCEL_OIDC_TOKEN")
client = OpenAI(
api_key=api_key,
base_url="https://ai-gateway.vercel.sh/v1",
)
response = client.embeddings.create(
model="openai/text-embedding-3-small",
input="Sunny day at the beach",
)
print(response.data[0].embedding)curl -X POST "https://ai-gateway.vercel.sh/v1/embeddings" \
-H "Authorization: Bearer $AI_GATEWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/text-embedding-3-small",
"input": "Sunny day at the beach"
}'The following response truncates the embedding vector for readability:
{
"object": "list",
"data": [
{
"object": "embedding",
"index": 0,
"embedding": [-0.0038, 0.021]
}
],
"model": "openai/text-embedding-3-small",
"usage": {
"prompt_tokens": 6,
"total_tokens": 6
},
"providerMetadata": {
"gateway": {
"routing": {},
"cost": "0.00000012"
}
}
}You can set the root-level dimensions field (from the OpenAI Embeddings API spec) and the gateway will auto-map it to each provider's expected field; providerOptions.[provider] still passes through as-is and isn't required for dimensions to work.
import OpenAI from 'openai';
const openai = new OpenAI({
apiKey: process.env.AI_GATEWAY_API_KEY,
baseURL: 'https://ai-gateway.vercel.sh/v1',
});
const response = await openai.embeddings.create({
model: 'openai/text-embedding-3-small',
input: 'Sunny day at the beach',
dimensions: 768,
});import os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv('AI_GATEWAY_API_KEY'),
base_url='https://ai-gateway.vercel.sh/v1'
)
response = client.embeddings.create(
model='openai/text-embedding-3-small',
input='Sunny day at the beach',
dimensions=768,
)curl -X POST "https://ai-gateway.vercel.sh/v1/embeddings" \
-H "Authorization: Bearer $AI_GATEWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/text-embedding-3-small",
"input": "Sunny day at the beach",
"dimensions": 768
}'Was this helpful?