tRPC API
This guide explains how to use the tRPC integration for type-safe API calls in the application, including available routes, custom hooks, and best practices.
This template uses tRPC with react-query under the hood. For more information, go to tRPC docs
Overview
tRPC enables end-to-end typesafe APIs, ensuring that your frontend and backend are always in sync without schema validation or code generation. Our application implements a structured tRPC router system with dedicated hooks for common operations.
Key Benefits
- 1Full type safety between client and server
- 2Automatic API documentation through TypeScript
- 3Simplified error handling
- 4Integrated with React Query for caching and state management
- 5Zero configuration for most use cases
Available Routes
The application organizes tRPC routes into logical groups by feature. Below are the available routers defined in _app.ts
:
trpc.users.*
User management operations
trpc.items.*
CRUD operations for items
trpc.notifications.*
User notification management
trpc.workspaces.*
Workspace operations
trpc.members.*
Workspace member management
trpc.invitations.*
Invitation creation and handling
trpc.usersSettings.*
User settings management
trpc.subscriptions.*
Subscription management
trpc.feedbacks.*
User feedback submission
Router Configuration
Custom Hooks
Each router has dedicated hooks organized in files like items-hooks.ts
or users-hooks.ts
. These hooks encapsulate common functionality and provide built-in error handling, toast notifications, and cache invalidation.
Item Hooks Example
Custom hooks for item operations defined in items-hooks.ts:
Using tRPC Hooks
Using Mutation Hooks
Example of using a tRPC mutation hook in a component:
Using Query Hooks
Example of fetching data with tRPC queries:
Type Safety Benefits
Automatic Type Inference
Types flow from your backend to frontend without any manual work.
Input Validation
Input validation happens at compile time and runtime.
Error Handling
Errors are properly typed and can be handled gracefully.
Hook Patterns
Common Hook Structure
- 1Import dependencies - Import tRPC client and other utilities
- 2Access tRPC utilities - Get access to the tRPC client
- 3Setup mutation or query - Configure the tRPC operation
- 4Handle success/error cases - Manage toast messages and state updates
- 5Return necessary values - Expose mutate function, loading state, etc.
Cache Invalidation
Best Practices
Hook Organization
- 1Group related hooks in feature-specific files
- 2Keep hooks focused on single responsibilities
- 3Provide optional callback props for flexibility
- 4Handle errors consistently across hooks
Type Safety
- 1Leverage TypeScript's inference capabilities
- 2Define input and output types explicitly for complex operations
- 3Avoid using 'any' or type assertions
- 4Use zod schemas for validation
Performance
- 1Use proper cache invalidation strategies
- 2Implement optimistic updates for better UX
- 3Use suspense mode when appropriate
- 4Enable prefetching for frequently accessed data
Error Handling
- 1Provide descriptive error messages
- 2Use toast notifications for user feedback
- 3Handle different error types appropriately
- 4Implement proper fallbacks and loading states
Key Features
End-to-End Type Safety
Full TypeScript integration from backend to frontend
React Query Integration
Built-in caching, refetching, and state management
Error Handling
Standardized error handling with custom error types
Developer Experience
Autocomplete and type checking in your IDE
Client Configuration
tRPC Client Setup
When adding new tRPC routes, make sure to follow the established patterns and create corresponding hook files to maintain consistency across the application. Learn more about tRPC