Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat/cloudflare-integration-blog

awaliasf52e200a5274
diff --git a/apps/www/_blog/2023-05-18-supabase-cloudflare-integration.mdx b/apps/www/_blog/2023-05-18-supabase-cloudflare-integration.mdx
new file mode 100644
index 00000000000..532a44a797f
--- /dev/null
+++ b/apps/www/_blog/2023-05-18-supabase-cloudflare-integration.mdx
@@ -0,0 +1,101 @@
+---
+title: Cloudflare Integrates with Supabase
+description: Using Supabase from your Cloudflare Workers just got even easier.
+author: ant_wilson
+image: 2023-05-09-beta-update-april/monthly-update-april-2023.jpg
+thumb: 2023-05-09-beta-update-april/monthly-update-april-2023.jpg
+tags:
+ - release-notes
+date: '2023-05-18'
+toc_depth: 3
+---
+
+Using Supabase in Cloudflare Workers has always been a great way to interact with your data from the edge. Supabase-js communicates with your Supabase Postgres instance via HTTP using PostgREST, so you never need to worry about running out of database connections.
+
+In this post we'll walk you through a new addition to the Cloudflare Workers dashboard - the ability to integrate directly with your Supabase instance, and automatically inject your Supabase environment variables into your Worker code.
+
+## How To Enable Supabase Integration in Cloudflare Workers
+
+Start of by heading to the Cloudflare Dashboard, go to the Workers & Pages tab and hit 'Create Application' followed by 'Create Worker'.
+
+![Cloudflare Dashboard 2](/images/blog/cloudflare-integration/2.png)
+
+Deploy the Hello World example Worker. Once it's deployed hit 'Configure Worker'.
+
+On the configuration page select the Settings tab, followed by the Integrations option.
+
+![Cloudflare Dashboard 3](/images/blog/cloudflare-integration/3.png)
+
+You should now see the database integration options. On the Supabase card, click 'Add Integration'
+
+![Cloudflare Dashboard 4](/images/blog/cloudflare-integration/4.png)
+
+After reviewing and accepting the terms, you will be shown the option to connect and a Supabase popup should appear.
+
+Follow the flow by selecting your Supabase Org and the Project you wish to connect to. If you don't have any projects yet, head over to the [Supabase Dashboard](https://app.supabase.com) to create one.
+
+![Cloudflare Dashboard 5](/images/blog/cloudflare-integration/5.png)
+
+Once it's connected you will be given the option to select which Supabase Key you want to pull into the Worker context.
+
+The `Anon` key here is one that always adhires to the Database's RLS policies (read more on [Row Level Security](https://supabase.com/docs/guides/auth/row-level-security)).
+
+The `Service Role` is typically ok to use in backend contexts, such as Cloudflare Workers, but note that this key **bypasses your Row Level Security policies**, and has the ablity to read, write, and delete any data in your database.
+
+![Cloudflare Dashboard 6](/images/blog/cloudflare-integration/6.png)
+
+Once this is done the `SUPABASE_KEY` and `SUPABASE_URL` environment variables will now be available from your Cloudflare Worker code.
+
+You can now install the supabase-js client in your Worker:
+
+`npm install @supabase/supabase-js`
+
+Then you can initiate the Supabase client, and start querying your data:
+
+```javascript
+import { createClient } from '@supabase/supabase-js'
+
+export default {
+ async fetch(request, env) {
+ const supabase = createClient(env.SUPABASE_URL, env.SUPABASE_KEY)
+ const { data, error } = await supabase.from('countries').select('*')
+ if (error) throw error
+ return new Response(JSON.stringify(data), {
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ })
+ },
+}
+```
+
+The snippet above assumes you already have a `countries` table. Run the following in the [SQL Editor in the Supabase Dashboard](https://app.supabase.com/project/_/sql) if you wish to install this demo schema:
+
+```sql
+create table countries (
+ id serial primary key,
+ name varchar(255) not null
+);
+
+insert into countries
+ (name)
+values
+ ('Oceania');
+
+insert into countries
+ (name)
+values
+ ('Genovia');
+
+insert into countries
+ (name)
+values
+ ('Wakanda');
+
+insert into countries
+ (name)
+values
+ ('Lilliput');
+```
+
+Remember that you don't need to use supabase-js to connect to your Supabase database, you can connect "directly" to the underlying Postgres database using the connection string (every Supabase database comes pre-installed with a connection pooler), or you can try Cloudflare's new [TCP socket method of connecting to Postgres](https://blog.cloudflare.com/workers-tcp-socket-api-connect-databases/) directly from Cloudflare Workers.
diff --git a/apps/www/public/images/blog/cloudflare-integration/2.png b/apps/www/public/images/blog/cloudflare-integration/2.png
new file mode 100644
index 00000000000..4348e508742
Binary files /dev/null and b/apps/www/public/images/blog/cloudflare-integration/2.png differ
diff --git a/apps/www/public/images/blog/cloudflare-integration/3.png b/apps/www/public/images/blog/cloudflare-integration/3.png
new file mode 100644
index 00000000000..2a6db2dc104
Binary files /dev/null and b/apps/www/public/images/blog/cloudflare-integration/3.png differ
diff --git a/apps/www/public/images/blog/cloudflare-integration/4.png b/apps/www/public/images/blog/cloudflare-integration/4.png
new file mode 100644
index 00000000000..d1aba0bb706
Binary files /dev/null and b/apps/www/public/images/blog/cloudflare-integration/4.png differ
diff --git a/apps/www/public/images/blog/cloudflare-integration/5.png b/apps/www/public/images/blog/cloudflare-integration/5.png
new file mode 100644
index 00000000000..1db9444f210
Binary files /dev/null and b/apps/www/public/images/blog/cloudflare-integration/5.png differ
diff --git a/apps/www/public/images/blog/cloudflare-integration/6.png b/apps/www/public/images/blog/cloudflare-integration/6.png
new file mode 100644
index 00000000000..0c7ce386ac6
Binary files /dev/null and b/apps/www/public/images/blog/cloudflare-integration/6.png differ