This feature or workflow is considered advanced - it may not be part of an official Clerk SDK or fall within typical usage patterns. The Clerk support team will do their best to assist you, but cannot guarantee a resolution for this type of advanced usage.
Clerk supports two configuration methods for connecting to the Clerk Frontend API: CNAME and Proxy.
The recommended way to connect to the Clerk Frontend API is to set up CNAME records and use DNS. However, if you're unable to use this approach, or would like more control over your integration with Clerk, you can use a proxy.
When using a proxy, all requests to the Frontend API will be made through your domain. This allows you to use your own SSL certificate, and gives you more control over how you configure your application.
Warning
This guide is for users who need to proxy the Frontend API for production (proxying does not work for Clerk development instances). If your application already uses a CNAME subdomain that is required for deploying with Clerk, then you must proxy the Frontend API using a different subdomain. Refer to the deployment guide on how to configure DNS records for deployment.
To get started, you need to create an application from the Clerk Dashboard. Once you create an instance via the Clerk Dashboard, you will be prompted to choose a domain. For the purposes of this guide, the domain will be app.dev.
Note
For more information on creating a Clerk application, see the setup guide.
The frontendApiProxy option in clerkMiddleware() handles all proxy requirements automatically, including header forwarding, body streaming, and redirect rewriting. The proxyUrl for authentication handshake is also auto-derived.
Important
Ensure your middleware matcher includes the proxy path (by default, /__clerk) so proxy requests are handled by the middleware.
proxy.ts
import { clerkMiddleware } from'@clerk/nextjs/server'exportdefaultclerkMiddleware({ frontendApiProxy: { enabled:true, },})exportconstconfig= { matcher: [// Skip Next.js internals and all static files, unless found in search params'/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)',// Always run for API routes and the proxy path'/(api|trpc|__clerk)(.*)', ],}
For applications that serve multiple domains (e.g., preview deployments, staging environments), you can pass a function to enabled to conditionally enable proxying based on the request URL.
proxy.ts
import { clerkMiddleware } from'@clerk/nextjs/server'exportdefaultclerkMiddleware({ frontendApiProxy: {// Only proxy on non-production domainsenabled: (url) =>url.hostname !=='myapp.com', },})exportconstconfig= { matcher: [// Skip Next.js internals and all static files, unless found in search params'/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)',// Always run for API routes and the proxy path'/(api|trpc|__clerk)(.*)', ],}
As an alternative to the middleware approach, you can use createFrontendApiProxyHandlers() in a Next.js App Router route handler.
Important
When using route handlers instead of the frontendApiProxy middleware option, the proxyUrl is not auto-derived. You must set the proxyUrl option on clerkMiddleware() or set the NEXT_PUBLIC_CLERK_PROXY_URL environment variable for the authentication handshake to work correctly.
If you're using Express, you can use the built-in frontendApiProxy option in clerkMiddleware() instead of manually configuring a proxy server. This option handles all proxy requirements automatically, including header forwarding, body streaming, and redirect rewriting. The proxyUrl for authentication handshake is also auto-derived.
In order to enable proxying, you need to set a proxy URL for your Clerk instance's domain. This can be done through the Clerk Dashboard or through the Backend API.
Note
To avoid downtime, your proxy must be set up according to the above configuration before it can be enabled for your instance.
Make sure your proxy forwards requests to the Clerk Frontend API correctly and includes the required headers.
Dashboard
Backend API
In the Clerk Dashboard, navigate to the Domains page.
In the Frontend API section, select the Set proxy configuration option.
Enter your proxy URL. The proxy URL must be a valid URL and resolve correctly.
The request below will update the domain to use the proxy URL https://app.dev/__clerk. In doing so, it will trigger checks to validate the proxy URL.
If you used the frontendApiProxy option in clerkMiddleware() (Next.js or Express), the server-side proxyUrl is auto-derived from the proxy configuration. You only need to set the client-side environment variable (e.g., NEXT_PUBLIC_CLERK_PROXY_URL) so that ClerkJS in the browser routes requests through the proxy.
You will only need to set environment variables in your JavaScript application if you are using a bundler (the NPM module method for ClerkJS installation). If you are using the <script> method, configure your proxy setup using properties in your application instead.