Skip to content

Commit ffdfe05

Browse files
committed
First pass without images
1 parent 348bc8e commit ffdfe05

File tree

1 file changed

+36
-125
lines changed

1 file changed

+36
-125
lines changed

docs/guides/frameworks/supabase-edge-functions-database-webhooks.mdx

Lines changed: 36 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ import WorkerFailedToStartWhenRunningDevCommand from "/snippets/worker-failed-to
1818

1919
## Overview
2020

21-
Supabase edge functions allow you to trigger tasks either when an event is sent from a third party (e.g. when a new Stripe payment is processed, when a new user signs up to a service, etc), or when there are any changes / updates to your Supabase database.
21+
Database webhooks allow you to send realtime data from your database to another system whenever an event occurs in your table.
2222

23-
This guide shows you how to set up and deploy a simple Supabase edge function example that triggers a task when an edge function URL is accessed.
23+
This could be when a new row is inserted, updated, or deleted, or when a specific column is updated.
24+
25+
This guide shows you how to set up and deploy a simple Supabase edge function that triggers a task every time a new row is added to your database.
2426

2527
## Prerequisites
2628

@@ -95,33 +97,33 @@ Install the "Hello World" example task when prompted. We'll use this task to tes
9597
<CliViewRunStep />
9698
</Steps>
9799

98-
## Create and a new Supabase edge function and trigger a dev run
100+
## Create and deploy a new Supabase edge function
99101

100102
<Steps>
101103

102104
<Step title="Create a new edge function using the Supabase CLI">
103105

104-
We will call this example edge function `edge-function-trigger`.
106+
We will call this example edge function `database-webhook`.
105107

106108
```bash
107-
supabase functions new edge-function-trigger
109+
supabase functions new database-webhook
108110
```
109111

110112
</Step>
111113

112114
<Step title="Update the edge function code">
113115

114-
Replace the `edge-function-trigger` placeholder code with the following:
116+
Replace the `database-webhook` placeholder code with the following:
115117

116-
```ts edge-function-trigger/index.ts
118+
```ts database-webhook/index.ts
117119
import "jsr:@supabase/functions-js/edge-runtime.d.ts";
118120
import { tasks } from "npm:@trigger.dev/[email protected]/v3";
119121
// Import your task type from your /trigger folder
120122
import type { helloWorldTask } from "../../../src/trigger/example.ts";
121123

122124
Deno.serve(async (req) => {
123125
// @ts-expect-error: Ignoring Task type mismatch and RunHandle incompatibility
124-
await tasks.trigger<typeof helloWorldTask>("edge-function-trigger", "hello");
126+
await tasks.trigger<typeof helloWorldTask>("database-webhook", "hello");
125127
return new Response("OK");
126128
});
127129
```
@@ -135,149 +137,58 @@ Deno.serve(async (req) => {
135137

136138
</Step>
137139

138-
<Step title="Add a .env file to be able to test locally">
139-
140-
First, go to your Trigger.dev project and copy the `dev` secret key from the API keys page.
141-
142-
![How to find your dev secret key](/images/api-key-dev.png)
143-
144-
Then create an `.env` file in the functions folder, and add your `TRIGGER_SECRET_KEY`.
145-
146-
```ts functions/.env
147-
TRIGGER_SECRET_KEY = "your-secret-key";
148-
```
149-
150-
<Warning>
151-
If you are using GitHub - make sure to include `.env` in your `.gitignore` file before committing
152-
any files to GitHub.
153-
</Warning>
154-
155-
</Step>
156-
157-
<Step title="Test your new edge function locally">
158-
159-
To do this, open up two terminal windows in your project.
160-
161-
In the first terminal window run:
162-
163-
<CodeGroup>
164-
165-
```bash npm
166-
npx trigger.dev@beta dev
167-
```
168-
169-
```bash pnpm
170-
pnpm dlx trigger.dev@beta dev
171-
```
172-
173-
```bash yarn
174-
yarn dlx trigger.dev@beta dev
175-
```
176-
177-
</CodeGroup>
178-
179-
The CLI `dev` command runs a server for your tasks. It watcbes for changes in your `/trigger` directory and communicates with the Trigger.dev platform to register your tasks, perform runs, and send data back and forth.
180-
181-
In the second terminal run this command:
182-
183-
```bash
184-
supabase functions serve edge-function-trigger
185-
```
186-
187-
Click the link provided to open the preview URL, making sure to add your function name to the end:
188-
189-
`http://127.0.0.1:54321/functions/v1/<function-name>`
140+
<Step title="Create a new database">
190141

191-
So in this case:
142+
In your Supabase dashboard, click on 'Table Editor' in the left-hand menu.
192143

193-
`http://127.0.0.1:54321/functions/v1/edge-function-trigger`
144+
[How to create a new table]()
194145

195-
When the URL is opened, your `hello-world` task should be triggered, and you should see `"OK"` in your browser window.
146+
Create a new table called `skynet`.
196147

197-
Check the first terminal window to see if the task has run successfully. You can also check the [cloud.trigger.dev](http://cloud.trigger.dev) runs dashboard to see if there has been a successful run.
148+
Add a new column called `name` with the type `text`.
198149

199150
</Step>
200-
</Steps>
201-
202-
## Deploy your new Supabase edge function and Trigger.dev task
203-
204-
Now that your edge function is working in dev, you can deploy it to production.
205-
206-
<Steps>
207-
208-
<Step title="Add your Trigger.dev prod secret key in Supabase">
209-
210-
Go to your Trigger.dev project and copy the `prod` secret key from the API keys page.
211-
212-
![How to find your prod secret key](/images/api-key-prod.png)
213151

214-
Then, in Supabase, navigate to your settings page, click 'Edge functions' in the configurations menu, and then click the 'Add new secret' button.
152+
<Step title="Configure JWT settings">
215153

216-
Add `TRIGGER_SECRET_KEY` with the pasted value of your Trigger.dev `prod` secret key.
154+
By default, Supabase edge functions require a JSON Web Token (JWT) in the authorization header. This is to ensure that only authorized users can access your edge functions.
217155

218-
![Add secret key in Supabase](/images/supabase-keys-1.png)
156+
First, go to your Supabase dashboard, click 'Project settings', 'API' tab, and copy the `anon` `public` API key from the table.
219157

220-
</Step>
221-
222-
<Step title="Deploy your edge function using the Supabase CLI">
158+
[How to find your Supabase API keys]()
223159

224-
Go back to your project, and deploy your edge function with the following command:
160+
Then, go to 'Database' in the Supabase dashboard, click on 'Webhooks', and then click 'Create a new hook'.
225161

226-
```bash
227-
supabase functions deploy edge-function-trigger --no-verify-jwt
228-
```
162+
[How to create a new webhook]()
229163

230-
<Note>
231-
`--no-verify-jwt` removes the JSON Web Tokens requirement from the authorization header. By
232-
default this should be on, but it is not required for this example. Learn more about JWTs
233-
[here](https://supabase.com/docs/guides/auth/jwts).
234-
</Note>
164+
Call the hook `edge-function-hook`:
235165

236-
Follow the CLI instructions and once complete you should now see your new edge function deployment in your Supabase edge functions dashboard.
166+
1. Select the new table you have created: `public``skynet`.
167+
2. Choose the `insert` event.
168+
3. Under 'Webhook configuration', select 'Supabase Edge functions'
169+
4. Under 'Edge function', choose `POST` and select the edge function you have created: `database-webhook`.
170+
5. Under 'Headers', add a new header with the key `Authorization` and the value `Bearer <your-api-key>` (replace `<your-api-key>` with the `anon` `public` API key you copied earlier).
171+
6. Click 'Create webhook'.
237172

238-
Your new `edge-function-trigger` edge function is now ready to trigger your `hello-world` task.
173+
[How to configure the webhook]()
239174

240175
</Step>
176+
<Step title="Trigger your new database webhook edge function">
241177

242-
<Step title="Deploy your hello-world task">
178+
Go back to your Supabase dashboard, click on 'Table Editor' in the left-hand menu, and then click on the `skynet` table.
243179

244-
Deploy your `hello-world` task to [Trigger.dev cloud](https://cloud.trigger.dev).
245-
246-
<CodeGroup>
180+
Click 'insert', and add a new item under `name`, with the value `Sarah Connor`.
247181

248-
```bash npm
249-
npx trigger.dev@beta deploy
250-
```
182+
[How to insert a new row]()
251183

252-
```bash pnpm
253-
pnpm dlx trigger.dev@beta deploy
254-
```
184+
Go back to your edge function dashboard, and you should see a new run of your edge function, under 'Logs'.
255185

256-
```bash yarn
257-
yarn dlx trigger.dev@beta deploy
258-
```
186+
[How to view the logs]()
259187

260-
</CodeGroup>
188+
Congratulations, you have successfully triggered a task from a Supabase edge function using a database webhook!
261189

262190
</Step>
263191

264-
<Step title="Trigger a prod run from your deployed edge function">
265-
266-
To do this all you need to do is simply open the `edge-function-trigger` URL.
267-
268-
On your Supabase dashboard, go to your Edge function dashboard, copy the URL, and paste it into your browser.
269-
270-
Once loaded you should see ‘OK’ on the new screen.
271-
272-
![Edge function URL](/images/supabase-function-url.png)
273-
274-
The task will be triggered when your edge function URL is accessed.
275-
276-
Check the terminal to see if the task has run. You can also check the [cloud.trigger.dev](http://cloud.trigger.dev) dashboard for any successful runs.
277-
278-
**Congratulations, you have run a simple Hello World task from a Supabase edge function!**
279-
280-
</Step>
281192
</Steps>
282193

283194
<UsefulNextSteps />

0 commit comments

Comments
 (0)