You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -18,9 +18,11 @@ import WorkerFailedToStartWhenRunningDevCommand from "/snippets/worker-failed-to
18
18
19
19
## Overview
20
20
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.
22
22
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.
24
26
25
27
## Prerequisites
26
28
@@ -95,33 +97,33 @@ Install the "Hello World" example task when prompted. We'll use this task to tes
95
97
<CliViewRunStep />
96
98
</Steps>
97
99
98
-
## Create and a new Supabase edge function and trigger a dev run
100
+
## Create and deploy a new Supabase edge function
99
101
100
102
<Steps>
101
103
102
104
<Steptitle="Create a new edge function using the Supabase CLI">
103
105
104
-
We will call this example edge function `edge-function-trigger`.
106
+
We will call this example edge function `database-webhook`.
105
107
106
108
```bash
107
-
supabase functions new edge-function-trigger
109
+
supabase functions new database-webhook
108
110
```
109
111
110
112
</Step>
111
113
112
114
<Steptitle="Update the edge function code">
113
115
114
-
Replace the `edge-function-trigger` placeholder code with the following:
116
+
Replace the `database-webhook` placeholder code with the following:
<Steptitle="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
-

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
-
<Steptitle="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:
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`.
196
147
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`.
198
149
199
150
</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
-
<Steptitle="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
-

213
151
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
+
<Steptitle="Configure JWT settings">
215
153
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.
217
155
218
-

156
+
First, go to your Supabase dashboard, click 'Project settings', 'API' tab, and copy the `anon``public` API key from the table.
219
157
220
-
</Step>
221
-
222
-
<Steptitle="Deploy your edge function using the Supabase CLI">
158
+
[How to find your Supabase API keys]()
223
159
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'.
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'.
237
172
238
-
Your new `edge-function-trigger` edge function is now ready to trigger your `hello-world` task.
173
+
[How to configure the webhook]()
239
174
240
175
</Step>
176
+
<Steptitle="Trigger your new database webhook edge function">
241
177
242
-
<Steptitle="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.
243
179
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`.
247
181
248
-
```bash npm
249
-
npx trigger.dev@beta deploy
250
-
```
182
+
[How to insert a new row]()
251
183
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'.
255
185
256
-
```bash yarn
257
-
yarn dlx trigger.dev@beta deploy
258
-
```
186
+
[How to view the logs]()
259
187
260
-
</CodeGroup>
188
+
Congratulations, you have successfully triggered a task from a Supabase edge function using a database webhook!
261
189
262
190
</Step>
263
191
264
-
<Steptitle="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
-

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!**
0 commit comments