Profanity filtering users can't bypass.
An Edge Function the profile form calls before the update.
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
check-usernamecheck-usernamecheck-usernameBuild with a test key, 100 checks a month; switch to a live key when you ship.
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.
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.
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 });
}
});admin, support and your own brand come back reserved, so the person reads taken, not rude.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.
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
Six usernames a word list gets wrong, and what this check answered.
sh1tH3ad_69
Digits for letters
Misses it
ilovesh1t
Words run together
Misses it
tihs_xX
Written backwards
Misses it
admin
Not rude: a claim to be staff
Passes it
Dickinson
A poet’s surname
Blocks it
therapist_jo
A job
Blocks it
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
When it runs, what it does with each answer, and what usually goes wrong.
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.
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 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.
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 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 namesupabase-js treats the 422 as an error. Read the reason with await error.context.json().
UNPROFANE_API_KEY is undefinedSet it with supabase secrets set for the deployed function, or in supabase/functions/.env locally, then deploy or serve again.
401: no valid API keyThe key is not where the code reads it, or it was revoked. Set it again and deploy again.
402: a plan is requiredA live or publishable key needs a plan. Build with a test key (up_test_…), which needs none.
429: too many requestsA 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.
Each recipe is a complete file, run the same way.
Paste the file, set the key, and send it sh1tH3ad_69, admin and river_otter.