Bindings Reference
Cloudwerk provides seamless access to all Cloudflare bindings through the context object.
Accessing Bindings
Section titled “Accessing Bindings”Bindings are available in loaders, handlers, and middleware:
export async function loader({ context }: LoaderArgs) { // Access bindings via context const db = context.db; // D1 Database const kv = context.kv; // KV Namespace const r2 = context.r2; // R2 Bucket const env = context.env; // Environment variables}D1 Database
Section titled “D1 Database”Cloudflare D1 is a serverless SQLite database.
Configuration
Section titled “Configuration”# wrangler.toml[[d1_databases]]binding = "DB"database_name = "my-database"database_id = "your-database-id"// Query builder (recommended)const users = await context.db .selectFrom('users') .where('status', '=', 'active') .execute();
// Raw queriesconst result = await context.env.DB .prepare('SELECT * FROM users WHERE id = ?') .bind(userId) .first();
// Batch queriesconst results = await context.env.DB.batch([ context.env.DB.prepare('SELECT * FROM users'), context.env.DB.prepare('SELECT * FROM posts'),]);interface D1Database { prepare(query: string): D1PreparedStatement; batch<T>(statements: D1PreparedStatement[]): Promise<D1Result<T>[]>; run(query: string): Promise<D1ExecResult>;}
interface D1PreparedStatement { bind(...values: unknown[]): D1PreparedStatement; first<T>(column?: string): Promise<T | null>; all<T>(): Promise<D1Result<T>>; run(): Promise<D1ExecResult>;}KV Namespace
Section titled “KV Namespace”Cloudflare Workers KV provides key-value storage.
Configuration
Section titled “Configuration”# wrangler.toml[[kv_namespaces]]binding = "KV"id = "your-kv-id"// Get valueconst value = await context.kv.get('key');const jsonValue = await context.kv.get('key', 'json');const streamValue = await context.kv.get('key', 'stream');
// Set valueawait context.kv.put('key', 'value');await context.kv.put('key', JSON.stringify(data));
// With expirationawait context.kv.put('key', 'value', { expirationTtl: 3600, // 1 hour in seconds});
// With metadataawait context.kv.put('key', 'value', { metadata: { createdAt: Date.now() },});
// Deleteawait context.kv.delete('key');
// List keysconst keys = await context.kv.list();const prefixedKeys = await context.kv.list({ prefix: 'user:' });interface KVNamespace { get(key: string, type?: 'text'): Promise<string | null>; get(key: string, type: 'json'): Promise<unknown | null>; get(key: string, type: 'arrayBuffer'): Promise<ArrayBuffer | null>; get(key: string, type: 'stream'): Promise<ReadableStream | null>;
put(key: string, value: string | ArrayBuffer | ReadableStream, options?: KVPutOptions): Promise<void>;
delete(key: string): Promise<void>;
list(options?: KVListOptions): Promise<KVListResult>;}R2 Object Storage
Section titled “R2 Object Storage”Cloudflare R2 provides S3-compatible object storage.
Configuration
Section titled “Configuration”# wrangler.toml[[r2_buckets]]binding = "R2"bucket_name = "my-bucket"// Get objectconst object = await context.r2.get('path/to/file.txt');if (object) { const text = await object.text(); const arrayBuffer = await object.arrayBuffer(); const blob = await object.blob();}
// Put objectawait context.r2.put('path/to/file.txt', 'Hello, World!');await context.r2.put('path/to/file.txt', fileStream, { httpMetadata: { contentType: 'text/plain', },});
// Delete objectawait context.r2.delete('path/to/file.txt');
// List objectsconst objects = await context.r2.list();const prefixedObjects = await context.r2.list({ prefix: 'uploads/', limit: 100,});
// Head (metadata only)const head = await context.r2.head('path/to/file.txt');interface R2Bucket { get(key: string): Promise<R2ObjectBody | null>; put(key: string, value: ReadableStream | ArrayBuffer | string, options?: R2PutOptions): Promise<R2Object>; delete(key: string): Promise<void>; list(options?: R2ListOptions): Promise<R2Objects>; head(key: string): Promise<R2Object | null>;}Queues
Section titled “Queues”Cloudflare Queues for async message processing.
Configuration
Section titled “Configuration”# wrangler.toml[[queues.producers]]binding = "MY_QUEUE"queue = "my-queue"// Send messageawait context.queues.MY_QUEUE.send({ type: 'email', to: 'user@example.com',});
// Send batchawait context.queues.MY_QUEUE.sendBatch([ { body: { type: 'email', to: 'user1@example.com' } }, { body: { type: 'email', to: 'user2@example.com' } },]);
// With delayawait context.queues.MY_QUEUE.send( { type: 'reminder' }, { delaySeconds: 300 });interface Queue<T = unknown> { send(message: T, options?: QueueSendOptions): Promise<void>; sendBatch(messages: MessageBatch<T>[], options?: QueueSendBatchOptions): Promise<void>;}Durable Objects
Section titled “Durable Objects”Durable Objects for stateful edge computing.
Configuration
Section titled “Configuration”# wrangler.toml[durable_objects]bindings = [ { name = "COUNTER", class_name = "Counter" }]// Get stub by nameconst id = context.env.COUNTER.idFromName('my-counter');const stub = context.env.COUNTER.get(id);
// Get stub by unique IDconst uniqueId = context.env.COUNTER.newUniqueId();const stub = context.env.COUNTER.get(uniqueId);
// Call the Durable Objectconst response = await stub.fetch('http://counter/increment');const count = await response.text();interface DurableObjectNamespace { idFromName(name: string): DurableObjectId; newUniqueId(): DurableObjectId; get(id: DurableObjectId): DurableObjectStub;}
interface DurableObjectStub { fetch(input: RequestInfo, init?: RequestInit): Promise<Response>;}Environment Variables
Section titled “Environment Variables”Access environment variables and secrets.
Configuration
Section titled “Configuration”# wrangler.toml[vars]ENVIRONMENT = "production"API_URL = "https://api.example.com"Set secrets:
wrangler secret put API_KEY// Access variablesconst environment = context.env.ENVIRONMENT;const apiUrl = context.env.API_URL;const apiKey = context.env.API_KEY; // SecretService Bindings
Section titled “Service Bindings”Call other Workers directly.
Configuration
Section titled “Configuration”# wrangler.toml[[services]]binding = "AUTH_SERVICE"service = "auth-worker"// Call another Workerconst response = await context.env.AUTH_SERVICE.fetch( new Request('https://auth/validate', { method: 'POST', body: JSON.stringify({ token }), }));
const result = await response.json();Vectorize
Section titled “Vectorize”Vector database for AI applications.
Configuration
Section titled “Configuration”# wrangler.toml[[vectorize]]binding = "VECTORIZE"index_name = "my-index"// Insert vectorsawait context.env.VECTORIZE.insert([ { id: 'doc-1', values: [0.1, 0.2, 0.3], metadata: { title: 'Doc 1' } },]);
// Query vectorsconst results = await context.env.VECTORIZE.query(queryVector, { topK: 10, filter: { category: 'tech' },});Cloudflare Workers AI for inference.
Configuration
Section titled “Configuration”# wrangler.toml[ai]binding = "AI"// Text generationconst response = await context.env.AI.run('@cf/meta/llama-2-7b-chat-int8', { prompt: 'Hello!',});
// Embeddingsconst embeddings = await context.env.AI.run('@cf/baai/bge-base-en-v1.5', { text: 'Hello, world!',});
// Image classificationconst result = await context.env.AI.run('@cf/microsoft/resnet-50', { image: imageArrayBuffer,});Next Steps
Section titled “Next Steps”- Database Guide - D1 in depth
- Authentication - Using KV for sessions
- Durable Objects - Stateful applications