Skip to content

fix(http): send Supabase apikey header when anonKey is configured - #6

Merged
thinkclay merged 1 commit into
mainfrom
fix/http-anonkey-apikey-header
Jun 11, 2026
Merged

thinkclay merged 1 commit into
mainfrom
fix/http-anonkey-apikey-header

Conversation

@thinkclay

Copy link
Copy Markdown
Contributor

Summary

  • Adds a typed anonKey field to ScaffaldConfig and has HttpClient emit apikey: <anonKey> on every request when set.
  • Public-endpoint callers (e.g. /v1/auth/magic-link) can now pass anonKey alone — no Authorization required.
  • Without this, every SDK call against a baseUrl that sits behind Supabase Kong (any <project>.supabase.co/functions/v1/api) was rejected with 400 {"message":"No API key found in request"}.

Why this exists

Supabase's Kong gateway requires an apikey header on every /functions/v1/* request, even when Authorization carries a valid Bearer JWT. The SDK only emitted Authorization, so unauthenticated routes (magic-link) and even authenticated ones (anything proxied through Kong) failed at the gateway before the Edge Function ever ran. Discovered while debugging a prod magic-link regression on UNI-Construct.

Test plan

  • Added unit tests covering anonKey-alone construction, apikey header is sent, header is omitted when anonKey isn't set
  • pnpm test — 777/777 pass
  • pnpm typecheck — clean
  • pnpm build — clean dual CJS/ESM emit; apikey literal verified in built chunk

Backwards compat

  • anonKey is optional; existing callers that don't set it get identical behavior (no apikey header sent).
  • Error message for "no auth provided" updated to mention anonKey as a valid option.

🤖 Generated with Claude Code

Supabase Kong (the gateway in front of /functions/v1/*) rejects every
request that lacks an `apikey` header, even when Authorization carries a
valid Bearer JWT. The SDK previously only emitted Authorization, so any
caller whose baseUrl pointed at a Supabase Edge Function got back a 400
with `{"message":"No API key found in request"}`.

Adds a typed `anonKey` field to ScaffaldConfig and has HttpClient emit
`apikey: <anonKey>` on every request when it's set. Public-endpoint
callers can pass anonKey alone (no Authorization) — useful for
unauthenticated routes like /v1/auth/magic-link.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@thinkclay
thinkclay merged commit 11a863b into main Jun 11, 2026
@thinkclay
thinkclay deleted the fix/http-anonkey-apikey-header branch June 11, 2026 07:27
thinkclay added a commit to Scaffald/SaaS that referenced this pull request Jun 11, 2026
Picks up the merged Scaffald/sdk#6 — the typed anonKey field and
apikey-header emit are now on main.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
thinkclay added a commit to Scaffald/SaaS that referenced this pull request Jun 11, 2026
…ikey (#341)

* fix(auth): stop prod magic-link from hitting localhost and missing apikey

Two distinct bugs combined to break magic-link on app.scaffald.com:

1. `EXPO_PUBLIC_SCAFFALD_API_URL=http://127.0.0.1:54321/...` in `.env`
   leaked into the prod web bundle. Expo loads `.env` before
   `.env.production`, and `.env.production` doesn't define this var, so
   the dev value survived. The Scaffald SDK then routed every call to
   localhost from users' browsers. Fixed by documenting the gotcha
   prominently in `.env.template` (`.env` is gitignored — devs need to
   update their local copy).

2. Even if the URL were right, every request hit Supabase Kong without
   an `apikey` header, which Kong requires on `/functions/v1/*` (it
   returns `400 {"message":"No API key found in request"}` regardless of
   Authorization). Fixed at the SDK level (see Scaffald/sdk#6 —
   adds typed `anonKey` field, HttpClient emits `apikey` header from
   it) and plumbed through `jobs-sdk-context.tsx` from
   `EXPO_PUBLIC_SUPABASE_ANON_KEY`.

Bumps `packages/sdk` to a2bd925 (fix/http-anonkey-apikey-header
branch); re-bump to the merge commit once Scaffald/sdk#6 lands.

Verified against a fresh prod-mode bundle:
- `127.0.0.1:54321` no longer appears in `entry-*.js`.
- `auth.scaffald.com` is the only Supabase host present.
- `this.config.anonKey && (v.apikey = ...)` is in the minified output.

Co-Authored-By: Claude Opus 4.7 <[email protected]>

* chore(sdk): bump submodule to Scaffald/sdk@11a863b

Picks up the merged Scaffald/sdk#6 — the typed anonKey field and
apikey-header emit are now on main.

Co-Authored-By: Claude Opus 4.7 <[email protected]>

---------

Co-authored-by: Claude Opus 4.7 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant