Supabase Edge Functions
Run an HTTP bot on Supabase Edge Functions.
This page assumes you've read Serverless, and have a createBot() function like the one there.
Edge Functions run on Deno, so packages are imported with npm: specifiers.
Creating the function
In a project set up with the Supabase CLI:
npx supabase functions new discordDiscord doesn't send Supabase's authorization header, so turn off JWT verification for the function in supabase/config.toml:
[functions.discord]
verify_jwt = falseWriting the function
Your secrets are in Deno.env when the module loads, so the bot can be created right away. Pass EdgeRuntime.waitUntil,
so the function stays alive until your handlers finish. Keep the edge-runtime.d.ts import the CLI generated, which types it:
import 'jsr:@supabase/functions-js/edge-runtime.d.ts';
import { } from 'npm:@nyx-discord/http';
import { } from './bot.ts';
const = (
{
: Deno.env.get('APP_ID')!,
: Deno.env.get('DISCORD_PUBLIC_KEY')!,
: Deno.env.get('TOKEN')!,
},
false,
).then(() => {
const = .getClient();
return {
: (),
: (.getWebhookEventHandler()),
};
});
Deno.serve(async () => {
const { , } = await ;
const { } = new (.url);
const = .('/webhook-events') ? : ;
return (, { : () => EdgeRuntime.waitUntil() });
});Import HttpBot in your bot.ts from npm:@nyx-discord/http as well.
Running locally
Put your secrets in supabase/functions/.env:
APP_ID=...
DISCORD_PUBLIC_KEY=...
TOKEN=...npx supabase functions serveThis needs Docker running. Test it without Discord at
http://localhost:54321/functions/v1/discord.
Deploying
npx supabase secrets set --env-file supabase/functions/.envnpx supabase functions deploy discordRemember to deploy your commands too, from Node or with deno run.
Setting the endpoint
On the Developer Portal, set your app's Interactions Endpoint URL to https://<project-ref>.supabase.co/functions/v1/discord,
and its Webhook Events URL (if you use them) to .../functions/v1/discord/webhook-events.
Last updated on
