Skip to main content

Introduction

Budgetron uses oRPC to provide a fully type-safe API layer between the client and server. oRPC enables end-to-end type safety from your database to your UI components, catching errors at compile time rather than runtime.

Architecture

oRPC Foundation

The API architecture is built on three key components:
  1. Router Definition - Centralized router that combines all feature routers
  2. Procedures - Type-safe endpoints with input validation using Zod schemas
  3. Context - Request context including authentication session

API Structure

All API routes are defined in src/server/api/router.ts:

Available Routers

Authentication (auth)

Handles user authentication and session management:
  • signIn - Email/password authentication
  • signUp - User registration
  • signInWithSocial - Social provider authentication (Google)
  • signInWithOAuth - Custom OAuth provider authentication
  • signOut - End user session
  • session - Get current session
  • forgotPassword - Request password reset
  • resetPassword - Reset password with token

Transactions (transactions)

Manage financial transactions:
  • create - Create a single transaction
  • createMany - Bulk create transactions
  • update - Update transaction details
  • delete - Delete a transaction
  • deleteMany - Bulk delete transactions
  • getByDateRange - Fetch transactions within date range
  • getByCategory - Fetch transactions by category
  • parseOFX - Parse and import OFX bank files

Budgets (budgets)

Manage budget tracking:
  • create - Create a new budget
  • update - Update budget details
  • delete - Delete a budget
  • summary - Get all budgets summary
  • details - Get detailed budget information

Bank Accounts (bankAccounts)

Manage user bank accounts:
  • create - Add a new bank account
  • update - Update account details
  • delete - Remove a bank account
  • getAll - List all bank accounts

Categories (categories)

Manage transaction categories:
  • getAll - Get all categories
  • getAllSubCategories - Get all subcategories

Analytics (analytics)

Generate financial reports and insights:
  • getDashboardSummary - Get dashboard overview data
  • getCashFlowReport - Generate cash flow analysis
  • getCategorySpend - Analyze spending by category
  • getCategoryIncome - Analyze income by category

User (user)

Manage user profile and accounts:
  • listAccounts - List connected accounts

AI (ai)

AI-powered features for financial insights.

Making API Calls

Server-Side (React Server Components)

Use the server RPC client to call procedures directly as functions:

Client-Side (React Query)

Use the client RPC with React Query for data fetching:

Client-Side (Mutations)

Use mutations for data modifications:

Type Safety

Input Types

Infer input types from the router:

Output Types

Infer output types from the router:

Request/Response Format

Standard Request

All procedures accept an input object validated by Zod schemas:

Standard Response

Procedures return typed data directly:

Error Response

Errors are thrown as ORPCError with standardized codes:
Common error codes:
  • UNAUTHORIZED - Authentication required or invalid credentials
  • FORBIDDEN - Insufficient permissions
  • BAD_REQUEST - Invalid input data
  • NOT_FOUND - Resource not found
  • INTERNAL_SERVER_ERROR - Server error

Middleware

Authorization Middleware

Protected procedures automatically verify authentication:

Timing Middleware

Logs execution time and adds artificial delay in development:

Batching

The client automatically batches multiple requests into a single HTTP call:
To disable batching for a specific request:

Example: Complete Procedure

Here’s a complete example of a protected procedure:

Next Steps