Next.js Quickstart (Pages Router)
Before you start
Example repository
Create a new Next.js app
If you don't already have a Next.js app (Pages Router), run the following commands to create a new one.
npm create next-app@latest clerk-nextjs-pages -- --no-app
cd clerk-nextjs-pages
npm installpnpm create next-app clerk-nextjs-pages --no-app
cd clerk-nextjs-pages
pnpm installyarn create next-app clerk-nextjs-pages --no-app
cd clerk-nextjs-pages
yarn installbunx create-next-app clerk-nextjs-pages --no-app
cd clerk-nextjs-pages
bun installInstall @clerk/nextjs
The Clerk Next.js SDK
Run the following command to install the SDK:
npm install @clerk/nextjspnpm add @clerk/nextjsyarn add @clerk/nextjsbun add @clerk/nextjsAdd the following keys to your .env file. These keys can always be retrieved from the API keys page in the Clerk Dashboard.
- In the Clerk Dashboard, navigate to the API keys page.
- In the Quick Copy section, copy your Clerk and .
- Paste your keys into your
.envfile.
The final result should resemble the following:
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=YOUR_PUBLISHABLE_KEY
CLERK_SECRET_KEY=YOUR_SECRET_KEYAdd clerkMiddleware() to your app
clerkMiddleware()clerkMiddleware() to your app, follow these steps:
-
Create a
proxy.tsfile.- If you're using the
/srcdirectory, createproxy.tsin the/srcdirectory. - If you're not using the
/srcdirectory, createproxy.tsin the root directory alongside.env.
- If you're using the
-
In your
proxy.tsfile, export theclerkMiddleware()helper:proxy.ts import { clerkMiddleware } from '@clerk/nextjs/server' export default clerkMiddleware() export const config = { 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 '/(api|trpc)(.*)', // Always run for Clerk-specific frontend API routes '/__clerk/(.*)', ], } -
By default,
clerkMiddleware()will not protect any routes. All routes are public and you must opt-in to protection. To require a signed-in user, protect resources close to where they're used, as shown in the Pages Router examples in the guide on reading user data.
Add <ClerkProvider> and Clerk components to your app
The <ClerkProvider> component provides session and user context to Clerk's hooks and components. It's recommended to wrap your entire app at the entry point with <ClerkProvider> to make authentication globally accessible. See the reference docs for other configuration options.
Copy and paste the following code into your app. This:
- Adds the
<ClerkProvider>to your app, providing Clerk's authentication context to your app. - Creates a header with Clerk's prebuilt components to allow users to sign in and out, and display different content for signed-in and signed-out users.
import '@/styles/globals.css'
import { ClerkProvider, SignInButton, SignUpButton, Show, UserButton } from '@clerk/nextjs'
import type { AppProps } from 'next/app'
function MyApp({ Component, pageProps }: AppProps) {
return (
<ClerkProvider
{...pageProps}
appearance={{
cssLayerName: 'clerk',
}}
>
<header className="flex justify-end items-center p-4 gap-4 h-16">
<Show when="signed-out">
<SignInButton />
<SignUpButton>
<button className="bg-[#6c47ff] text-white rounded-full font-medium text-sm sm:text-base h-10 sm:h-12 px-4 sm:px-5 cursor-pointer">
Sign Up
</button>
</SignUpButton>
</Show>
<Show when="signed-in">
<UserButton />
</Show>
</header>
<Component {...pageProps} />
</ClerkProvider>
)
}
export default MyAppThis example uses the following components:
- <Show when="signed-in">: Children of this component can only be seen while signed in.
- <Show when="signed-out">: Children of this component can only be seen while signed out.
- <UserButton />: Shows the signed-in user's avatar. Selecting it opens a dropdown menu with account management options.
- <SignInButton />: An unstyled component that links to the sign-in page. In this example, since no props or environment variables are set for the sign-in URL, this component links to the Account Portal sign-in page.
- <SignUpButton />: An unstyled component that links to the sign-up page. In this example, since no props or environment variables are set for the sign-up URL, this component links to the Account Portal sign-up page.
Update globals.css
In the previous step, the cssLayerName property is set on the <ClerkProvider> component. Now, you need to add the following line to the top of your globals.css file in order to include the layer you named in the cssLayerName property. The example names the layer clerk but you can name it anything you want. These steps are necessary for v4 of Tailwind as it ensures that Tailwind's utility styles are applied after Clerk's styles.
@layer theme, base, clerk, components, utilities;
@import 'tailwindcss';Run your project
Run your project with the following command:
npm run devpnpm run devyarn devbun run devCreate your first user
- Visit your app's homepage at http://localhost:3000.
- Select "Sign up" on the page and authenticate to create your first user.
Next steps
Explore the most relevant next steps for your SDK using the following guides.
Prebuilt components
Learn how to add Clerk's prebuilt authentication and user-management UI to your app.
Build custom flows
Learn how to build custom user interfaces entirely from scratch using the Clerk API.
Read user data
Learn how to use Clerk's helpers to read user data in your app.
Customization & localization
Learn how to customize and localize Clerk components.
More to explore
Explore additional Clerk features that help you build, manage, and grow your application.
- Organizations - Organizations are shared accounts that let teams collaborate, manage members and roles, and control access to shared resources.
- Billing - Billing enables you to manage subscriptions, free trials, payments, plans, and billing-related webhook events for B2C and B2B applications.
- Waitlist - Waitlist lets you collect signups and control access to new products or features before launch through a simple, integrated workflow.
Feedback
Last updated on