Workers binding (3)
Last updated Oct 1, 2026 provides a serverless execution environment that allows you to create new applications or augment existing ones. Use a Workers binding to create, list, update, and delete AI Search instances from a Cloudflare Worker. You can also check instance configuration and monitor indexing progress.
Configure the binding
To use AI Search with Workers, you must create an AI Search binding. You create bindings by updating your Wrangler configuration. AI Search provides two types of bindings:
- Namespace binding:
ai_search_namespaces - Instance binding:
ai_search
Namespace binding
Access all instances within a namespace. You can get, create, list, and delete instances at runtime.
{
"$schema": "./node_modules/wrangler/config-schema.json",
"compatibility_date": "2026-03-27",
"ai_search_namespaces": [
{
"binding": "AI_SEARCH",
"namespace": "my-namespace"
}
]
}
compatibility_date = "2026-03-27"
[[ai_search_namespaces]]
binding = "AI_SEARCH"
namespace = "my-namespace"
| Field | Type | Required | Description |
|---|---|---|---|
binding |
string | Yes | The variable name available on env. For example, "AI_SEARCH" makes it accessible as env.AI_SEARCH. |
namespace |
string | Yes | The namespace to bind to. A default namespace is created automatically for every account. If the namespace does not exist, Wrangler creates it on deploy. |
remote |
boolean | No | Set to true for local development with wrangler dev. |
Instance binding
Bind directly to a single instance in the default namespace. Use this when you know which instance you need at deploy time.
{
"$schema": "./node_modules/wrangler/config-schema.json",
"compatibility_date": "2026-03-27",
"ai_search": [
{
"binding": "MY_SEARCH",
"instance_name": "my-instance"
}
]
}
compatibility_date = "2026-03-27"
[[ai_search]]
binding = "MY_SEARCH"
instance_name = "my-instance"
| Field | Type | Required | Description |
|---|---|---|---|
binding |
string | Yes | The variable name available on env. For example, "MY_SEARCH" makes it accessible as env.MY_SEARCH. |
instance_name |
string | Yes | The name of the AI Search instance. Must exist in the default namespace at deploy time. |
remote |
boolean | No | Set to true for local development with wrangler dev. |
Namespace methods
The following methods are only available when using the ai_search_namespaces binding. The namespace handle (env.AI_SEARCH) exposes methods for working with instances within a namespace.
get()
Returns a handle to a specific instance. This is synchronous and does not make a network call. The instance is resolved lazily when you call methods like search() or info().
const instance = env.AI_SEARCH.get("my-instance");
const results = await instance.search({
messages: [{ role: "user", content: "What is Cloudflare?" }],
});
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
name |
string | Yes | The name of the instance to get a handle to. |
list()
Returns all instances within the namespace.
const { result, result_info } = await env.AI_SEARCH.list();
for (const instance of result) {
console.log(`${instance.id} (${instance.type}) - ${instance.status}`);
}
// result_info.total_count contains the total number of instances
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
page |
number | No | The page number to return. Defaults to 1. |
per_page |
number | No | The number of instances per page. Defaults to 20. Maximum 100. |
search |
string | No | Search instances by ID. |
order_by |
string | No | Sort column. Valid value: created_at. Defaults to created_at. |
order_by_direction |
string | No | Sort direction. Valid values: asc, desc. Defaults to desc. |
Response
| Field | Type | Description |
|---|---|---|
result |
array | Array of instance objects. |
result[].id |
string | The instance identifier. |
result[].type |
string | The data source type (r2, web-crawler, or null for empty instances). |
result[].source |
string | The data source location. |
result[].status |
string | The current indexing lifecycle status, such as waiting, index_source, embed_files, or a paused_* status. |
result[].paused |
boolean | Whether indexing is paused. |
result[].namespace |
string | The namespace the instance belongs to. |
result[].created_at |
string | ISO 8601 timestamp of when the instance was created. |
result[].modified_at |
string | ISO 8601 timestamp of the last modification. |
result_info |
object | Pagination metadata. |
result_info.total_count |
number | Total number of instances in the namespace. |
create()
Creates a new instance and returns a handle to it. You can create instances backed by a data source or create empty instances for use with the Items API.
Create an empty instance for file uploads:
AI Search instances come with built-in storage where you can upload documents directly.
const instance = await env.AI_SEARCH.create({
id: "knowledge-base",
});
// Upload documents using the Items API
await instance.items.upload("guide.pdf", pdfBlob);
Create a web-crawler instance:
Automatically crawl and index a website that you own. For more configuration options, refer to Website data source.
const instance = await env.AI_SEARCH.create({
id: "my-docs",
type: "web-crawler",
source: "developers.cloudflare.com",
});
Create an R2-backed instance:
Index documents stored in an R2 bucket. For more configuration options, refer to R2 data source.
const instance = await env.AI_SEARCH.create({
id: "internal-docs",
type: "r2",
source: "my-docs-bucket",
});
Parameters
id string required
The unique identifier for the AI Search instance. Must be 1-64 characters and match the pattern ^[a-z0-9_]+(?:-[a-z0-9_]+)*$.
type string optional
The type of data source. Valid values: r2 or web-crawler. If omitted, an HTTP(S) URL source selects web-crawler, an existing R2 bucket selects r2, and a missing or blank source creates an instance with built-in storage. Other sources return error 7111. The REST API also accepts null, which behaves like an omitted value. The Workers binding does not accept null.
source string optional
The data source location. For r2 type, this is the R2 bucket name. For web-crawler type, this is the website domain. Required when type is specified.
source_params object optional
Additional parameters for the data source.
prefixstringoptional- For R2 sources, limits indexing to objects with this key prefix.
r2_jurisdictionstringoptional- The jurisdiction for the R2 bucket, for example
eu. include_itemsarrayoptional- Glob patterns for paths to include in indexing. For example:
["/blog/**", "/docs/**/*.html"]. exclude_itemsarrayoptional- Glob patterns for paths to exclude from indexing. For example:
["/admin/**", "/private/**"]. web_crawlerobjectoptional- Configuration for web crawler sources.
parse_typestringoptional- How pages are discovered. Valid values:
sitemap(reads XML sitemaps),discover(starts at the source URL and, by default, uses both sitemaps and links found on crawled pages). Defaults tositemap. Refer to Parse types.
- How pages are discovered. Valid values:
parse_optionsobjectoptionalinclude_headersobjectoptional- Custom HTTP headers to include when crawling.
include_imagesbooleanoptional- Whether to include images in the index.
specific_sitemapsarrayoptional- Specific sitemap URLs to crawl. For example:
["https://example.com/sitemap.xml"]. Only valid whenparse_typeissitemap. use_browser_renderingbooleanoptional- Use Browser Run (formerly Browser Rendering) to crawl JavaScript-rendered pages.
discover_optionsobjectoptional- Crawl settings that apply when
parse_typeisdiscover. sourcestringoptional- Where the crawler looks for candidate URLs. Valid values:
all,sitemaps,links. Defaults toall. limitnumberoptional- Maximum number of pages to crawl. Valid values:
1to100000. Defaults to100000. depthnumberoptional- Maximum number of link hops to follow from the source URL. Valid values:
1to100000. Defaults to5. max_agenumberoptional- How long, in seconds, the crawler reuses cached page content before it re-fetches from the origin. Valid values:
0to604800. Defaults to86400. include_external_linksbooleanoptional- Whether to follow links that point to other domains. Defaults to
false. include_subdomainsbooleanoptional- Whether to follow links that point to subdomains of the source URL. Defaults to
false.
- Crawl settings that apply when
store_optionsobjectoptionalstorage_typestringoptional- The storage type. Valid value:
r2. storage_idstringoptional- The storage bucket ID.
r2_jurisdictionstringoptional- The jurisdiction for the storage bucket.
index_method object optional
Configures which indexing methods are enabled for the instance. Determines whether vector (semantic) search, keyword search, or both are available. At least one must be true.
vectorbooleanoptional- Enable vector-based semantic search. Defaults to
true. keywordbooleanoptional- Enable keyword-based search. Defaults to
true.
Set both to true for hybrid search.
fusion_method string optional
Controls how vector and keyword scores are combined when using hybrid search. Valid values: rrf (Reciprocal Rank Fusion), max (takes the maximum score). Defaults to rrf.
indexing_options object optional
Configuration for how content is indexed.
keyword_tokenizerstringoptional- The tokenizer used for keyword search indexing. Valid values:
porter(stemming-based),trigram(character n-gram). Defaults toporter. use_ocrbooleanoptional- Use optical character recognition (OCR) when ingesting PDFs and images. Defaults to
false. OCR is available on every account and is billed as image-processing ingestion tokens.
retrieval_options object optional
Default retrieval configuration for the instance. These defaults can be overridden per-request using ai_search_options.
keyword_match_modestringoptional- Controls how keyword (BM25) matching selects candidate documents.
andrequires all terms to match.orrequires any term to match. Defaults toand. boost_byarrayoptional- Default boost fields applied to all search queries. Maximum 3 items. Each item has:
fieldstringrequired - The metadata field name to boost by. Maximum 64 characters.directionstringoptional - The boost direction. Valid values:asc,desc,exists,not_exists.
sync_interval number optional
Seconds between automatic data source syncs. Valid values: 3600, 7200, 14400, 21600, 43200, 86400. Defaults to 21600 (6 hours).
token_id string optional
The UUID of the service API token to use for this instance. Only required if you have never created an AI Search instance before. Refer to the API get started guide for how to create and register a service token.
