Programmatic configuration·续2
<dt><code>previewUrls?: boolean</code></dt> <dd>Whether we use <code><version>-<name>.<subdomain>.workers.dev</code> to serve Preview URLs for your Worker.</dd>
<dt><code>unsafe?: { / * Arbitrary key/value pairs that will be included in the uploaded metadata. Values specified * here will always be applied to metadata last, so can add new or override existing fields. */ metadata?: Record<string, unknown>; / * Used for internal capnp uploads for the Workers runtime. */ capnp?: { basePath: string; sourceSchemas: string[]; compiledSchema?: never; } | { basePath?: never; sourceSchemas?: never; compiledSchema: string; }; }</code></dt> <dd>"Unsafe" tables for runtime features that aren't directly supported by this configuration. Values are forwarded verbatim in the Worker's upload metadata.</dd>
<dt><code>env?: Record<string, Binding | false | null | undefined></code></dt> <dd>Bindings exposed on the Worker's <code>env</code> object. Construct entries with <code>bindings.kv(...)</code>, <code>bindings.r2(...)</code>, etc. Entries set to <code>false</code>, <code>null</code>, or <code>undefined</code> are omitted, which allows bindings to be included conditionally.</dd>
<dt><code>exports?: Record<string, Export></code></dt> <dd>Configuration for named exports declared by the Worker. Each entry's key is the exported class name; the value configures the export. - Construct entries with <code>exports.durableObject(...)</code>. - Declares Durable Object classes exported from this Worker. For more information about Durable Objects, see the documentation at <a href="https://developers.cloudflare.com/workers/learning/using-durable-objects">https://developers.cloudflare.com/workers/learning/using-durable-objects</a>. For reference, see <a href="https://developers.cloudflare.com/workers/wrangler/configuration/#durable-objects">https://developers.cloudflare.com/workers/wrangler/configuration/#durable-objects</a>. - Construct entries with <code>exports.workflow(...)</code>. - Declares Workflows defined by this Worker. For more information about Workflows, see the documentation at <a href="https://developers.cloudflare.com/workflows/">https://developers.cloudflare.com/workflows/</a>.</dd>
</dl></details>
</details>
<details>
<summary>name: "api-worker", (name reference)</summary>
<code>name</code>Required<a href="#config-cross-worker-api-workerconfig-name">Link to name</a>
<code>name: string</code>
The name of your Worker.
</details>
<details>
<summary>entrypoint, (entrypoint reference)</summary>
<code>entrypoint</code>Optional<a href="#config-cross-worker-api-workerconfig-entrypoint">Link to entrypoint</a>
<code>entrypoint?: string | WorkerModule</code>
The entrypoint module that will be executed. May be either a path string (e.g. <code>"./src/index.ts"</code>) or a module namespace imported with the <code>cf-worker</code> import attribute.
</details>
<details>
<summary>compatibilityDate: "<COMPATIBILITY_DATE>", (compatibilityDate reference)</summary>
<code>compatibilityDate</code>Required<a href="#config-cross-worker-api-workerconfig-compatibilitydate">Link to compatibilityDate</a>
<code>compatibilityDate: string</code>
A date in the form yyyy-mm-dd, which will be used to determine which version of the Workers runtime is used. More details at <a href="https://developers.cloudflare.com/workers/configuration/compatibility-dates">https://developers.cloudflare.com/workers/configuration/compatibility-dates</a>
</details>
<details>
<summary>exports: { (exports reference)</summary>
<code>exports</code>Optional<a href="#config-cross-worker-api-workerconfig-exports">Link to exports</a>
<code>exports?: Record<string, Export></code>
Configuration for named exports declared by the Worker. Each entry's key is the exported class name; the value configures the export. - Construct entries with <code>exports.durableObject(...)</code>. - Declares Durable Object classes exported from this Worker. For more information about Durable Objects, see the documentation at <a href="https://developers.cloudflare.com/workers/learning/using-durable-objects">https://developers.cloudflare.com/workers/learning/using-durable-objects</a>. For reference, see <a href="https://developers.cloudflare.com/workers/wrangler/configuration/#durable-objects">https://developers.cloudflare.com/workers/wrangler/configuration/#durable-objects</a>. - Construct entries with <code>exports.workflow(...)</code>. - Declares Workflows defined by this Worker. For more information about Workflows, see the documentation at <a href="https://developers.cloudflare.com/workflows/">https://developers.cloudflare.com/workflows/</a>.
</details>
<details>
<summary>Admin: exports.worker(), (worker reference)</summary>
<code>worker</code>Builder<a href="#config-cross-worker-api-exports-worker-default">Link to worker</a>
<code>worker(options?: WorkerEntrypointExportOptions): WorkerEntrypointExport;</code>
Declares a WorkerEntrypoint export defined by this Worker.
<details>
<summary>Options (1)</summary>
<dl>
<dt><code>cache?: { /* Whether cache is enabled for this entrypoint. / enabled: boolean; }</code></dt> <dd></dd>
</dl></details>
</details>
<details>
<summary>Counter: exports.durableObject({ storage: "sqlite" }), (durableObject, storage reference)</summary>
<code>durableObject</code>Builder<a href="#config-cross-worker-api-exports-durableobject-created">Link to durableObject</a>
<code>durableObject<TContainer extends ContainerDefinition | undefined = undefined>(options: DurableObjectCreatedExportOptions<TContainer>): DurableObjectCreatedExport<TContainer>;</code>
Declares a Durable Object class defined by this Worker. For more information about Durable Objects, see the documentation at <a href="https://developers.cloudflare.com/workers/learning/using-durable-objects">https://developers.cloudflare.com/workers/learning/using-durable-objects</a> For reference, see <a href="https://developers.cloudflare.com/workers/wrangler/configuration/#durable-objects">https://developers.cloudflare.com/workers/wrangler/configuration/#durable-objects</a>
<details>
<summary>Options (4)</summary>
<dl>
<dt><code>state?: "created"</code></dt> <dd></dd>
<dt><code>storage: "sqlite"</code></dt> <dd>Selects the SQLite-backed storage engine (recommended for new classes).</dd>
<dt><code>container?: TContainer</code></dt> <dd>Attach a Container application to this Durable Object by config reference.</dd>
<dt><code>storage: "legacy-kv"</code></dt> <dd>Selects the legacy key-value storage engine.</dd></dl></details>
<code>storage</code>Required<a href="#config-cross-worker-api-exports-durableobject-created">Link to storage</a>
<code>storage: "sqlite" | "legacy-kv"</code>
Selects the SQLite-backed storage engine (recommended for new classes). Selects the legacy key-value storage engine.
</details>
},
});
<details>
<summary>export default defineConfig({ worker: apiWorker }); (defineConfig, worker reference)</summary>
<code>defineConfig</code>Function<a href="#config-cross-worker-api-defineconfig">Link to defineConfig</a>
<code>defineConfig<T extends ConfigInput<CloudflareConfig>>(config: T): T;</code>
Defines the default export of <code>cloudflare.config.ts</code>. Pass a configuration object, a promise that resolves to one, or a function that receives the config context (<code>isPreview</code> and <code>mode</code>) and returns either.
<details>
<summary>Options (4)</summary>
<dl>
<dt><code>accountId?: string</code></dt> <dd>This is the ID of the account associated with your zone. It can also be specified through the <code>CLOUDFLARE_ACCOUNT_ID</code> environment variable.</dd>
<dt><code>complianceRegion?: "public" | "fedramp-high"</code></dt> <dd>The compliance boundary in which commands should operate. When omitted, this can be supplied through <code>CLOUDFLARE_COMPLIANCE_REGION</code>.</dd>
<dt><code>worker?: ConfigInput<WorkerConfig></code></dt> <dd>The Worker defined by this configuration.</dd>
<dt><code>containers?: ConfigInput<ContainerConfig>[]</code></dt> <dd>Container applications defined by this configuration.</dd></dl></details>
<code>worker</code>Optional<a href="#config-cross-worker-api-defineconfig">Link to worker</a>
<code>worker?: ConfigInput<WorkerConfig></code>
The Worker defined by this configuration.
</details>
A second Worker can import that definition:
Select a highlighted line to show its type and description below it.
web/cloudflare.config.ts
Expand allCopy
import { bindings, defineConfig } from "cf/config";
import { apiWorker } from "../api/cloudflare.config.ts";
import * as entrypoint from "./src/index.ts" with { type: "cf-worker" };
<details>
<summary>export default defineConfig({ (defineConfig reference)</summary>
<code>defineConfig</code>Function<a href="#config-cross-worker-web-defineconfig">Link to defineConfig</a>
<code>defineConfig<T extends ConfigInput<CloudflareConfig>>(config: T): T;</code>
Defines the default export of <code>cloudflare.config.ts</code>. Pass a configuration object, a promise that resolves to one, or a function that receives the config context (<code>isPreview</code> and <code>mode</code>) and returns either.
<details>
<summary>Options (4)</summary>
<dl>
<dt><code>accountId?: string</code></dt> <dd>This is the ID of the account associated with your zone. It can also be specified through the <code>CLOUDFLARE_ACCOUNT_ID</code> environment variable.</dd>
<dt><code>complianceRegion?: "public" | "fedramp-high"</code></dt> <dd>The compliance boundary in which commands should operate. When omitted, this can be supplied through <code>CLOUDFLARE_COMPLIANCE_REGION</code>.</dd>
<dt><code>worker?: ConfigInput<WorkerConfig></code></dt> <dd>The Worker defined by this configuration.</dd>
<dt><code>containers?: ConfigInput<ContainerConfig>[]</code></dt> <dd>Container applications defined by this configuration.</dd></dl></details>
</details>
<details>
<summary>worker: { (worker reference)</summary>
<code>worker</code>Optional<a href="#config-cross-worker-web-cloudflareconfig-worker">Link to worker</a>
<code>worker?: ConfigInput<WorkerConfig></code>
The Worker defined by this configuration.
</details>
<details>
<summary>name: "web-worker", (name reference)</summary>
<code>name</code>Required<a href="#config-cross-worker-web-workerconfig-name">Link to name</a>
<code>name: string</code>
The name of your Worker.
</details>
<details>
<summary>entrypoint, (entrypoint reference)</summary>
<code>entrypoint</code>Optional<a href="#config-cross-worker-web-workerconfig-entrypoint">Link to entrypoint</a>
<code>entrypoint?: string | WorkerModule</code>
The entrypoint module that will be executed. May be either a path string (e.g. <code>"./src/index.ts"</code>) or a module namespace imported with the <code>cf-worker</code> import attribute.
</details>
<details>
<summary>compatibilityDate: "<COMPATIBILITY_DATE>", (compatibilityDate reference)</summary>
<code>compatibilityDate</code>Required<a href="#config-cross-worker-web-workerconfig-compatibilitydate">Link to compatibilityDate</a>
<code>compatibilityDate: string</code>
A date in the form yyyy-mm-dd, which will be used to determine which version of the Workers runtime is used. More details at <a href="https://developers.cloudflare.com/workers/configuration/compatibility-dates">https://developers.cloudflare.com/workers/configuration/compatibility-dates</a>
</details>
<details>
<summary>env: { (env reference)</summary>
<code>env</code>Optional<a href="#config-cross-worker-web-workerconfig-env">Link to env</a>
<code>env?: Record<string, Binding | false | null | undefined></code>
Bindings exposed on the Worker's <code>env</code> object. Construct entries with <code>bindings.kv(...)</code>, <code>bindings.r2(...)</code>, etc. Entries set to <code>false</code>, <code>null</code>, or <code>undefined</code> are omitted, which allows bindings to be included conditionally.
</details>
<details>
<summary>API: bindings.worker({ (worker reference)</summary>
<code>worker</code>Builder<a href="#config-cross-worker-web-bindings-worker-default">Link to worker</a>
<code>worker<TWorker$1 extends WorkerReference, TExportName$1 extends WorkerEntrypointExportName<TWorker$1> | undefined = undefined>(options: WorkerBindingOptions<TWorker$1, TExportName$1>): WorkerBinding<TWorker$1, NoInfer<TExportName$1>>;</code>
Service binding (Worker-to-Worker). <code>worker</code> is the name or config of the bound Worker; <code>exportName</code> selects a named <code>WorkerEntrypoint</code> export (defaults to the default export). For reference, see <a href="https://developers.cloudflare.com/workers/wrangler/configuration/#service-bindings">https://developers.cloudflare.com/workers/wrangler/configuration/#service-bindings</a>
<details>
<summary>Options (4)</summary>
<dl>
<dt><code>worker: TWorker$1</code></dt> <dd>The name or config of the bound Worker.</dd>
<dt><code>exportName?: TExportName$1</code></dt> <dd>The named export to bind to (defaults to the default export).</dd>
<dt><code>props?: Record<string, unknown></code></dt> <dd>Optional properties that will be made available to the service via <code>ctx.props</code>.</dd>
<dt><code>dev?: BindingDevOptions</code></dt> <dd>Options that only apply during local development.</dd></dl></details>
</details>
<details>
<summary>worker: apiWorker, (worker reference)</summary>
<code>worker</code>Required<a href="#config-cross-worker-web-bindings-worker-default-worker">Link to worker</a>
<code>worker: TWorker$1</code>
The name or config of the bound Worker.
</details>
<details>
<summary>exportName: "Admin", (exportName reference)</summary>
<code>exportName</code>Optional<a href="#config-cross-worker-web-bindings-worker-default-exportname">Link to exportName</a>
<code>exportName?: TExportName$1</code>
The named export to bind to (defaults to the default export).
</details>
}),
<details>
<summary>COUNTERS: bindings.durableObject({ (durableObject reference)</summary>
<code>durableObject</code>Builder<a href="#config-cross-worker-web-bindings-durableobject-all">Link to durableObject</a>
<code>durableObject<TWorker$1 extends WorkerReference, TExportName$1 extends DurableObjectExportName<TWorker$1>>(options: DurableObjectBindingOptions<TWorker$1, TExportName$1>): DurableObjectBinding<TWorker$1, TExportName$1>;</code>
Binding to a Durable Object class. <code>worker</code> is the name or config of the Worker that defines the class; <code>exportName</code> is the exported class name. For reference, see <a href="https://developers.cloudflare.com/workers/wrangler/configuration/#durable-objects">https://developers.cloudflare.com/workers/wrangler/configuration/#durable-objects</a>
<details>
<summary>Options (2)</summary>
<dl>
<dt><code>worker: TWorker$1</code></dt> <dd>The name or config of the Worker that defines the Durable Object class.</dd>
<dt><code>exportName: TExportName$1</code></dt> <dd>The exported class name of the Durable Object.</dd></dl></details>
</details>
<details>
<summary>worker: apiWorker, (worker reference)</summary>
<code>worker</code>Required<a href="#config-cross-worker-web-bindings-durableobject-all-worker">Link to worker</a>
<code>worker: TWorker$1</code>
The name or config of the Worker that defines the Durable Object class.
</details>
<details>
<summary>exportName: "Counter", (exportName reference)</summary>
<code>exportName</code>Required<a href="#config-cross-worker-web-bindings-durableobject-all-exportname">Link to exportName</a>
<code>exportName: TExportName$1</code>
The exported class name of the Durable Object.
</details>
}),
},
},
});
Import the other configuration file with its .ts extension. Node.js loads cloudflare.config.ts with its own module resolution, which does not add extensions. An import without the extension fails with a Cannot find module error (ERR_MODULE_NOT_FOUND). Because API commands also load the nearest configuration file, the error breaks ordinary API commands in that directory too.
TypeScript rejects unknown export names. It also infers the API Remote Procedure Call (RPC) methods and the Durable Object stub type from the imported entrypoint.
Use a Worker name when the target definition cannot be imported. TypeScript can validate the binding shape, but it cannot validate the remote export name.
Declare triggers
Each triggers method returns a typed event declaration. Add these values to the worker.triggers array.
Select a highlighted line to show its type and description below it.
cloudflare.config.ts
Expand allCopy
import { defineConfig, triggers } from "cf/config";
import * as entrypoint from "./src/index.ts" with { type: "cf-worker" };
<details>
<summary>export default defineConfig({ (defineConfig reference)</summary>
<code>defineConfig</code>Function<a href="#config-triggers-defineconfig">Link to defineConfig</a>
<code>defineConfig<T extends ConfigInput<CloudflareConfig>>(config: T): T;</code>
Defines the default export of <code>cloudflare.config.ts</code>. Pass a configuration object, a promise that resolves to one, or a function that receives the config context (<code>isPreview</code> and <code>mode</code>) and returns either.
