Skip to content

Workers binding

Workers provides a serverless execution environment that allows you to create new applications or augment existing ones. Use a Workers binding to upload, list, and manage documents in your AI Search instances from a Cloudflare Worker. Access the Items API through the items property on an instance handle.

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.

JSONC
{
"$schema": "./node_modules/wrangler/config-schema.json",
"compatibility_date": "2026-03-27",
"ai_search_namespaces": [
{
"binding": "AI_SEARCH",
"namespace": "my-namespace"
}
]
}
FieldTypeRequiredDescription
bindingstringYesThe variable name available on env. For example, "AI_SEARCH" makes it accessible as env.AI_SEARCH.
namespacestringYesThe namespace to bind to. A default namespace is created automatically for every account. If the namespace does not exist, Wrangler creates it on deploy.
remotebooleanNoSet 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.

JSONC
{
"$schema": "./node_modules/wrangler/config-schema.json",
"compatibility_date": "2026-03-27",
"ai_search": [
{
"binding": "MY_SEARCH",
"instance_name": "my-instance"
}
]
}
FieldTypeRequiredDescription
bindingstringYesThe variable name available on env. For example, "MY_SEARCH" makes it accessible as env.MY_SEARCH.
instance_namestringYesThe name of the AI Search instance. Must exist in the default namespace at deploy time.
remotebooleanNoSet to true for local development with wrangler dev.

Methods

The Items API methods are available on both the ai_search_namespaces and ai_search bindings. With the namespace binding, call methods on the handle returned by get(). With the instance binding, call methods directly on the binding (for example, env.MY_SEARCH.items.upload()).

The examples below use the namespace binding.

TypeScript
const instance = env.AI_SEARCH.get("my-instance");

items.upload()

Uploads a document for indexing. Returns immediately. The document is queued for processing.

TypeScript
// Upload from a string
await instance.items.upload(
"faq.md",
"# FAQ\n\nQ: How do I reset my password?\nA: Go to Settings > Security...",
);
// Upload from an ArrayBuffer
const pdfResponse = await fetch("https://example.com/guide.pdf");
const pdfBuffer = await pdfResponse.arrayBuffer();
await instance.items.upload("guide.pdf", pdfBuffer);
// Upload from a ReadableStream
await instance.items.upload("doc.txt", request.body);

Upload with metadata

Attach custom metadata to a document for filtering in search queries. Custom metadata fields must be defined on the instance first using the update() method or at creation time.

TypeScript
await instance.items.upload("guide.pdf", pdfBuffer, {
metadata: {
category: "onboarding",
language: "en",
version: "2.0",
},
});

Parameters

ParameterTypeRequiredDescription
namestringYesThe filename for the uploaded document. Used as the item key.
contentReadableStream, ArrayBuffer, or stringYesThe document content. Maximum file size is 4 MB. Pass a string for plain text or markdown, an ArrayBuffer for binary files, or a ReadableStream for streaming uploads.
options.metadataRecord<string, string>NoCustom metadata key-value pairs to attach to the item. Use for filtering in search queries. Maximum 5 fields per instance.

Response

FieldTypeDescription
idstringThe unique item identifier.
keystringThe filename or key of the item.

items.uploadAndPoll()

Uploads a document and polls until processing completes or the timeout is reached. Use this when you need to search the document immediately after upload.

TypeScript
// Wait for a specific document to finish indexing before searching
const item = await instance.items.uploadAndPoll(
"handbook.txt",
handbookContent,
);
console.log(`handbook.txt status: ${item.status}`); // "completed"
// Now search across all uploaded documents
const results = await instance.search({
messages: [{ role: "user", content: "password reset policy" }],
});

Parameters

Same as items.upload(), with additional polling options:

ParameterTypeRequiredDescription
options.pollIntervalMsnumberNoHow often to check the item status, in milliseconds. Defaults to 1000.
options.timeoutMsnumberNoMaximum time to wait for processing to complete, in milliseconds. Defaults to 30000.

Response

Returns the full item object after polling completes:

FieldTypeDescription
idstringThe unique item identifier.
keystringThe filename or key of the item.
statusstringThe processing status: queued, running, completed, error, skipped, outdated.
chunks_countnumberNumber of chunks created from the document.
file_sizenumberSize of the uploaded file in bytes.
metadataobjectItem metadata including filename, folder, and timestamp.
source_idstringThe source identifier (for example, builtin for uploaded files).
created_atstringTimestamp of when the item was created.
last_seen_atstringTimestamp of when the item was last seen during indexing.

items.list()

Returns a paginated list of items in the instance.

TypeScript
const { result, result_info } = await instance.items.list();
for (const item of result) {
console.log(`${item.key} (${item.status})`);
}
// result_info.total_count contains the total number of items

Parameters

ParameterTypeRequiredDescription
pagenumberNoThe page number to return. Defaults to 1.
per_pagenumberNoThe number of items per page. Defaults to 20. Maximum 50.
statusstringNoFilter by processing status: queued, running, completed, error, skipped, or outdated.
sort_bystringNoSort order for items: status (default) or modified_at.
searchstringNoSearch items by text content.
sourcestringNoFilter by source identifier (for example, builtin for uploaded files).

Response

FieldTypeDescription
resultarrayArray of item objects.
result[].idstringThe unique item identifier.
result[].keystringThe filename or key of the item.
result[].statusstringThe processing status: queued, running, completed, error, skipped, outdated.
result[].chunks_countnumberNumber of chunks created from the document.
result[].file_sizenumberSize of the uploaded file in bytes.
result[].metadataobjectItem metadata including filename, folder, and timestamp.
result[].source_idstringThe source identifier (for example, builtin for uploaded files).
result[].created_atstringTimestamp of when the item was created.
result[].last_seen_atstringTimestamp of when the item was last seen during indexing.
result_infoobjectPagination metadata.
result_info.countnumberNumber of items in the current page.
result_info.total_countnumberTotal number of items in the instance.
result_info.pagenumberThe current page number.
result_info.per_pagenumberItems per page.

items.delete()

Deletes an item and its indexed chunks.

TypeScript
await instance.items.delete("item-id-123");

Parameters

ParameterTypeRequiredDescription
itemIdstringYesThe unique identifier of the item to delete.

Response

Returns void. Throws an error if the item does not exist.

items.get()

Returns a handle to a specific item for retrieving its status or downloading the original file.

items.get().info()

Returns the status and metadata of a specific item.

TypeScript
const itemInfo = await instance.items.get("item-id-123").info();
Parameters
ParameterTypeRequiredDescription
itemIdstringYesThe unique identifier of the item.
Response
FieldTypeDescription
idstringThe unique item identifier.
keystringThe filename or key of the item.
statusstringThe processing status: queued, running, completed, error, skipped, outdated.
chunks_countnumberNumber of chunks created from the document.
file_sizenumberSize of the uploaded file in bytes.
metadataobjectItem metadata including filename, folder, and timestamp.
source_idstringThe source identifier (for example, builtin for uploaded files).
created_atstringTimestamp of when the item was created.
last_seen_atstringTimestamp of when the item was last seen during indexing.

items.get().download()

Downloads the original source file for an item.

TypeScript
const file = await instance.items.get("item-id-123").download();
// file.body is a ReadableStream
Parameters
ParameterTypeRequiredDescription
itemIdstringYesThe unique identifier of the item.
Response
FieldTypeDescription
filenamestringThe original filename.
contentTypestringThe MIME type of the file (for example, application/pdf).
sizenumberThe file size in bytes.
bodyReadableStreamA readable stream of the file contents.