Clerk logo

Clerk Docs

Ctrl + K
Go to clerk.dev

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

Installation

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.

1

Configure Middleware

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

1

Using Clerk in your TRPC Context

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

1

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

1

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.

1

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.

1

Was this helpful?

Clerk © 2022