Skip to main content

Clerk Setup

This guide covers how to configure Clerk for AskAtlas. We use separate Clerk applications per environment.

Clerk Applications

EnvironmentClerk App
DevSeparate Clerk app (dev keys)
StageSeparate Clerk app (staging keys)
ProdNot yet provisioned

Each app has its own API keys and webhook configuration.

Creating a Clerk Application

  1. Go to clerk.com/dashboard and sign in
  2. Click Create application
  3. Name it something clear (e.g. AskAtlas Dev, AskAtlas Stage)
  4. Choose your sign-in methods (email, Google, etc.)
  5. Copy the keys from the dashboard

Getting Your Keys

From the Clerk dashboard, navigate to API Keys. You'll need:

KeyWhere It's UsedEnvironment Variable
Publishable KeyFrontend (Next.js)NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY
Secret KeyBackend (Go API)CLERK_SECRET_KEY

Both keys are managed through Infisical. Add them to the appropriate Infisical environment so they're injected at runtime.

Configuring Webhooks

Clerk sends webhook events when users are created, updated, or deleted. Our Go API listens for these events to keep the local database in sync.

Steps

  1. In the Clerk dashboard, navigate to Webhooks
  2. Click Add Endpoint
  3. Set the endpoint URL to your API's webhook route:
    https://<your-api-domain>/webhooks/clerk
    Replace <your-api-domain> with the appropriate environment's API domain.
  4. Subscribe to the following events:
    • user.created
    • user.updated
    • user.deleted
  5. Click Create
  6. Copy the Signing Secret — this is your CLERK_WEBHOOK_SECRET
  7. Add CLERK_WEBHOOK_SECRET to Infisical for the matching environment

Verifying Webhooks Work

After configuring the webhook:

  1. Create a test user via the Clerk dashboard or sign-up flow
  2. Check the Go API logs for webhook receipt
  3. Verify the user appears in your PostgreSQL users table
  4. Check the Clerk dashboard Webhooks → Logs tab for delivery status

Registering the Clerk SDK (Go API)

The Go API registers the Clerk secret key at startup in cmd/api/main.go:

clerkSDK.SetKey(clerkSecretKey)

This must be called before any middleware or handlers that use the Clerk SDK.

Frontend Integration

The Next.js app uses the @clerk/nextjs package. Key files:

FilePurpose
app/layout.tsxWraps the app in <ClerkProvider>
proxy.tsClerk middleware protecting dashboard routes
components/providers.tsxTheme and auth providers

Route Protection

Protected routes are configured in proxy.ts:

const isDashboardRoute = createRouteMatcher([
"/home(.*)",
"/courses(.*)",
"/resources(.*)",
"/study-guides(.*)",
"/me(.*)",
]);

export default clerkMiddleware(async (auth, req) => {
if (isDashboardRoute(req)) {
await auth.protect();
}
});

To protect a new route, add its pattern to the createRouteMatcher array.

Troubleshooting

IssueCauseFix
401 on API callsMissing or invalid JWTCheck CLERK_SECRET_KEY is set correctly in Infisical
Webhook events not receivedWrong endpoint URLVerify the URL in Clerk dashboard matches your API domain
Webhook signature verification failsWrong secretEnsure CLERK_WEBHOOK_SECRET matches the signing secret from Clerk
User created in Clerk but not in DBWebhook not processedCheck API logs and Clerk webhook delivery logs