Unprofane

Supabase username check in one Edge Function

An Edge Function the profile form calls before the update.

Your profile formExample

Profile

How other people see you

Username

sh1tH3ad_69

Pick a different username.

Profile

How other people see you

Username

admin

That name is reserved.

Profile

How other people see you

Username

river_otter

Saved

Unprofane
blockleetspeak
check-username
422{ ok: false, problem: "Pick a different username." }
Unprofane
blockreserved
check-username
422{ ok: false, problem: "That name is reserved." }
Unprofane
allow
check-username
200{ ok: true }
Try a name

Live in four steps

Build with a test key, 100 checks a month; switch to a live key when you ship.

  1. Before you start

    • A Supabase project, and the Supabase CLI linked to it.

    • A profile form that saves a username, with the supabase-js client in the page.

    • An Unprofane key: a test key while you build, a live key in production.

  2. Store the key

    • Set the key on the project:

      supabase secrets set UNPROFANE_API_KEY=up_live_…
    • For local runs, put UNPROFANE_API_KEY=up_test_… in supabase/functions/.env, which supabase functions serve reads.

    • The function is the only thing that holds the key.

      The page calls the function, never Unprofane.

  3. Where the file goes

    • supabase/functions/check-username/index.ts.

      Make it with supabase functions new check-username, replace its contents with the code, and deploy with supabase functions deploy check-username.

      The SDK is imported with an npm: specifier, so there is nothing to install.

    // supabase/functions/check-username/index.ts: the profile form calls this before it saves
    import { Unprofane } from 'npm:@unprofane/sdk@^0.1.2';
    
    let unprofane: Unprofane | undefined;
    
    /** Returns the reason a username cannot be shown, or null when it can. Throws when Unprofane cannot answer. */
    export async function usernameProblem(username: string): Promise<string | null> {
      unprofane ??= new Unprofane(Deno.env.get('UNPROFANE_API_KEY')!, { timeoutMs: 3_000, retries: 1 });
      const r = await unprofane.check({ text: username, context: 'username', policy: 'teen' });
      if (r.allowed) return null;
      return r.matches[0]?.category === 'reserved'
        ? 'That name is reserved.'
        : 'Pick a different username.';
    }
    
    // The form calls from a browser, so the function answers the preflight.
    const cors = {
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
    };
    
    Deno.serve(async (req) => {
      if (req.method === 'OPTIONS') return new Response('ok', { headers: cors });
      const { username } = await req.json().catch(() => ({}));
      if (typeof username !== 'string' || !username.trim())
        return Response.json({ ok: false, problem: 'Choose a username.' }, { status: 400, headers: cors });
      try {
        const problem = await usernameProblem(username);
        return Response.json(problem ? { ok: false, problem } : { ok: true }, { status: problem ? 422 : 200, headers: cors });
      } catch {
        // No answer is not a yes: the form says so and lets the person try again.
        return Response.json({ ok: false, problem: 'The name could not be checked. Try again.' }, { status: 503, headers: cors });
      }
    });
    1. Line 8A form waits on this, so it gives up after three seconds and one retry.
    2. Line 9Read as a handle: glued words are split, leetspeak and look-alike letters are read, and the name is tried backwards too. The teen policy lets mild words through and turns the rest away; pick the one for your audience.
    3. Line 12admin, support and your own brand come back reserved, so the person reads taken, not rude.
    4. Line 18The form calls from a browser, so the function answers the preflight and every answer carries CORS.
    5. Line 32No answer is not a yes. The form saves only on ok: true, so nothing unchecked is saved.

    Complete as printed: it reads the key from the environment. Every name goes with context: "username" and the teen policy.

  4. Check that it works

    • Run supabase functions serve, then send the function a name:

      curl -i http://localhost:54321/functions/v1/check-username -H "Authorization: Bearer YOUR_ANON_KEY" -H "Content-Type: application/json" -d '{"username":"sh1tH3ad_69"}'
    • sh1tH3ad_69 answers 422 with Pick a different username., admin answers 422 with That name is reserved., and river_otter answers 200 with ok: true.

    What the run sends, and what comes back@unprofane/sdk 0.1.2Deno 2.7.14

    1. sh1tH3ad_69422{ ok: false, problem: "Pick a different username." }
    2. admin422{ ok: false, problem: "That name is reserved." }
    3. river_otter200{ ok: true }
    4. Unprofane unreachable503{ ok: false, problem: "The name could not be checked. Try again." }

It reads the name, not a list

Six usernames a word list gets wrong, and what this check answered.

  1. sh1tH3ad_69

    Digits for letters

    Misses it

    blockreads shitheadleetspeak
  2. ilovesh1t

    Words run together

    Misses it

    blockreads shitleetspeakconcatenation
  3. tihs_xX

    Written backwards

    Misses it

    blockreads shitreversed
  4. admin

    Not rude: a claim to be staff

    Passes it

    blockreserved
  5. Dickinson

    A poet’s surname

    Blocks it

    allow
  6. therapist_jo

    A job

    Blocks it

    allow
Out of 6

4 let through2 innocent blocked

4 turned away2 innocent let through

76.5%

Unprofane

Balanced mode, as the recipes run it

32.5%

Perspective API

Set to flag as much clean text as Unprofane

Toxic usernames caught. Held-back judged test set, run of .How it was measured

How it behaves in production

When it runs, what it does with each answer, and what usually goes wrong.

When the check runs
  1. Typed
  2. Unprofane checks
  3. Saved
  4. Shown to others

Before the save, from your form: call supabase.functions.invoke("check-username", { body: { username } }) and save only on ok: true. The check binds only a client that calls it, and a client that skips it can still update the row. To make it binding, have this function save the name itself with the service role key, and take the client’s update permission on the username column away.

Allow, review and block
allow
Goes through
review
Turned away, like a block
block
Turned away

allowed is true only when action is allow. The recipes turn away review as well as block: a username has no moderator queue, and asking for another name costs the person a few seconds. To accept a name held for review and look at it later, test action !== "block" instead and keep the user id where a person will read it. A name that claims to be the platform or its staff, such as admin, comes back with matches[0].category set to reserved, and the code says so in its own words.

Every field of the answer is in the API reference.

When Unprofane does not answer

When Unprofane does not answer within the timeout and the one retry, the function answers 503 with The name could not be checked. Try again. The form saves only on ok: true, so nothing is saved, and the person can send the name again.

How this recipe is run on every change

Typechecked with deno check and run on Deno, the runtime Supabase’s Edge Runtime is built on: the preflight, an empty name, the three names below, and once with Unprofane unreachable. Supabase itself is not involved.

The browser reports a CORS error

The function answers the preflight and puts cors on every answer. An answer you add must carry it too.

invoke returns an error and no data for a refused name

supabase-js treats the 422 as an error. Read the reason with await error.context.json().

UNPROFANE_API_KEY is undefined

Set it with supabase secrets set for the deployed function, or in supabase/functions/.env locally, then deploy or serve again.

401: no valid API key

The key is not where the code reads it, or it was revoked. Set it again and deploy again.

402: a plan is required

A live or publishable key needs a plan. Build with a test key (up_test_…), which needs none.

429: too many requests

A test key’s free checks are capped across the account, a month’s with no plan and a day’s on one, and every plan has a per-minute limit. The answer’s type says which.

Every error, and what to send instead, is on the errors page.

The same check, elsewhere

Each recipe is a complete file, run the same way.

Run this recipe with your API key

Paste the file, set the key, and send it sh1tH3ad_69, admin and river_otter.

Get your API key