|
| 1 | +--- |
| 2 | +title: AiSearchToken |
| 3 | +description: Learn how to create and manage Cloudflare AI Search service tokens for authenticating with the AI Search API using Alchemy. |
| 4 | +--- |
| 5 | + |
| 6 | +The AiSearchToken resource creates a service token for authenticating with [Cloudflare AI Search](https://developers.cloudflare.com/ai-search/). This token is required for AI Search to access your R2 buckets or other data sources. |
| 7 | + |
| 8 | +:::note |
| 9 | +In most cases, you don't need to create this resource directly. The [AiSearch](/providers/cloudflare/ai-search) resource automatically detects existing tokens or creates one for you. |
| 10 | +::: |
| 11 | + |
| 12 | +## When to Use |
| 13 | + |
| 14 | +Use `AiSearchToken` explicitly when you need to: |
| 15 | + |
| 16 | +- Share a single token across multiple AI Search instances |
| 17 | +- Have fine-grained control over token lifecycle |
| 18 | +- Inspect token properties (e.g., `tokenId`, `cfApiId`) |
| 19 | + |
| 20 | +## Minimal Example |
| 21 | + |
| 22 | +Create an AI Search token and use it with an AI Search instance: |
| 23 | + |
| 24 | +```ts |
| 25 | +import { AiSearch, AiSearchToken, R2Bucket } from "alchemy/cloudflare"; |
| 26 | + |
| 27 | +const bucket = await R2Bucket("docs", { name: "my-docs" }); |
| 28 | + |
| 29 | +const token = await AiSearchToken("search-token", { |
| 30 | + name: "docs-search-token", |
| 31 | +}); |
| 32 | + |
| 33 | +const search = await AiSearch("docs-search", { |
| 34 | + source: { |
| 35 | + type: "r2", |
| 36 | + bucket, |
| 37 | + token, |
| 38 | + }, |
| 39 | +}); |
| 40 | +``` |
| 41 | + |
| 42 | +## Share Token Across Instances |
| 43 | + |
| 44 | +Use a single token for multiple AI Search instances: |
| 45 | + |
| 46 | +```ts |
| 47 | +import { AiSearch, AiSearchToken, R2Bucket } from "alchemy/cloudflare"; |
| 48 | + |
| 49 | +const docsToken = await AiSearchToken("shared-token", { |
| 50 | + name: "shared-docs-token", |
| 51 | +}); |
| 52 | + |
| 53 | +const docsBucket = await R2Bucket("docs", { name: "docs-bucket" }); |
| 54 | +const blogBucket = await R2Bucket("blog", { name: "blog-bucket" }); |
| 55 | + |
| 56 | +const docsSearch = await AiSearch("docs-search", { |
| 57 | + source: { type: "r2", bucket: docsBucket, token: docsToken }, |
| 58 | +}); |
| 59 | + |
| 60 | +const blogSearch = await AiSearch("blog-search", { |
| 61 | + source: { type: "r2", bucket: blogBucket, token: docsToken }, |
| 62 | +}); |
| 63 | +``` |
| 64 | + |
| 65 | +## Adopt Existing Token |
| 66 | + |
| 67 | +Adopt a token that already exists in your account: |
| 68 | + |
| 69 | +```ts |
| 70 | +import { AiSearchToken } from "alchemy/cloudflare"; |
| 71 | + |
| 72 | +const token = await AiSearchToken("existing-token", { |
| 73 | + name: "my-existing-token", |
| 74 | + adopt: true, |
| 75 | +}); |
| 76 | +``` |
| 77 | + |
| 78 | +## How It Works |
| 79 | + |
| 80 | +When you create an `AiSearchToken`, Alchemy: |
| 81 | + |
| 82 | +1. Creates an **account API token** with the required permissions: |
| 83 | + - `AI Search Index Engine` — allows AI Search to index and query data |
| 84 | + - `Workers R2 Storage Write` — allows AI Search to read from R2 buckets |
| 85 | +2. Registers the token with the **AI Search service** |
| 86 | +3. Returns the token details including the `tokenId` and `cfApiKey` |
| 87 | + |
| 88 | +When the resource is destroyed, both the AI Search token registration and the underlying account API token are cleaned up. |
| 89 | + |
| 90 | +## Configuration Options |
| 91 | + |
| 92 | +| Property | Type | Default | Description | |
| 93 | +|----------|------|---------|-------------| |
| 94 | +| `name` | `string` | resource ID | Name of the token | |
| 95 | +| `adopt` | `boolean` | `false` | Adopt an existing token with the same name | |
| 96 | +| `delete` | `boolean` | `true` | Delete the token when removed from Alchemy | |
| 97 | + |
| 98 | +## Output Properties |
| 99 | + |
| 100 | +| Property | Type | Description | |
| 101 | +|----------|------|-------------| |
| 102 | +| `tokenId` | `string` | The AI Search token ID (UUID) | |
| 103 | +| `accountTokenId` | `string` | The underlying account API token ID | |
| 104 | +| `accountId` | `string` | The Cloudflare account ID | |
| 105 | +| `accountTag` | `string` | The Cloudflare account tag | |
| 106 | +| `name` | `string` | Name of the token | |
| 107 | +| `cfApiId` | `string` | The CF API ID for this token | |
| 108 | +| `cfApiKey` | `Secret` | The CF API key (stored securely) | |
| 109 | +| `enabled` | `boolean` | Whether the token is enabled | |
| 110 | +| `createdAt` | `string` | When the token was created | |
| 111 | +| `modifiedAt` | `string` | When the token was last modified | |
0 commit comments