Skip to content
GitHubXDiscordRSS

AiSearchToken

Learn how to create and manage Cloudflare AI Search service tokens for authenticating with the AI Search API using Alchemy.

The AiSearchToken resource creates a service token for authenticating with Cloudflare AI Search. This token is required for AI Search to access your R2 buckets or other data sources.

Use AiSearchToken explicitly when you need to:

  • Share a single token across multiple AI Search instances
  • Have fine-grained control over token lifecycle
  • Inspect token properties (e.g., tokenId, cfApiId)

Create an AI Search token and use it with an AI Search instance:

import { AiSearch, AiSearchToken, R2Bucket } from "alchemy/cloudflare";
const bucket = await R2Bucket("docs", { name: "my-docs" });
const token = await AiSearchToken("search-token", {
name: "docs-search-token",
});
const search = await AiSearch("docs-search", {
source: {
type: "r2",
bucket,
token,
},
});

Use a single token for multiple AI Search instances:

import { AiSearch, AiSearchToken, R2Bucket } from "alchemy/cloudflare";
const docsToken = await AiSearchToken("shared-token", {
name: "shared-docs-token",
});
const docsBucket = await R2Bucket("docs", { name: "docs-bucket" });
const blogBucket = await R2Bucket("blog", { name: "blog-bucket" });
const docsSearch = await AiSearch("docs-search", {
source: { type: "r2", bucket: docsBucket, token: docsToken },
});
const blogSearch = await AiSearch("blog-search", {
source: { type: "r2", bucket: blogBucket, token: docsToken },
});

Adopt a token that already exists in your account:

import { AiSearchToken } from "alchemy/cloudflare";
const token = await AiSearchToken("existing-token", {
name: "my-existing-token",
adopt: true,
});

When you create an AiSearchToken, Alchemy:

  1. Creates an account API token with the required permissions:
    • AI Search Index Engine — allows AI Search to index and query data
    • Workers R2 Storage Write — allows AI Search to read from R2 buckets
  2. Registers the token with the AI Search service
  3. Returns the token details including the tokenId and cfApiKey

When the resource is destroyed, both the AI Search token registration and the underlying account API token are cleaned up.

PropertyTypeDefaultDescription
namestringresource IDName of the token
adoptbooleanfalseAdopt an existing token with the same name
deletebooleantrueDelete the token when removed from Alchemy
PropertyTypeDescription
tokenIdstringThe AI Search token ID (UUID)
accountTokenIdstringThe underlying account API token ID
accountIdstringThe Cloudflare account ID
accountTagstringThe Cloudflare account tag
namestringName of the token
cfApiIdstringThe CF API ID for this token
cfApiKeySecretThe CF API key (stored securely)
enabledbooleanWhether the token is enabled
createdAtstringWhen the token was created
modifiedAtstringWhen the token was last modified