TanStack React Start Quickstart - Getting started | Clerk Docs

Create a new TanStack React Start app

If you don't already have a TanStack React Start app, run the following commands to create a new one.

Add clerkMiddleware() to your app

clerkMiddleware()Tanstack Start Icon grants you access to user authentication state throughout your app. It also allows you to protect specific routes from unauthenticated users. To add clerkMiddleware() to your app, follow these steps:

  1. Create a src/start.ts file with the following code:

    src/start.ts
  2. By default, clerkMiddleware() will not protect any routes. All routes are public and you must opt-in to protection for routes. See the clerkMiddleware() referenceTanstack Start Icon to learn how to require authentication for specific routes.

Add <ClerkProvider> 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.

Add the <ClerkProvider> component to your app's root route, as shown in the following example:

src/routes/__root.tsx
src/routes/index.tsx

Server-side

To protect your routes, create a server function that checks the user's authentication state via the auth()Tanstack Start Icon method. If the user is not authenticated, they are redirected to a sign-in page. If authenticated, the user's userId is passed to the route, allowing access to the <Home /> component, which welcomes the user and displays their userId. The beforeLoad() method ensures authentication is checked before loading the page, and the loader() method returns the user data for use in the component.

src/routes/index.tsx

Run your project

Run your project with the following command:

Create your first user

  1. Select "Sign up" on the page and authenticate to create your first user.

  2. To make configuration changes to your Clerk development instance, claim the Clerk keys that were generated for you by selecting Configure your application in the bottom right of your app. This will associate the application with your Clerk account.