Skip to content

Vectorize

Vectorize is Cloudflare’s vector database — store embeddings and query for nearest neighbours (semantic search, RAG). It pairs naturally with the AI integration: generate embeddings with IEmbeddingGenerator, store and search them with IVectorizeClient.

builder.AddCloudflareEnvironment();
// dimensions must match your embedding model (e.g. 768 for @cf/baai/bge-base-en-v1.5).
var docs = builder.AddVectorizeIndex("docs", dimensions: 768, metric: VectorizeMetric.Cosine)
.RunAsEmulator();
  • aspire run → with .RunAsEmulator(), an in-process in-memory vector store backs the index. No credentials needed.
  • aspire deploy → the index is provisioned in real Vectorize via the Cloudflare API.

Supported metrics: Cosine (default), Euclidean, DotProduct.

builder.AddProject<Projects.Api>("api").WithReference(docs);
builder.AddVectorizeClient("docs");

This registers an IVectorizeClient bound to the index. The same code runs against the in-memory store locally and the Vectorize v2 HTTP API in production:

app.MapPost("/index", async (IVectorizeClient vectors, IEmbeddingGenerator<string, Embedding<float>> embedder, Doc doc, CancellationToken ct) =>
{
var vector = await embedder.GenerateVectorAsync(doc.Text, cancellationToken: ct);
await vectors.UpsertAsync(
[
new VectorRecord(doc.Id, vector, new Dictionary<string, object?> { ["title"] = doc.Title }),
], ct);
return Results.Ok();
});
app.MapGet("/search", async (string q, IVectorizeClient vectors, IEmbeddingGenerator<string, Embedding<float>> embedder, CancellationToken ct) =>
{
var query = await embedder.GenerateVectorAsync(q, cancellationToken: ct);
var matches = await vectors.QueryAsync(query, topK: 5, cancellationToken: ct);
return Results.Ok(matches.Select(m => new { m.Id, m.Score, m.Metadata }));
});

IVectorizeClient also exposes GetByIdsAsync and DeleteAsync.

Provider Backend When
InMemory In-process vector store aspire run with .RunAsEmulator()
Vectorize The Vectorize v2 HTTP API Deployed
  • API token: needs Vectorize: Edit — see API Tokens. By default the client reuses the environment’s Cloudflare API token; scope it with .WithAccessToken(parameter) if you prefer.
  • Local dev with .RunAsEmulator(): none.