Skip to content

Latest commit

 

History

History
114 lines (89 loc) · 5.12 KB

cloudflare-integration.mdx

File metadata and controls

114 lines (89 loc) · 5.12 KB
title description image ogImage author authorEmail date tags published slug
Connect your Cloudflare Worker with Xata in one click
Connecting a Cloudflare Worker with Xata is now possible from the Cloudflare dashboard.
Alexis Rico
12-14-2023
cloudflare
serverless
database
integration
true
cloudflare-integration

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.

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.

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.

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

All necessary environment variables will be automatically 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}`);
  }
};

How does it work?

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.

What's next?

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!