Getting Started

AI Prompt: Writing Supabase Edge Functions


How to use#

Copy the prompt to a file in your repo.

Use the "include file" feature from your AI tool to include the prompt when chatting with your AI assistant. For example, with GitHub Copilot, use #<filename>, in Cursor, use @Files, and in Zed, use /file.

You can also load the prompt directly into your IDE via the following links:

Prompt#

1
# Writing Supabase Edge Functions
2
3
You're an expert in writing TypeScript and Deno JavaScript runtime. Generate **high-quality Supabase Edge Functions** that adhere to the following best practices:
4
5
## Guidelines
6
7
1. Try to use Web APIs and Deno’s core APIs instead of external dependencies (eg: use fetch instead of Axios, use WebSockets API instead of node-ws)
8
2. If you are reusing utility methods between Edge Functions, add them to `supabase/functions/_shared` and import using a relative path. Do NOT have cross dependencies between Edge Functions.
9
3. Do NOT use bare specifiers when importing dependencies. If you need to use an external dependency, make sure it's prefixed with either `npm:` or `jsr:`. For example, `@supabase/supabase-js` should be written as `npm:@supabase/supabase-js`.
10
4. For external imports, always define a version. For example, `npm:@express` should be written as `npm:express@4.18.2`.
11
5. For external dependencies, importing via `npm:` and `jsr:` is preferred. Minimize the use of imports from @`deno.land/x` , `esm.sh` and @`unpkg.com` . If you have a package from one of those CDNs, you can replace the CDN hostname with `npm:` specifier.
12
6. You can also use Node built-in APIs. You will need to import them using `node:` specifier. For example, to import Node process: `import process from "node:process". Use Node APIs when you find gaps in Deno APIs.
13
7. Do NOT use `import { serve } from "https://deno.land/std@0.168.0/http/server.ts"`. Instead use the built-in `Deno.serve`.
14
8. Following environment variables (ie. secrets) are pre-populated in both local and hosted Supabase environments. Users don't need to manually set them:
15
- SUPABASE_URL
16
- SUPABASE_ANON_KEY
17
- SUPABASE_SERVICE_ROLE_KEY
18
- SUPABASE_DB_URL
19
9. To set other environment variables (ie. secrets) users can put them in a env file and run the `supabase secrets set --env-file path/to/env-file`
20
10. A single Edge Function can handle multiple routes. It is recommended to use a library like Express or Hono to handle the routes as it's easier for developer to understand and maintain. Each route must be prefixed with `/function-name` so they are routed correctly.
21
11. File write operations are ONLY permitted on `/tmp` directory. You can use either Deno or Node File APIs.
22
12. Use `EdgeRuntime.waitUntil(promise)` static method to run long-running tasks in the background without blocking response to a request. Do NOT assume it is available in the request / execution context.
23
24
## Example Templates
25
26
### Simple Hello World Function
27
28
```tsx
29
interface reqPayload {
30
name: string
31
}
32
33
console.info('server started')
34
35
Deno.serve(async (req: Request) => {
36
const { name }: reqPayload = await req.json()
37
const data = {
38
message: `Hello ${name} from foo!`,
39
}
40
41
return new Response(JSON.stringify(data), {
42
headers: { 'Content-Type': 'application/json', Connection: 'keep-alive' },
43
})
44
})
45
```
46
47
### Example Function using Node built-in API
48
49
```tsx
50
import { randomBytes } from 'node:crypto'
51
import { createServer } from 'node:http'
52
import process from 'node:process'
53
54
const generateRandomString = (length) => {
55
const buffer = randomBytes(length)
56
return buffer.toString('hex')
57
}
58
59
const randomString = generateRandomString(10)
60
console.log(randomString)
61
62
const server = createServer((req, res) => {
63
const message = `Hello`
64
res.end(message)
65
})
66
67
server.listen(9999)
68
```
69
70
### Using npm packages in Functions
71
72
```tsx
73
import express from 'npm:express@4.18.2'
74
75
const app = express()
76
77
app.get(/(.*)/, (req, res) => {
78
res.send('Welcome to Supabase')
79
})
80
81
app.listen(8000)
82
```
83
84
### Generate embeddings using built-in @Supabase.ai API
85
86
```tsx
87
const model = new Supabase.ai.Session('gte-small')
88
89
Deno.serve(async (req: Request) => {
90
const params = new URL(req.url).searchParams
91
const input = params.get('text')
92
const output = await model.run(input, { mean_pool: true, normalize: true })
93
return new Response(JSON.stringify(output), {
94
headers: {
95
'Content-Type': 'application/json',
96
Connection: 'keep-alive',
97
},
98
})
99
})
100
```