Bindings (3)
Last updated Jun 25, 2026 A binding enables your Pages Functions to interact with resources on the Cloudflare developer platform. Use bindings to integrate your Pages Functions with Cloudflare resources like KV, Durable Objects, R2, and D1. You can set bindings for both production and preview environments.
This guide will instruct you on configuring a binding for your Pages Function. You must already have a Cloudflare Developer Platform resource set up to continue.
Note
Pages Functions only support a subset of all bindings, which are listed on this page.
KV namespaces
Workers KV is Cloudflare's key-value storage solution.
To bind your KV namespace to your Pages Function, you can configure a KV namespace binding in the Wrangler configuration file or the Cloudflare dashboard.
To configure a KV namespace binding via the Cloudflare dashboard:
- In the Cloudflare dashboard, go to the Workers & Pages page. Go to Workers & Pages ↗
- Select your Pages project.
- Go to Settings > Bindings > Add > KV namespace.
- Give your binding a name under Variable name.
- Under KV namespace, select your desired namespace.
- Redeploy your project for the binding to take effect.
Below is an example of how to use KV in your Function. In the following example, your KV namespace binding is called TODO_LIST and you can access the binding in your Function code on context.env:
export async function onRequest(context) {
const task = await context.env.TODO_LIST.get("Task:123");
return new Response(task);
}
interface Env {
TODO_LIST: KVNamespace;
}
export const onRequest: PagesFunction<Env> = async (context) => {
const task = await context.env.TODO_LIST.get("Task:123");
return new Response(task);
};
Interact with your KV namespaces locally
You can interact with your KV namespace bindings locally in one of two ways:
- Configure your Pages project's Wrangler file and run
npx wrangler pages dev. - Pass arguments to
wrangler pages devdirectly.
To interact with your KV namespace binding locally by passing arguments to the Wrangler CLI, add -k <BINDING_NAME> or --kv=<BINDING_NAME> to the wrangler pages dev command. For example, if your KV namespace is bound your Function via the TODO_LIST binding, access the KV namespace in local development by running:
npx wrangler pages dev <OUTPUT_DIR> --kv=TODO_LIST
Note
If a binding is specified in a Wrangler configuration file and via a command-line argument, the command-line argument takes precedence.
Durable Objects
Durable Objects (DO) are Cloudflare's strongly consistent data store that power capabilities such as connecting WebSockets and handling state.
You must create a Durable Object Worker and bind it to your Pages project using the Cloudflare dashboard or your Pages project's Wrangler configuration file. You cannot create and deploy a Durable Object within a Pages project.
To bind your Durable Object to your Pages Function, you can configure a Durable Object binding in the Wrangler configuration file or the Cloudflare dashboard.
To configure a Durable Object binding via the Cloudflare dashboard:
- In the Cloudflare dashboard, go to the Workers & Pages page. Go to Workers & Pages ↗
- Select your Pages project.
- Go to Settings > Bindings > Add > Durable Object.
- Give your binding a name under Variable name.
- Under Durable Object namespace, select your desired namespace.
- Redeploy your project for the binding to take effect.
Below is an example of how to use Durable Objects in your Function. In the following example, your DO binding is called DURABLE_OBJECT and you can access the binding in your Function code on context.env:
export async function onRequestGet(context) {
const id = context.env.DURABLE_OBJECT.newUniqueId();
const stub = context.env.DURABLE_OBJECT.get(id);
// Pass the request down to the durable object
return stub.fetch(context.request);
}
interface Env {
DURABLE_OBJECT: DurableObjectNamespace;
}
export const onRequestGet: PagesFunction<Env> = async (context) => {
const id = context.env.DURABLE_OBJECT.newUniqueId();
const stub = context.env.DURABLE_OBJECT.get(id);
// Pass the request down to the durable object
return stub.fetch(context.request);
};
Interact with your Durable Object namespaces locally
You can interact with your Durable Object bindings locally in one of two ways:
- Configure your Pages project's Wrangler file and run
npx wrangler pages dev. - Pass arguments to
wrangler pages devdirectly.
While developing locally, to interact with a Durable Object namespace, run wrangler dev in the directory of the Worker exporting the Durable Object. In another terminal, run wrangler pages dev in the directory of your Pages project.
To interact with your Durable Object namespace locally via the Wrangler CLI, append --do <BINDING_NAME>=<CLASS_NAME>@<SCRIPT_NAME> to wrangler pages dev. CLASS_NAME indicates the Durable Object class name and SCRIPT_NAME the name of your Worker.
For example, if your Worker is called do-worker and it declares a Durable Object class called DurableObjectExample, access this Durable Object by running npx wrangler dev in the do-worker directory. At the same time, run npx wrangler pages dev <OUTPUT_DIR> --do MY_DO=DurableObjectExample@do-worker in your Pages' project directory. Interact with the MY_DO binding in your Function code by using context.env (for example, context.env.MY_DO).
Note
If a binding is specified in a Wrangler configuration file and via a command-line argument, the command-line argument takes precedence.
R2 buckets
R2 is Cloudflare's blob storage solution that allows developers to store large amounts of unstructured data without the egress fees.
To bind your R2 bucket to your Pages Function, you can configure a R2 bucket binding in the Wrangler configuration file or the Cloudflare dashboard.
To configure a R2 bucket binding via the Cloudflare dashboard:
- In the Cloudflare dashboard, go to the Workers & Pages page. Go to Workers & Pages ↗
- Select your Pages project.
- Go to Settings > Bindings > Add > R2 bucket.
- Give your binding a name under Variable name.
- Under R2 bucket, select your desired R2 bucket.
- Redeploy your project for the binding to take effect.
Below is an example of how to use R2 buckets in your Function. In the following example, your R2 bucket binding is called BUCKET and you can access the binding in your Function code on context.env:
export async function onRequest(context) {
const obj = await context.env.BUCKET.get("some-key");
if (obj === null) {
return new Response("Not found", { status: 404 });
}
return new Response(obj.body);
}
interface Env {
BUCKET: R2Bucket;
}
export const onRequest: PagesFunction<Env> = async (context) => {
const obj = await context.env.BUCKET.get("some-key");
if (obj === null) {
return new Response("Not found", { status: 404 });
}
return new Response(obj.body);
};
Interact with your R2 buckets locally
You can interact with your R2 bucket bindings locally in one of two ways:
- Configure your Pages project's Wrangler file and run
npx wrangler pages dev. - Pass arguments to
wrangler pages devdirectly.
Note
By default, Wrangler automatically persists data to local storage. For more information, refer to Local development.
To interact with an R2 bucket locally via the Wrangler CLI, add --r2=<BINDING_NAME> to the wrangler pages dev command. If your R2 bucket is bound to your Function with the BUCKET binding, access this R2 bucket in local development by running:
npx wrangler pages dev <OUTPUT_DIR> --r2=BUCKET
Interact with this binding by using context.env (for example, context.env.BUCKET.)
Note
If a binding is specified in a Wrangler configuration file and via a command-line argument, the command-line argument takes precedence.
D1 databases
D1 is Cloudflare's native serverless database.
To bind your D1 database to your Pages Function, you can configure a D1 database binding in the Wrangler configuration file or the Cloudflare dashboard.
To configure a D1 database binding via the Cloudflare dashboard:
- In the Cloudflare dashboard, go to the Workers & Pages page. Go to Workers & Pages ↗
- Select your Pages project.
- Go to Settings > Bindings > Add> D1 database bindings.
- Give your binding a name under Variable name.
- Under D1 database, select your desired D1 database.
- Redeploy your project for the binding to take effect.
Below is an example of how to use D1 in your Function. In the following example, your D1 database binding is NORTHWIND_DB and you can access the binding in your Function code on context.env:
export async function onRequest(context) {
// Create a prepared statement with our query
const ps = context.env.NORTHWIND_DB.prepare("SELECT * from users");
const data = await ps.first();
return Response.json(data);
}
interface Env {
NORTHWIND_DB: D1Database;
}
export const onRequest: PagesFunction<Env> = async (context) => {
// Create a prepared statement with our query
const ps = context.env.NORTHWIND_DB.prepare("SELECT * from users");
const data = await ps.first();
return Response.json(data);
};
Interact with your D1 databases locally
You can interact with your D1 database bindings locally in one of two ways:
- Configure your Pages project's Wrangler file and run
npx wrangler pages dev. - Pass arguments to
wrangler pages devdirectly.
To interact with a D1 database via the Wrangler CLI while developing locally, add --d1 <BINDING_NAME>=<DATABASE_ID> to the wrangler pages dev command.
If your D1 database is bound to your Pages Function via the NORTHWIND_DB binding and the database_id in your Wrangler file is xxxx-xxxx-xxxx-xxxx-xxxx, access this database in local development by running:
npx wrangler pages dev <OUTPUT_DIR> --d1 NORTHWIND_DB=xxxx-xxxx-xxxx-xxxx-xxxx
Interact with this binding by using context.env (for example, context.env.NORTHWIND_DB.)
Note
By default, Wrangler automatically persists data to local storage. For more information, refer to Local development.
Refer to the D1 Workers Binding API documentation for the API methods available on your D1 binding.
Note
If a binding is specified in a Wrangler configuration file and via a command-line argument, the command-line argument takes precedence.
Vectorize indexes
Vectorize is Cloudflare’s native vector database.
To bind your Vectorize index to your Pages Function, you can configure a Vectorize index binding in the Wrangler configuration file or the Cloudflare dashboard.
To configure a Vectorize index binding via the Cloudflare dashboard:
- In the Cloudflare dashboard, go to the Workers & Pages page. Go to Workers & Pages ↗
- Choose whether you would like to set up the binding in your Production or Preview environment.
- Select your Pages project > Settings.
- Go to Bindings > Add > Vectorize index.
- Give your binding a name under Variable name.
- Under Vectorize index, select your desired Vectorize index.
- Redeploy your project for the binding to take effect.
