Skip to content

Use KV, D1, R2, Queues and service bindings from Python

A binding in your Wrangler config becomes an attribute of self.env. crosswind-workers wraps each one in a class with async snake_case methods that take and return Python values. Every example on this page uses this config:

{
  "name": "bindings-demo",
  "main": "src/entry.py",
  "compatibility_date": "2026-09-08",
  "compatibility_flags": ["python_workers"],
  "kv_namespaces": [{ "binding": "CACHE", "id": "<your KV namespace id>" }],
  "d1_databases": [
    { "binding": "DB", "database_name": "notes", "database_id": "<your D1 database id>" }
  ],
  "r2_buckets": [{ "binding": "FILES", "bucket_name": "files" }],
  "queues": { "producers": [{ "binding": "JOBS", "queue": "jobs" }] }
}
name = "bindings-demo"
main = "src/entry.py"
compatibility_date = "2026-09-08"
compatibility_flags = ["python_workers"]

[[kv_namespaces]]
binding = "CACHE"
id = "<your KV namespace id>"

[[d1_databases]]
binding = "DB"
database_name = "notes"
database_id = "<your D1 database id>"

[[r2_buckets]]
binding = "FILES"
bucket_name = "files"

[[queues.producers]]
binding = "JOBS"
queue = "jobs"
from wynd.config import Config, Worker, bindings

config = Config(
    worker=Worker(
        name="bindings-demo",
        entrypoint="src/entry.py",
        compatibility_date="2026-09-08",
        env={
            "CACHE": bindings.KV(id="<your KV namespace id>"),
            "DB": bindings.D1(name="notes", id="<your D1 database id>"),
            "FILES": bindings.R2(name="files"),
            "JOBS": bindings.Queue(name="jobs"),
        },
    ),
)

wynd dev simulates every binding locally, so any placeholder id works until you deploy. Create the real resources with uvx wynd kv namespace create, uvx wynd d1 create, uvx wynd r2 bucket create and uvx wynd queues create. Wynd passes these commands to Wrangler with --config set to its staged copy of your config. If Wrangler writes the new binding into that copy, Wynd prints a warning that names what to copy into your own config.

crosswind-workers picks each binding's class from its JavaScript type at runtime:

Config section Class
kv_namespaces KVNamespace
d1_databases D1Database
r2_buckets R2Bucket
queues.producers Queue
durable_objects.bindings DurableObjectNamespace
services, assets Fetcher
workflows Workflow
ai, vectorize, analytics_engine_datasets, images, ratelimits AI, Vectorize, AnalyticsEngineDataset, ImagesBinding, RateLimit. See Other binding classes.
vars and secrets str, or a Python value for a JSON var
Anything else, such as browser Binding

Run uvx wynd types after you change the config, so Env in src/env.py lists the new bindings. wynd dev and wynd deploy also regenerate it.

KV

src/entry.py
from env import Env
from workers import Request, Response, WorkerEntrypoint


class Default(WorkerEntrypoint[Env]):
    async def fetch(self, request: Request) -> Response:
        await self.env.CACHE.put("user:1", '{"name": "Ada"}', expiration_ttl=3600)
        text = await self.env.CACHE.get("user:1")  # str, or None if missing
        user = await self.env.CACHE.get("user:1", "json")  # parsed into a dict
        listing = await self.env.CACHE.list(prefix="user:")
        return Response.json(
            {"text": text, "name": user["name"], "keys": len(listing["keys"])}
        )
{"text": "{\"name\": \"Ada\"}", "name": "Ada", "keys": 1}

The second argument of get picks the type. "text" is the default, "json" parses the value, "arrayBuffer" returns a memoryview and "stream" returns a JavaScript ReadableStream. Every type returns None for a missing key.

KV rejects an expiration_ttl below 60 seconds. expiration_ttl=30 raises JsException: Error: KV PUT failed: 400 Invalid expiration_ttl of 30. Expiration TTL must be at least 60.

D1

src/entry.py
from env import Env
from workers import Request, Response, WorkerEntrypoint


class Default(WorkerEntrypoint[Env]):
    async def fetch(self, request: Request) -> Response:
        db = self.env.DB
        await db.exec("CREATE TABLE IF NOT EXISTS notes (id INTEGER PRIMARY KEY, body TEXT)")
        await db.prepare("INSERT INTO notes (body) VALUES (?)").bind("first").run()

        result = await db.prepare("SELECT id, body FROM notes").all()
        first = await db.prepare("SELECT body FROM notes WHERE id = ?").bind(1).first("body")
        return Response.json({"rows": result["results"], "first": first})

On the first request:

{"rows": [{"id": 1, "body": "first"}], "first": "first"}

bind returns a new statement, so you can prepare a query once and bind it many times. first returns None when the query matches no row. batch runs a list of bound statements in one transaction. A dictionary from D1 also reads by attribute, so result.results works.

R2

src/entry.py
from env import Env
from workers import Request, Response, WorkerEntrypoint


class Default(WorkerEntrypoint[Env]):
    async def fetch(self, request: Request) -> Response:
        await self.env.FILES.put(
            "notes/hello.txt",
            "hello from R2",
            http_metadata={"contentType": "text/plain"},
            custom_metadata={"author": "ada"},
        )

        obj = await self.env.FILES.get("notes/hello.txt")
        if obj is None:
            return Response("not found", status=404)
        return Response(
            await obj.text(),
            headers={"content-type": obj.http_metadata["contentType"]},
        )

curl returns hello from R2 with content-type: text/plain.

put sends http_metadata and custom_metadata as written, so their keys keep R2's camelCase names. get returns an R2ObjectBody, or None when the key doesn't exist. list(prefix=...) returns a dictionary whose objects are R2Objects. When its truncated is True, pass its cursor to the next list call.

Queues

src/entry.py
from env import Env
from workers import Request, Response, WorkerEntrypoint


class Default(WorkerEntrypoint[Env]):
    async def fetch(self, request: Request) -> Response:
        await self.env.JOBS.send({"task": "resize", "id": 42}, delay_seconds=10)
        await self.env.JOBS.send_batch([{"body": {"id": 1}}, {"body": {"id": 2}}])
        return Response("queued", status=202)

curl returns queued with status 202. The message body can be any value that crosses RPC, such as a dict, list, str or bytes. send_batch takes one dictionary per message, with the body under "body". delay_seconds=10 holds the message for 10 seconds before delivery.

Consume messages

Add a consumer to the config, and a queue method to the entrypoint:

"queues": {
  "producers": [{ "binding": "JOBS", "queue": "jobs" }],
  "consumers": [{ "queue": "jobs" }]
}
[[queues.producers]]
binding = "JOBS"
queue = "jobs"

[[queues.consumers]]
queue = "jobs"
from wynd.config import bindings, triggers

env={"JOBS": bindings.Queue(name="jobs")},
triggers=[triggers.Queue("jobs")],
src/entry.py
class Default(WorkerEntrypoint[Env]):
    async def queue(self, batch, env, ctx):
        for message in batch.messages:
            await self.env.CACHE.put("last-task", message.body["task"])
            message.ack()

message.body is the Python value you sent, so message.body["task"] is "resize" for the message above. queue must take three arguments after self. The runtime passes env and ctx as None, so use self.env and self.ctx. A queue(self, batch) fails on every batch with TypeError: Default.queue() takes 2 positional arguments but 4 were given in the wynd dev log, and the runtime delivers the batch again.

Workers AI

Add "ai": { "binding": "AI" } to the config. AI.run takes the model name and a dict of inputs, and returns the model's output as a dict:

out = await self.env.AI.run(
    "@cf/meta/llama-4-scout-17b-16e-instruct",
    {"messages": [{"role": "user", "content": "Hello"}]},
)
out["response"]

With "stream": True in the inputs, run returns a js.ReadableStream of server-sent events. Return it in a Response to stream it to the client:

stream = await self.env.AI.run(model, {"messages": messages, "stream": True})
return Response(stream, headers={"content-type": "text/event-stream"})

Service bindings

A service binding is a Fetcher. Its fetch sends an HTTP request to the bound Worker and returns a crosswind Response. Any other method is an RPC call to the bound entrypoint. This Worker binds to its own Auth entrypoint, so it runs without a second project:

"services": [{ "binding": "AUTH", "service": "bindings-demo", "entrypoint": "Auth" }]
[[services]]
binding = "AUTH"
service = "bindings-demo"
entrypoint = "Auth"
from wynd.config import bindings

env={"AUTH": bindings.Service("bindings-demo", export="Auth")},
src/entry.py
from env import Env
from workers import Request, Response, WorkerEntrypoint


class Auth(WorkerEntrypoint):
    async def fetch(self, request: Request) -> Response:
        return Response("ok")

    async def lookup_user(self, name: str) -> dict:
        return {"name": name, "admin": False}


class Default(WorkerEntrypoint[Env]):
    async def fetch(self, request: Request) -> Response:
        health = await self.env.AUTH.fetch("https://auth/health")
        user = await self.env.AUTH.lookup_user("ada")
        return Response.json({"health": await health.text(), "user": user})
{"health": "ok", "user": {"name": "ada", "admin": false}}

The hostname in the URL doesn't matter. The request always goes to the bound Worker. As with Durable Objects, pass RPC arguments by position.

Keyword options and JavaScript names

Typed methods take snake_case keywords and turn them into the camelCase options object JavaScript expects. A keyword the method doesn't know goes into that object unchanged, so the stock SDK's spelling still works:

await self.env.CACHE.put("k", "v", expiration_ttl=60)  # typed keyword
await self.env.CACHE.put("k", "v", expirationTtl=60)   # same call, stock spelling

A method crosswind doesn't define passes through to the JavaScript object under its JavaScript name, with arguments and results converted as they are for RPC. Nothing translates snake_case to camelCase for you:

meta = await self.env.CACHE.getWithMetadata("k")  # passthrough, works
meta = await self.env.CACHE.get_with_metadata("k")  # typed method, also works
await self.env.CACHE.list_keys()  # neither defines it: AttributeError: list_keys

Bindings with no class, such as ai or browser, are a Binding. Call them with their JavaScript names:

# This line requires an "ai" binding named AI in wrangler.jsonc.
result = await self.env.AI.run("@cf/meta/llama-3.1-8b-instruct", {"prompt": "Hi"})

To reach the JavaScript object itself, read js_object. See Call JavaScript APIs.

Variables and secrets

A text var or a secret is a str. A JSON var arrives as a Python value:

"vars": { "GREETING": "hello", "LIMITS": { "uploads": 10 } }
[vars]
GREETING = "hello"
LIMITS = { uploads = 10 }
from wynd.config import bindings

env={
    "GREETING": bindings.Text("hello"),
    "LIMITS": bindings.Json({"uploads": 10}),
},

self.env.LIMITS["uploads"] is the integer 10. Wynd types secrets from the names in .dev.vars, so add each secret there for local development.