Skip to content

AiSearchToken

Source: src/Cloudflare/AiSearch/AiSearchToken.ts

A Cloudflare AI Search service token — the credential AI Search uses to access your data source (R2 bucket, Vectorize index, Workers AI) when indexing.

The token wraps an existing Cloudflare API token (cfApiId + cfApiKey). That API token must carry the “AI Search Index Engine” permission group — Cloudflare validates the credential on create and update and rejects tokens without it. Pair it with Cloudflare.AccountApiToken to mint the underlying API token in the same stack, then reference the service token’s id from an AI Search instance’s tokenId prop.

const apiToken = yield* Cloudflare.AccountApiToken("SearchTokenSource", {
policies: [
{
effect: "allow",
permissionGroups: ["AI Search Index Engine"],
resources: { [`com.cloudflare.api.account.${accountId}`]: "*" },
},
],
});
const token = yield* Cloudflare.AiSearchToken("SearchToken", {
cfApiId: apiToken.tokenId,
cfApiKey: apiToken.value,
});
const search = yield* Cloudflare.AiSearchInstance("Search", {
source: bucket.bucketName,
tokenId: token.id,
});