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.

Prompt


_115
---
_115
# Specify the following for Cursor rules
_115
description: Coding rules for Supabase Edge Functions
_115
globs: "supabase/functions/**/*.ts"
_115
---
_115
_115
# Writing Supabase Edge Functions
_115
_115
You're an expert in writing TypeScript and Deno JavaScript runtime. Generate **high-quality Supabase Edge Functions** that adhere to the following best practices:
_115
_115
## Guidelines
_115
_115
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)
_115
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.
_115
3. Do NOT use bare specifiers when importing dependecnies. 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`.
_115
4. For external imports, always define a version. For example, `npm:@express` should be written as `npm:express@4.18.2`.
_115
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.
_115
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.
_115
7. Do NOT use `import { serve } from "https://deno.land/std@0.168.0/http/server.ts"`. Instead use the built-in `Deno.serve`.
_115
8. Following environment variables (ie. secrets) are pre-populated in both local and hosted Supabase environments. Users don't need to manually set them:
_115
* SUPABASE_URL
_115
* SUPABASE_ANON_KEY
_115
* SUPABASE_SERVICE_ROLE_KEY
_115
* SUPABASE_DB_URL
_115
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`
_115
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.
_115
11. File write operations are ONLY permitted on `/tmp` directory. You can use either Deno or Node File APIs.
_115
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.
_115
_115
## Example Templates
_115
_115
### Simple Hello World Function
_115
_115
```tsx
_115
interface reqPayload {
_115
name: string;
_115
}
_115
_115
console.info('server started');
_115
_115
Deno.serve(async (req: Request) => {
_115
const { name }: reqPayload = await req.json();
_115
const data = {
_115
message: `Hello ${name} from foo!`,
_115
};
_115
_115
return new Response(
_115
JSON.stringify(data),
_115
{ headers: { 'Content-Type': 'application/json', 'Connection': 'keep-alive' }}
_115
);
_115
});
_115
_115
```
_115
_115
### Example Function using Node built-in API
_115
_115
```tsx
_115
import { randomBytes } from "node:crypto";
_115
import { createServer } from "node:http";
_115
import process from "node:process";
_115
_115
const generateRandomString = (length) => {
_115
const buffer = randomBytes(length);
_115
return buffer.toString('hex');
_115
};
_115
_115
const randomString = generateRandomString(10);
_115
console.log(randomString);
_115
_115
const server = createServer((req, res) => {
_115
const message = `Hello`;
_115
res.end(message);
_115
});
_115
_115
server.listen(9999);
_115
```
_115
_115
### Using npm packages in Functions
_115
_115
```tsx
_115
import express from "npm:express@4.18.2";
_115
_115
const app = express();
_115
_115
app.get(/(.*)/, (req, res) => {
_115
res.send("Welcome to Supabase");
_115
});
_115
_115
app.listen(8000);
_115
_115
```
_115
_115
### Generate embeddings using built-in @Supabase.ai API
_115
_115
```tsx
_115
const model = new Supabase.ai.Session('gte-small');
_115
_115
Deno.serve(async (req: Request) => {
_115
const params = new URL(req.url).searchParams;
_115
const input = params.get('text');
_115
const output = await model.run(input, { mean_pool: true, normalize: true });
_115
return new Response(
_115
JSON.stringify(
_115
output,
_115
),
_115
{
_115
headers: {
_115
'Content-Type': 'application/json',
_115
'Connection': 'keep-alive',
_115
},
_115
},
_115
);
_115
});
_115
_115
```