Bindings
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.
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 wrangler.toml
or the Cloudflare dashboard.
To configure a KV namespace binding via the Cloudflare dashboard:
- Log in to the Cloudflare dashboard ↗ and select your account.
- In Account Home, select Workers & Pages.
- Select your Pages project > Settings.
- Select your Pages environment > 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
:
You can interact with your KV namespace bindings locally in one of two ways:
- Configure your Pages project's
wrangler.toml
file and runnpx wrangler pages dev
. - Pass arguments to
wrangler pages dev
directly.
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:
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.toml
. 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 wrangler.toml
or the Cloudflare dashboard.
To configure a Durable Object binding via the Cloudflare dashboard:
- Log in to the Cloudflare dashboard ↗ and select your account.
- In Account Home, select Workers & Pages.
- Select your Pages project > Settings.
- Select your Pages environment > 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
:
You can interact with your Durable Object bindings locally in one of two ways:
- Configure your Pages project's
wrangler.toml
file and runnpx wrangler pages dev
. - Pass arguments to
wrangler pages dev
directly.
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
).
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 wrangler.toml
or the Cloudflare dashboard.
To configure a R2 bucket binding via the Cloudflare dashboard:
- Log in to the Cloudflare dashboard ↗ and select your account.
- In Account Home, select Workers & Pages.
- Select your Pages project > Settings.
- Select your Pages environment > 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
:
You can interact with your R2 bucket bindings locally in one of two ways:
- Configure your Pages project's
wrangler.toml
file and runnpx wrangler pages dev
. - Pass arguments to
wrangler pages dev
directly.
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:
Interact with this binding by using context.env
(for example, context.env.BUCKET
.)
D1 is Cloudflare’s native serverless database.
To bind your D1 database to your Pages Function, you can configure a D1 database binding in wrangler.toml
or the Cloudflare dashboard.
To configure a D1 database binding via the Cloudflare dashboard:
- Log in to the Cloudflare dashboard ↗ and select your account.
- In Account Home, select Workers & Pages.
- Select your Pages project > Settings.
- Select your Pages environment > 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
:
You can interact with your D1 database bindings locally in one of two ways:
- Configure your Pages project's
wrangler.toml
file and runnpx wrangler pages dev
. - Pass arguments to
wrangler pages dev
directly.
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.toml
file is xxxx-xxxx-xxxx-xxxx-xxxx
, access this database in local development by running:
Interact with this binding by using context.env
(for example, context.env.NORTHWIND_DB
.)
Refer to the D1 Workers Binding API documentation for the API methods available on your D1 binding.
Vectorize is Cloudflare’s native vector database.
To bind your Vectorize index to your Pages Function, you can configure a Vectorize index binding in wrangler.toml
or the Cloudflare dashboard.
To configure a Vectorize index binding via the Cloudflare dashboard:
- Log in to the Cloudflare dashboard ↗ and select your account.
- In Account Home, select Workers & Pages.
- Choose whether you would like to set up the binding in your Production or Preview environment.
- Select your Pages project > Settings.
- Select your Pages environment > 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.
To use Vectorize index in your Pages Function, you can access your Vectorize index binding in your Pages Function code. In the following example, your Vectorize index binding is called VECTORIZE_INDEX
and you can access the binding in your Pages Function code on context.env
.
Workers AI allows you to run machine learning models, powered by serverless GPUs, on Cloudflare’s global network.
To bind Workers AI to your Pages Function, you can configure a Workers AI binding in wrangler.toml
or the Cloudflare dashboard.
When developing locally using Wrangler, you can define an AI binding using the --ai
flag. Start Wrangler in development mode by running wrangler pages dev --ai AI
to expose the context.env.AI
binding.
To configure a Workers AI binding via the Cloudflare dashboard:
- Log in to the Cloudflare dashboard ↗ and select your account.
- In Account Home, select Workers & Pages.
- Select your Pages project > Settings.
- Select your Pages environment > Bindings > Add > Workers AI.
- Give your binding a name under Variable name.
- Redeploy your project for the binding to take effect.
To use Workers AI in your Pages Function, you can access your Workers AI binding in your Pages Function code. In the following example, your Workers AI binding is called AI
and you can access the binding in your Pages Function code on context.env
.
You can interact with your Workers AI bindings locally in one of two ways:
- Configure your Pages project's
wrangler.toml
file and runnpx wrangler pages dev
. - Pass arguments to
wrangler pages dev
directly.
To interact with a Workers AI binding via the Wrangler CLI while developing locally, run:
Service bindings enable you to call a Worker from within your Pages Function.
To bind your Pages Function to a Worker, configure a Service binding in your Pages Function using wrangler.toml
or the Cloudflare dashboard.
To configure a Service binding via the Cloudflare dashboard:
- Log in to the Cloudflare dashboard ↗ and select your account.
- In Account Home, select Workers & Pages.
- Select your Pages project > Settings.
- Select your Pages environment > Bindings > Add > Service binding.
- Give your binding a name under Variable name.
- Under Service, select your desired Worker.
- Redeploy your project for the binding to take effect.
Below is an example of how to use Service bindings in your Function. In the following example, your Service binding is called SERVICE
and you can access the binding in your Function code on context.env
:
You can interact with your Service bindings locally in one of two ways:
- Configure your Pages project's
wrangler.toml
file and runnpx wrangler pages dev
. - Pass arguments to
wrangler pages dev
directly.
To interact with a Service binding while developing locally, run the Worker you want to bind to via wrangler dev
and in parallel, run wrangler pages dev
with --service <BINDING_NAME>=<SCRIPT_NAME>
where SCRIPT_NAME
indicates the name of the Worker. For example, if your Worker is called my-worker
, connect with this Worker by running it via npx wrangler dev
(in the Worker's directory) alongside npx wrangler pages dev <OUTPUT_DIR> --service MY_SERVICE=my-worker
(in the Pages' directory). Interact with this binding by using context.env
(for example, context.env.MY_SERVICE
).
If you set up the Service binding via the Cloudflare dashboard, you will need to append wrangler pages dev
with --service <BINDING_NAME>=<SCRIPT_NAME>
where BINDING_NAME
is the name of the Service binding and SCRIPT_NAME
is the name of the Worker.
For example, to develop locally, if your Worker is called my-worker
, run npx wrangler dev
in the my-worker
directory. In a different terminal, also run npx wrangler pages dev <OUTPUT_DIR> --service MY_SERVICE=my-worker
in your Pages project directory. Interact with this Service binding by using context.env
(for example, context.env.MY_SERVICE
).
Queue Producers enable you to send messages into a queue within your Pages Function.
To bind a queue to your Pages Function, configure a queue producer binding in your Pages Function using wrangler.toml
or the Cloudflare dashboard:
To configure a queue producer binding via the Cloudflare dashboard:
- Log in to the Cloudflare dashboard ↗ and select your account.
- In Account Home, select Workers & Pages.
- Select your Pages project > Settings.
- Select your Pages environment > Functions > Add > Queue.
- Give your binding a name under Variable name.
- Under Queue, select your desired queue.
- Redeploy your project for the binding to take effect.
Below is an example of how to use a queue producer binding in your Function. In this example, the binding is named MY_QUEUE
and you can access the binding in your Function code on context.env
::
If using a queue producer binding with a Pages Function, you will be able to send events to a queue locally. However, it is not possible to consume events from a queue with a Pages Function. You will have to create a separate consumer Worker with a queue consumer handler to consume events from the queue. Wrangler does not yet support running separate producer Functions and consumer Workers bound to the same queue locally.
Hyperdrive is a service for connecting to your existing databases from Cloudflare Workers and Pages Functions.
To bind your Hyperdrive config to your Pages Function, you can configure a Hyperdrive binding in wrangler.toml
or the Cloudflare dashboard.
To configure a Hyperdrive binding via the Cloudflare dashboard:
- Log in to the Cloudflare dashboard ↗ and select your account.
- In Account Home, select Workers & Pages.
- Select your Pages project > Settings.
- Select your Pages environment > Bindings > Add > Hyperdrive.
- Give your binding a name under Variable name.
- Under Hyperdrive configuration, select your desired configuration.
- Redeploy your project for the binding to take effect.
Below is an example of how to use Hyperdrive in your Function. In the following example, your Hyperdrive config is named HYPERDRIVE
and you can access the binding in your Function code on context.env
:
To interact with your Hyperdrive binding locally, you must provide a local connection string to your database that your Pages project will connect to directly. You can set an environment variable WRANGLER_HYPERDRIVE_LOCAL_CONNECTION_STRING_<BINDING_NAME>
with the connection string of the database, or use the wrangler.toml
file to configure your Hyperdrive binding with a localConnectionString
as specified in Hyperdrive documentation for local development. Then, run npx wrangler pages dev <OUTPUT_DIR>
.
The Analytics Engine binding enables you to write analytics within your Pages Function.
To bind an Analytics Engine dataset to your Pages Function, you must configure an Analytics Engine binding using wrangler.toml
or the Cloudflare dashboard:
To configure an Analytics Engine binding via the Cloudflare dashboard:
- Log in to the Cloudflare dashboard ↗ and select your account.
- In Account Home, select Workers & Pages.
- Select your Pages project > Settings.
- Select your Pages environment > Bindings > Add > Analytics engine.
- Give your binding a name under Variable name.
- Under Dataset, input your desired dataset.
- Redeploy your project for the binding to take effect.
Below is an example of how to use an Analytics Engine binding in your Function. In the following example, the binding is called ANALYTICS_ENGINE
and you can access the binding in your Function code on context.env
:
You cannot use an Analytics Engine binding locally.
An environment variable is an injected value that can be accessed by your Functions. Environment variables are a type of binding that allow you to attach text strings or JSON values to your Pages Function. It is stored as plain text. Set your environment variables directly within the Cloudflare dashboard for both your production and preview environments at runtime and build-time.
To add environment variables to your Pages project, you can use wrangler.toml
or the Cloudflare dashboard.
To configure an environment variable via the Cloudflare dashboard:
- Log in to the Cloudflare dashboard ↗ and select your account.
- In Account Home, select Workers & Pages.
- Select your Pages project > Settings.
- Select your Pages environment > Variables and Secrets > Add .
- After setting a variable name and value, select Save.
Below is an example of how to use environment variables in your Function. The environment variable in this example is ENVIRONMENT
and you can access the environment variable on context.env
:
You can interact with your environment variables locally in one of two ways:
- Configure your Pages project's
wrangler.toml
file and runningnpx wrangler pages dev
. - Pass arguments to
wrangler pages dev
directly.
To interact with your environment variables locally via the Wrangler CLI, add --binding=<ENVIRONMENT_VARIABLE_NAME>=<ENVIRONMENT_VARIABLE_VALUE>
to the wrangler pages dev
command:
Secrets are a type of binding that allow you to attach encrypted text values to your Pages Function. You cannot see secrets after you set them and can only access secrets programmatically on context.env
. Secrets are used for storing sensitive information like API keys and auth tokens.
To add secrets to your Pages project:
- Log in to the Cloudflare dashboard ↗ and select your account.
- In Account Home, select Workers & Pages.
- Select your Pages project > select Settings.
- Select your Pages environment > Variables and Secrets > Add.
- Set a variable name and value.
- Select Encrypt to create your secret.
- Select Save.
You use secrets the same way as environment variables. When setting secrets with Wrangler or in the Cloudflare dashboard, it needs to be done before a deployment that uses those secrets. For more guidance, refer to Environment variables.
When developing locally, add secrets by creating a .dev.vars
file in the root directory of your Pages project. Then add the following code snippet to .dev.vars
: