Using production keys in local development
In rare cases, you may need to debug issues that only occur in your production environment. This guide explains how to configure your local development environment to use production Clerk keys.
Prerequisites
- Access to your production Clerk API keys
- A production domain already configured in your Clerk Dashboard
- The ability to run your local server with HTTPS on port 443
Why this works
Clerk enforces origin validation in production to protect your users. Production keys (pk_live_ and sk_live_) only work with your configured production domain. This means localhost won't work with production keys. You'll see an error like:
Clerk: Production Keys are only allowed for domain "your-domain.com".However, authentication across subdomains works by default in Clerk production instances. By mapping a subdomain of your production domain to your local machine, you can use production keys locally.
Map a subdomain to localhost
Configure your machine to resolve a subdomain of your production domain to 127.0.0.1 by editing your hosts file.
Replace your-domain.com with your actual production domain. Changes take effect immediately, no restart required.
Run the following command in Terminal:
echo "127.0.0.1 local.your-domain.com" | sudo tee -a /etc/hostsEnter your password if prompted. The line will be added to your hosts file.
- Search for Notepad in the Start menu.
- Right-click and select Run as administrator.
- In Notepad, select File > Open and navigate to
C:\Windows\System32\drivers\etc\hosts. You may need to change the file filter from "Text Documents" to "All Files" to see it. - Add the following line at the end of the file:
hosts 127.0.0.1 local.your-domain.com - Save the file.
- Open the hosts file in your preferred text editor with elevated privileges:
terminal sudo nano /etc/hosts - Add the following line at the end of the file:
/etc /hosts 127.0.0.1 local.your-domain.com - Save and exit (in nano, press
Ctrl+Oto save, thenCtrl+Xto exit).
Configure environment variables
Update your .env file to use your production Clerk keys. Production keys start with pk_live_ and sk_live_ instead of pk_test_ and sk_test_.
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_live_xxxxx
CLERK_SECRET_KEY=sk_live_xxxxxReplace pk_live_xxxxx and sk_live_xxxxx with your actual production keys from the API keys page in the Clerk Dashboard.
Run your server with HTTPS on port 443
Production Clerk requires HTTPS on port 443. Running on port 443 requires elevated privileges on most systems.
Next.js 13.5+ can automatically generate a locally-trusted certificate. You'll also need to configure Next.js to allow requests from your subdomain.
Add the following to your Next.js configuration:
const nextConfig = {
allowedDevOrigins: ['local.your-domain.com'],
// Other options...
}
export default nextConfigThen start your server on port 443:
sudo npx next dev --experimental-https -p 443Use mkcert to create locally-trusted certificates that match your subdomain:
# Install mkcert (macOS)
brew install mkcert
# Create a local CA and install it in the system trust store
mkcert -install
# Generate certificates for your subdomain
mkcert local.your-domain.comThis creates local.your-domain.com.pem and local.your-domain.com-key.pem files. Configure your development server to use these certificates and run on port 443. The exact configuration depends on your framework.
Test the configuration
- Open your browser and navigate to your local subdomain:
https://local.your-domain.com - If you see a certificate warning, select Advanced and proceed to the site.
- Verify that Clerk loads and you can sign in.
If you navigate to https://localhost instead, you should see the origin validation error, which confirms that production keys are correctly restricted.
Clean up
After debugging:
- Revert your
.envfile to use development keys (pk_test_xxxxxandsk_test_xxxxx). - Remove the
allowedDevOriginsconfiguration fromnext.config.js(if applicable). - Remove the entry from your hosts file:
Run the following command in Terminal, replacing your-domain.com with your actual production domain:
sudo sed -i '' '/local\.your-domain\.com/d' /etc/hostsEnter your password if prompted. The line will be removed from your hosts file.
- Search for Notepad in the Start menu. 1. Right-click and select Run as administrator.
- In Notepad, select File > Open and navigate to
C:\Windows\System32\drivers\etc\hosts. - Delete the line containing
local.your-domain.com. 1. Save the file.
Run the following command, replacing your-domain.com with your actual production domain:
sudo sed -i '/local\.your-domain\.com/d' /etc/hostsFeedback
Last updated on
Edit on GitHub