Creating tRPC Context
Understand how to create and populate the tRPC context object with request-specific data like user info.
What is tRPC Context?
Welcome! In tRPC, the context is a special object that's created for each incoming request to your server.
Think of it as a personalized backpack for every request. You can fill this backpack with any data that needs to be accessible by your tRPC procedures.
Why Use tRPC Context?
The tRPC context is incredibly useful for sharing request-specific data across all your API procedures. This prevents you from passing the same arguments repeatedly.
- Authentication: Store authenticated user details.
- Database Connections: Provide a database client for the current request.
- Request-specific Data: Access headers, IP addresses, or other request info.
- Logging: Attach a unique request ID for tracing.