Clerk logo

Clerk Docs

Ctrl + K
Go to

Using TRPC

Learn how to integrate Clerk into your Next.js using TRPC.

TRPC can be used with Clerk but requires a few tweaks from a traditional Clerk + Next.js setup.

Prefer to get started quickly? Check out the following repo which implements the essentials


Install @clerk/nextjs

Set Environment Keys

Three environment variables for Clerk's SDK need to be set. Below is an example of your .env.local file. To get the respective keys go to the API Keys page in the Clerk dashboard.


Configure Middleware

Middleware needs to be placed in the root of your project; for TRPC projects, this is usually the src directory.


Using Clerk in your TRPC Context

To use Clerk in your TRPC context, you will need to use our server helper getAuth


Accessing the context data in your application

Once you have context in your application you can access the data in any procedure by using ctx in your queries


Using TRPC Middleware

In most cases you need to protect your routes from users who aren't signed in or a specific user type. You can create a protected procedure.


Once you have created your procedure, you can use it in any router. The example below uses the protected procedure and returns the user's name.


Was this helpful?

Clerk © 2022