Skip to content

feat(backend): support configurable Frontend API proxy URLs#9223

Open
thiskevinwang wants to merge 1 commit into
mainfrom
kevin/aie-1015-bug-frontend-api-proxy-routes-custom-staging-frontend-api
Open

feat(backend): support configurable Frontend API proxy URLs#9223
thiskevinwang wants to merge 1 commit into
mainfrom
kevin/aie-1015-bug-frontend-api-proxy-routes-custom-staging-frontend-api

Conversation

@thiskevinwang

@thiskevinwang thiskevinwang commented Jul 23, 2026

Copy link
Copy Markdown
Member
  • Add fapiUrl support to backend and Next.js proxy helpers, including
    CLERK_FAPI_URL fallback, URL validation, and trailing-slash normalization
    for lower-environment FAPI targets.

This allows helpers like clerkFrontendApiProxy and createFrontendApiProxyHandlers to target different FAPIs.

Description

Checklist

  • pnpm test runs as expected.
  • pnpm build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

@vercel

vercel Bot commented Jul 23, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
clerk-js-sandbox Ready Ready Preview, Comment Jul 23, 2026 2:20am
swingset Ready Ready Preview, Comment Jul 23, 2026 2:20am

Request Review

@changeset-bot

changeset-bot Bot commented Jul 23, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 20728fb

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 10 packages
Name Type
@clerk/backend Minor
@clerk/nextjs Minor
@clerk/astro Patch
@clerk/express Patch
@clerk/fastify Patch
@clerk/hono Patch
@clerk/nuxt Patch
@clerk/react-router Patch
@clerk/tanstack-react-start Patch
@clerk/testing Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Jul 23, 2026

Copy link
Copy Markdown

Open in StackBlitz

@clerk/astro

npm i https://pkg.pr.new/@clerk/astro@9223

@clerk/backend

npm i https://pkg.pr.new/@clerk/backend@9223

@clerk/chrome-extension

npm i https://pkg.pr.new/@clerk/chrome-extension@9223

@clerk/clerk-js

npm i https://pkg.pr.new/@clerk/clerk-js@9223

@clerk/electron

npm i https://pkg.pr.new/@clerk/electron@9223

@clerk/electron-passkeys

npm i https://pkg.pr.new/@clerk/electron-passkeys@9223

@clerk/eslint-plugin

npm i https://pkg.pr.new/@clerk/eslint-plugin@9223

@clerk/expo

npm i https://pkg.pr.new/@clerk/expo@9223

@clerk/expo-google-signin

npm i https://pkg.pr.new/@clerk/expo-google-signin@9223

@clerk/expo-passkeys

npm i https://pkg.pr.new/@clerk/expo-passkeys@9223

@clerk/express

npm i https://pkg.pr.new/@clerk/express@9223

@clerk/fastify

npm i https://pkg.pr.new/@clerk/fastify@9223

@clerk/hono

npm i https://pkg.pr.new/@clerk/hono@9223

@clerk/localizations

npm i https://pkg.pr.new/@clerk/localizations@9223

@clerk/nextjs

npm i https://pkg.pr.new/@clerk/nextjs@9223

@clerk/nuxt

npm i https://pkg.pr.new/@clerk/nuxt@9223

@clerk/react

npm i https://pkg.pr.new/@clerk/react@9223

@clerk/react-router

npm i https://pkg.pr.new/@clerk/react-router@9223

@clerk/shared

npm i https://pkg.pr.new/@clerk/shared@9223

@clerk/tanstack-react-start

npm i https://pkg.pr.new/@clerk/tanstack-react-start@9223

@clerk/testing

npm i https://pkg.pr.new/@clerk/testing@9223

@clerk/ui

npm i https://pkg.pr.new/@clerk/ui@9223

@clerk/upgrade

npm i https://pkg.pr.new/@clerk/upgrade@9223

@clerk/vue

npm i https://pkg.pr.new/@clerk/vue@9223

commit: 20728fb

@coderabbitai

coderabbitai Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The backend proxy adds an optional fapiUrl, validates and normalizes its value, and resolves it before environment or publishable-key fallbacks. Invalid configuration returns a 500 error. Backend tests cover precedence, validation, environment isolation, and redirect rewriting. Next.js forwards fapiUrl to the backend proxy, with a new handler test covering the resulting request URL. A changeset records minor package updates.

Estimated code review effort: 3 (Moderate) | ~20 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly summarizes the main change: configurable Frontend API proxy URLs.
Description check ✅ Passed The description directly matches the PR changes, including fapiUrl support, validation, and fallback behavior.

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/backend/src/__tests__/proxy.test.ts`:
- Around line 238-252: Expand the invalid FAPI URL tests around
clerkFrontendApiProxy to cover URLs containing credentials, query strings, and
fragments, plus an invalid CLERK_FAPI_URL configuration. Assert each returns
status 500 with the configuration error response and that mockFetch is not
called, preserving the existing unsupported-scheme coverage.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Run ID: f5bf50f0-f358-4326-b810-5780be707f09

📥 Commits

Reviewing files that changed from the base of the PR and between 097432d and 7880501.

📒 Files selected for processing (5)
  • .changeset/bright-pandas-proxy.md
  • packages/backend/src/__tests__/proxy.test.ts
  • packages/backend/src/proxy.ts
  • packages/nextjs/src/server/__tests__/proxy.test.ts
  • packages/nextjs/src/server/proxy.ts
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • clerk/clerk_go (manual)

Comment thread packages/backend/src/__tests__/proxy.test.ts Outdated
@github-actions

github-actions Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

API Changes Report

Generated by Break Check on 2026-07-23T02:21:09.418Z

Summary

Metric Count
Packages analyzed 19
Packages with changes 1
🔴 Breaking changes 0
🟡 Non-breaking changes 0
🟢 Additions 1

@clerk/backend

Current version: 3.12.0
Recommended bump: MINOR → 3.13.0

Subpath ./proxy

🟢 Additions (1)

Added: FrontendApiProxyOptions.fapiUrl
+ fapiUrl?: string;

Added property FrontendApiProxyOptions.fapiUrl


Report generated by Break Check

Last ran on 20728fb.

- Add fapiUrl support to backend and Next.js proxy helpers, including
CLERK_FAPI_URL fallback, URL validation, and trailing-slash normalization
for lower-environment FAPI targets.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/backend/src/__tests__/proxy.test.ts`:
- Around line 224-236: Update the test “uses CLERK_FAPI_URL when no FAPI URL is
configured” to set CLERK_FAPI_URL with a trailing slash, while retaining the
expected fetch URL without a double slash. Keep the test focused on the
environment fallback path and leave the direct fapiUrl behavior unchanged.
- Around line 207-222: Update the public Next.js middleware options
documentation and delegated proxy-properties partial to include the new fapiUrl
option, describing its purpose and configuration behavior consistently with
clerkFrontendApiProxy and its existing options. Ensure the option appears in
both documented locations so the supported API is discoverable.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro Plus

Run ID: 7efa56d8-dd6f-4b85-a9e9-66faf7484fe1

📥 Commits

Reviewing files that changed from the base of the PR and between 7880501 and 20728fb.

📒 Files selected for processing (5)
  • .changeset/bright-pandas-proxy.md
  • packages/backend/src/__tests__/proxy.test.ts
  • packages/backend/src/proxy.ts
  • packages/nextjs/src/server/__tests__/proxy.test.ts
  • packages/nextjs/src/server/proxy.ts
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • clerk/clerk_go (manual)
  • clerk/dashboard (manual)
  • clerk/accounts (manual)
  • clerk/backoffice (manual)
  • clerk/clerk (manual)
  • clerk/clerk-docs (manual)
  • clerk/cloudflare-workers (manual)
🚧 Files skipped from review as they are similar to previous changes (3)
  • .changeset/bright-pandas-proxy.md
  • packages/nextjs/src/server/proxy.ts
  • packages/backend/src/proxy.ts

Comment on lines +207 to +222
it('uses the configured FAPI URL over CLERK_FAPI_URL and the publishable key', async () => {
process.env.CLERK_FAPI_URL = 'https://frontend-api.clerkstage.dev';
mockFetch.mockResolvedValue(new Response(JSON.stringify({ client: {} }), { status: 200 }));

const request = new Request('https://example.com/__clerk/v1/client');

await clerkFrontendApiProxy(request, {
publishableKey: 'pk_test_Y2xlcmsuZXhhbXBsZS5jb20k',
secretKey: 'sk_test_xxx',
fapiUrl: 'http://localhost:8001/',
});

const [url, options] = mockFetch.mock.calls[0];
expect(url).toBe('http://localhost:8001/v1/client');
expect(options.headers.get('Host')).toBe('localhost:8001');
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Document the new public fapiUrl option.

The linked Next.js middleware options table and delegated proxy-properties partial do not expose fapiUrl, leaving the new supported configuration undiscoverable. Update those docs alongside this API addition. As per coding guidelines, “Update documentation for API changes.”

🧰 Tools
🪛 Betterleaks (1.6.1)

[high] 214-214: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/backend/src/__tests__/proxy.test.ts` around lines 207 - 222, Update
the public Next.js middleware options documentation and delegated
proxy-properties partial to include the new fapiUrl option, describing its
purpose and configuration behavior consistently with clerkFrontendApiProxy and
its existing options. Ensure the option appears in both documented locations so
the supported API is discoverable.

Sources: Coding guidelines, Linked repositories

Comment on lines +224 to +236
it('uses CLERK_FAPI_URL when no FAPI URL is configured', async () => {
process.env.CLERK_FAPI_URL = 'https://frontend-api.clerkstage.dev';
mockFetch.mockResolvedValue(new Response(JSON.stringify({ client: {} }), { status: 200 }));

const request = new Request('https://example.com/__clerk/v1/client');

await clerkFrontendApiProxy(request, {
publishableKey: 'pk_test_Y2xlcmsuZXhhbXBsZS5jb20k',
secretKey: 'sk_test_xxx',
});

expect(mockFetch.mock.calls[0][0]).toBe('https://frontend-api.clerkstage.dev/v1/client');
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Cover trailing-slash normalization for CLERK_FAPI_URL.

This only verifies an environment URL without a trailing slash. Use https://frontend-api.clerkstage.dev/ here and retain the no-double-slash assertion, so the environment fallback contract is protected independently of the direct fapiUrl path.

🧰 Tools
🪛 Betterleaks (1.6.1)

[high] 231-231: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/backend/src/__tests__/proxy.test.ts` around lines 224 - 236, Update
the test “uses CLERK_FAPI_URL when no FAPI URL is configured” to set
CLERK_FAPI_URL with a trailing slash, while retaining the expected fetch URL
without a double slash. Keep the test focused on the environment fallback path
and leave the direct fapiUrl behavior unchanged.

Source: Coding guidelines

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant