Skip to Content
nyx logonyx guide
Supabase

Supabase Edge Functions

Run an HTTP bot on Supabase Edge Functions.

Reading time:1 min read

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 discord

Discord doesn't send Supabase's authorization header, so turn off JWT verification for the function in supabase/config.toml:

[functions.discord]
verify_jwt = false

Writing 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:

supabase/functions/discord/index.ts
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 serve

This needs Docker running. Test it without Discord at http://localhost:54321/functions/v1/discord.

Deploying

npx supabase secrets set --env-file supabase/functions/.env
npx supabase functions deploy discord

Remember 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.

Edit on GitHub

Last updated on

On this page