Skip to main content
Agent Builder v2 is in open beta. Features and limitations described here are subject to change.
This guide shows you how to add a database, object storage, queues, and cron jobs to an Agent Builder v2 Worker. After you finish, your app reads those resources from env, and you can inspect database tables and stored objects in Control Center. These features apply to Workers (index.js, no root Dockerfile). A container does not receive env.DATABASE or env.STORAGE—use Vault and an external service instead.

Add a database

When you deploy a Worker, WRITER attaches one D1 database for that agent as env.DATABASE. Every environment on the agent uses that same database.
  1. Deploy a Worker with index.js at the archive root and no root Dockerfile.
  2. Query from fetch with prepared statements and .bind() for user input:
  1. Store JSON as TEXT with JSON.stringify / JSON.parse when you need arrays or objects.
For more information, see Cloudflare’s D1 Worker API.

Apply database migrations

Put schema files in a top-level migrations/ directory so WRITER applies them at deploy time.
  1. Add *.sql files such as 0001_init.sql and 0002_add_status.sql (lexicographic order).
  2. Keep each file safe to run again (IF NOT EXISTS, or an ALTER that checks whether the change is already there).
WRITER applies migrations before the Worker serves traffic. A file that fails halfway is not recorded as applied, and the deploy fails. Already-applied files are skipped on later deploys.
SQL that lives only under a nested directory, such as backend/migrations/, fails the deploy. Move those files to a top-level migrations/ directory. Nested SQL next to a real root migrations/ directory is ignored.

Add object storage

When you deploy a Worker, WRITER attaches one R2 bucket for that agent as env.STORAGE. Every environment on the agent uses that same bucket. Put and get objects from fetch. Store the object key in the database when you need a file reference, and serve files through your Worker instead of persisting a public URL.
For more information, see Cloudflare’s R2 Workers API.

Add a queue

Declare queue producers (and consumers, if you need them) in a root Wrangler config, and omit a root Dockerfile. A root Wrangler config that includes queues uses the Wrangler deploy path, which binds only the resources you declare—also declare DATABASE and STORAGE if your Worker still needs them.
Send from fetch, and handle batches in queue when you declare a consumer. This example inserts into the tickets table from Add a database—apply that migration first, or the insert fails with no such table:
For more information, see Cloudflare Queues.

Add a cron job

Add cron expressions under triggers.crons in a root Wrangler config. WRITER applies those schedules on deploy. Handle them in scheduled. A config that only declares triggers.crons stays on the default Worker path, so env.DATABASE and env.STORAGE are still attached automatically—you don’t need to declare them here:
For more information, see Cloudflare Cron Triggers.

Package and deploy the Worker

Build TypeScript or npm packages locally into index.js. The platform does not install packages or compile Worker source. Include index.js and, if you use a schema, top-level migrations/. Omit a root Dockerfile.
Upload through Control Center or the WRITER Applications MCP server, as in Upload and deploy. You should see the revision move to live.

Browse database and object storage in the Data tab

After a Worker environment deploys successfully, open that environment in Control Center and select the Data tab. You can inspect and edit the same database tables and object-storage folders your Worker uses.
  1. Open the agent in Control Center and select the environment (for example Development).
  2. Select the Data tab.
  3. Choose a table or a folder in the resource tree to open it in the viewer.
From the Data tab you can create and rename tables, edit rows, export table data, upload objects, create top-level folders (prefixes), rename or delete folders, download folder contents, and search. Queues and cron jobs do not appear in this tab. You should see Databases and Object storage after the first successful Worker deploy. Rows and files appear after your app or the UI creates them. Uploads through the UI are capped at 25 MB per file. Folders with more than 90 objects cannot be deleted from the Data tab—delete those objects from your Worker or reduce the folder size first.

Match binding names for local development

Keep a root Wrangler config so local wrangler dev uses the same names WRITER injects. If the file declares D1 or R2, the binding names must be DATABASE and STORAGE or the deploy fails. Resource ids in the file are for local use only—WRITER creates the real database and bucket.
On your own Cloudflare account you would create and bind D1 or R2 in Wrangler yourself; see Cloudflare Bindings. On Agent Builder v2, WRITER attaches the database and object storage for you.

Troubleshoot bindings

Use this table when a Worker deploy fails on packaging or binding names, or when the Data tab has nothing to show after a successful deploy.

Next steps