Connect your Cloudflare Worker with Xata in one click

Connecting a Cloudflare Worker with Xata is now possible from the Cloudflare dashboard.

Written by

Alexis Rico

Published on

December 14, 2023

Cloudflare Workers is a serverless platform that empowers developers to run code at the edge of the network. This means running code closer to users, resulting in faster response times and lower latency. Cloudflare Workers also offers a secure environment, ensuring code isolation from other users on the same server.

We are thrilled to announce a collaboration with Cloudflare, enabling a one-click integration directly from the Cloudflare dashboard. This user-friendly solution eliminates the need for developers to create an API key and manually configure their database connection, instead you can create environment variables by merely following the flow below.

#

Integrate Xata with Cloudflare

To get started with the Cloudflare integration for production environments, all you need is a Xata database in a workspace. With the integration, Cloudflare forms an automatic link to your Xata database, taking charge of setting up and managing the essential connection parameters.

Cloudflare Integration Image
Cloudflare dashboard with the Xata integration

Simply log into the Cloudflare dashboard, go to Workers > Overview, and select your worker from the listed options. Then, click the Integrations tab, choose the Xata integration, and click Add Integration to be guided through connecting your Cloudflare Worker with Xata.

Cloudflare Integration Image
Wizard to connect your Cloudflare Worker with Xata

During the process, you'll be prompted to authorize Cloudflare to access your Xata account. Once authorized, you can select the database you want to connect to your worker.

Cloudflare Integration Image
Page to authorize Cloudflare to access your Xata account

After authorizing Cloudflare, continue the flow and select the specific workspace, database, and branch you wish to connect with your worker.

Cloudflare Integration Image
Page to select the database you want to connect to your worker

All necessary environment variables will be automatically added to your worker.

Cloudflare Integration Image
List of environment variables added to your worker

In your worker, add the environment variables to establish a connection with your database. The following script shows how a Cloudflare Worker can interact with a Xata database using environment variables for configuration and the Xata client library for database operations.

import { XataClient } from './xata';

export interface Env {
  XATA_BRANCH: string;
  XATA_API_KEY: string;
  XATA_DATABASE_URL: string;
}

export default {
  async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
    const xata = new XataClient({
      apiKey: env.XATA_API_KEY,
      branch: env.XATA_BRANCH,
      databaseURL: env.XATA_DATABASE_URL
    });

    const posts = await xata.db.Posts.getAll();
    return new Response(`Total Posts: ${posts.length}`);
  }
};

The integration is powered by an OAuth2 flow, allowing Cloudflare to access your Xata account and create a new API key for your worker. The API key is then used to connect your worker with your database.

For more details on integrating with Xata using OAuth2, refer to our OAuth2 integration documentation.

The integration is now available for all Cloudflare users. To learn more about how to use it, consult our documentation.

For questions or feedback, reach out to us on X / Twitter or Discord. Sign up today to get started with Xata 🚀

Happy coding!

Start free,
pay as you grow

Xata provides the best free plan in the industry. It is production ready by default and doesn't pause or cool-down. Take your time to build your business and upgrade when you're ready to scale.

Free plan includes
  • 10 database branches
  • High availability
  • 15 GB data storage
  • 15 GB search engine storage
  • 2 GB file attachments
  • 250 AI queries per month
Start freeExplore all plans
Free plan includes
  • 10 database branches
  • High availability
  • 15 GB data storage
  • 15 GB search engine storage
  • 2 GB file attachments
  • 250 AI queries per month
Add column to table

Copyright © 2024 Xatabase Inc.
All rights reserved.

Product

RoadmapFeature requestsPricingStatusAI solutionsFile attachments