Skip to content

Bindings Reference

Cloudwerk provides seamless access to all Cloudflare bindings through the context object.

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
}

Cloudflare D1 is a serverless SQLite database.

# 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 queries
const result = await context.env.DB
.prepare('SELECT * FROM users WHERE id = ?')
.bind(userId)
.first();
// Batch queries
const 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>;
}

Cloudflare Workers KV provides key-value storage.

# wrangler.toml
[[kv_namespaces]]
binding = "KV"
id = "your-kv-id"
// Get value
const value = await context.kv.get('key');
const jsonValue = await context.kv.get('key', 'json');
const streamValue = await context.kv.get('key', 'stream');
// Set value
await context.kv.put('key', 'value');
await context.kv.put('key', JSON.stringify(data));
// With expiration
await context.kv.put('key', 'value', {
expirationTtl: 3600, // 1 hour in seconds
});
// With metadata
await context.kv.put('key', 'value', {
metadata: { createdAt: Date.now() },
});
// Delete
await context.kv.delete('key');
// List keys
const 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>;
}

Cloudflare R2 provides S3-compatible object storage.

# wrangler.toml
[[r2_buckets]]
binding = "R2"
bucket_name = "my-bucket"
// Get object
const 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 object
await context.r2.put('path/to/file.txt', 'Hello, World!');
await context.r2.put('path/to/file.txt', fileStream, {
httpMetadata: {
contentType: 'text/plain',
},
});
// Delete object
await context.r2.delete('path/to/file.txt');
// List objects
const 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>;
}

Cloudflare Queues for async message processing.

# wrangler.toml
[[queues.producers]]
binding = "MY_QUEUE"
queue = "my-queue"
// Send message
await context.queues.MY_QUEUE.send({
type: 'email',
});
// Send batch
await context.queues.MY_QUEUE.sendBatch([
{ body: { type: 'email', to: '[email protected]' } },
{ body: { type: 'email', to: '[email protected]' } },
]);
// With delay
await 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 for stateful edge computing.

# wrangler.toml
[durable_objects]
bindings = [
{ name = "COUNTER", class_name = "Counter" }
]
// Get stub by name
const id = context.env.COUNTER.idFromName('my-counter');
const stub = context.env.COUNTER.get(id);
// Get stub by unique ID
const uniqueId = context.env.COUNTER.newUniqueId();
const stub = context.env.COUNTER.get(uniqueId);
// Call the Durable Object
const 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>;
}

Access environment variables and secrets.

# wrangler.toml
[vars]
ENVIRONMENT = "production"
API_URL = "https://api.example.com"

Set secrets:

Terminal window
wrangler secret put API_KEY
// Access variables
const environment = context.env.ENVIRONMENT;
const apiUrl = context.env.API_URL;
const apiKey = context.env.API_KEY; // Secret

Call other Workers directly.

# wrangler.toml
[[services]]
binding = "AUTH_SERVICE"
service = "auth-worker"
// Call another Worker
const response = await context.env.AUTH_SERVICE.fetch(
new Request('https://auth/validate', {
method: 'POST',
body: JSON.stringify({ token }),
})
);
const result = await response.json();

Vector database for AI applications.

# wrangler.toml
[[vectorize]]
binding = "VECTORIZE"
index_name = "my-index"
// Insert vectors
await context.env.VECTORIZE.insert([
{ id: 'doc-1', values: [0.1, 0.2, 0.3], metadata: { title: 'Doc 1' } },
]);
// Query vectors
const results = await context.env.VECTORIZE.query(queryVector, {
topK: 10,
filter: { category: 'tech' },
});

Cloudflare Workers AI for inference.

# wrangler.toml
[ai]
binding = "AI"
// Text generation
const response = await context.env.AI.run('@cf/meta/llama-2-7b-chat-int8', {
prompt: 'Hello!',
});
// Embeddings
const embeddings = await context.env.AI.run('@cf/baai/bge-base-en-v1.5', {
text: 'Hello, world!',
});
// Image classification
const result = await context.env.AI.run('@cf/microsoft/resnet-50', {
image: imageArrayBuffer,
});