Documentation Index
Fetch the complete documentation index at: https://launchdarkly-preview.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
The Cloudflare integration is only available to customers on select plans. To learn more, read about our pricing. To upgrade your plan, contact Sales.
Overview
This topic explains how to use LaunchDarkly’s Cloudflare integration. This integration enables flag evaluation within Cloudflare Workers by writing the latest flag data from the configured environment directly to your Workers KV. To learn more about Cloudflare Workers KV, read How KV Works.You must install and configure the Cloudflare SDK in your Cloudflare Worker to use this integration. To learn more, read Cloudflare SDK.
Create a Cloudflare Worker with the Launch
Darkly Cloudflare SDKCloudflare Workers enforce KV size limits that might be smaller than your LaunchDarkly flag data in a given environment.To learn more, read about KV limits.
- Install Wrangler, the Workers CLI.
- Authenticate Wrangler:
wrangler login command, read login.
- Create a new project using the LaunchDarkly Cloudflare Worker template. Replace
my-projectwith the name of the project you wish to create:
wrangler generate command, read generate.
- Add your Cloudflare account ID to your
wrangler.tomlfile:
wrangler whoami.
- Create a new Cloudflare Workers KV namespace:
- Add the
nodejs_compatflag towrangler.toml. This is required because the Cloudflare SDK usesnode:events, which needs to be enabled:
- Bind your Worker with the new KV namespace by adding the code Wrangler returned in step 5 to the
kv_namespacesarray in your Worker’swrangler.tomlfile:
- (Optional) If you intend to use
wrangler devfor local development, you can also create a preview namespace:
kv_namespaces array. Replace the existing array entry created above with the value returned by Wrangler.
- (Optional) If you set up a preview namespace, you may run your worker locally:
wrangler dev command, read dev.
- Publish your Worker to Cloudflare:
wrangler publish command, read publish.
Your worker is running, but flag data will not be synced to your namespace until you set up the LaunchDarkly Cloudflare integration. To learn how, read Configure the LaunchDarkly Cloudflare integration.
Configure the Launch
Darkly Cloudflare integrationFor a Cloudflare integration to work, it needs a Cloudflare API token. To create a Cloudflare API token read Creating API tokens. Your token needs to have Account - Workers KV Storage:Edit permissions. You can create and manage Cloudflare integrations on the Integrations page. To create a new Cloudflare integration for your environment:- In LaunchDarkly, click the gear icon in the left sidenav to view Organization settings.
- Click Integrations and find “Cloudflare.”
- Click Add integration. A “Create Cloudflare configuration” panel appears.
- (Optional) Enter a name for the integration.
- Choose an environment from the Environment menu.
- Enter your Cloudflare Account ID. You can find this on the Worker home in Cloudflare, in the right side navigation.
- Enter your KV Namespace ID.
- Enter your API token generated from Cloudflare.
- Click Save configuration.You can evaluate flags using the flag data that is set up to write to your Worker KV using the Cloudflare SDK.
Use the Cloudflare integration to bootstrap feature flags
You can use the LaunchDarkly Cloudflare integration with Workers Sites to serve HTML injected with feature flags for bootstrapping client-side SDKs. To learn more about Workers Sites, read Cloudflare’s documentation. To serve HTML injected with feature flags:HTMLRewriter to inject a script tag into the head of your document that saves your set of feature flags to window.ldFlags. To learn more about HTMLRewriter, read Cloudflare’s documentation.
These flag values can then be used to bootstrap your client-side SDK. For example, if you are using the React Web SDK:
Troubleshooting
If you encounter a4xx error when trying to access the Cloudflare integration, it usually means the request was invalid or access is restricted. To learn how to resolve these errors, read the LaunchDarkly Knowledge Base article Troubleshooting Cloudflare Integration 4XX error codes.