API Contracts - Server (tRPC)
API Contracts - Server (tRPC)
Section titled “API Contracts - Server (tRPC)”Part: server Framework: Hono + tRPC 11 Generated: 2026-01-04
Overview
Section titled “Overview”The server exposes a type-safe tRPC API consumed by the client and subscription apps. All procedures require authentication via Better Auth or WorkOS session.
Router Structure
Section titled “Router Structure”appRouter├── getAccounts # Query: Get user's WhatsApp accounts├── account # Account management router│ ├── addAccount # Mutation: Create new WhatsApp account│ └── deleteAccount # Mutation: Delete account (cascades)├── chat # Chat operations router│ ├── getTags # Query: Get tags for accounts│ ├── getTag # Query: Get single tag│ └── setTag # Mutation: Create/update tag├── folders # Folder management router│ ├── getFolders # Query: Get folders for accounts│ ├── createFolder # Mutation: Create folder│ ├── updateFolder # Mutation: Update folder│ ├── deleteFolder # Mutation: Delete folder│ ├── createChatFolder # Mutation: Assign chat to folder│ └── deleteChatFolder # Mutation: Remove chat from folder├── priorities # Priority management router├── subscriptions # Subscription management router│ ├── getUserSubscription # Query: Get current subscription│ ├── verifyPayment # Mutation: Verify Creem payment│ ├── cancelSubscription # Mutation: Cancel subscription│ ├── getBillingHistory # Query: Get billing history│ └── devGrantSubscription # Mutation: Dev-only subscription grant└── templates # Template management router ├── getTemplates # Query: Get templates for account ├── getTemplate # Query: Get single template ├── createTemplate # Mutation: Create template (limit enforced) ├── updateTemplate # Mutation: Update template └── deleteTemplate # Mutation: Delete templateEndpoint Details
Section titled “Endpoint Details”Root Router
Section titled “Root Router”getAccounts
Section titled “getAccounts”- Type: Query
- Auth: Required (session.user.id)
- Returns: Array of account objects with computed
accountNumber
Account Router (account.*)
Section titled “Account Router (account.*)”account.addAccount
Section titled “account.addAccount”- Type: Mutation
- Auth: Required + subscription limit check
- Input:
{id?: string // Optional UUID for coordinationname: stringphoneNumber: stringavatar?: stringcolor: stringstatus?: 'active' | 'away' | 'offline'isBusinessAccount: booleanisVerified: booleanaccountNumber: number}
- Errors:
FORBIDDEN(account limit),UNAUTHORIZED
account.deleteAccount
Section titled “account.deleteAccount”- Type: Mutation
- Auth: Required + ownership check
- Input:
{ id: string } - Cascade Deletes: chat_tags, folders, templates, chat_priorities, chat_folders
Chat Router (chat.*)
Section titled “Chat Router (chat.*)”chat.getTags
Section titled “chat.getTags”- Type: Query
- Input:
{ accountIds: string[] }
chat.setTag
Section titled “chat.setTag”- Type: Mutation
- Input:
{ chatId: string, accountId: string, tag: string } - Behavior: Upsert (update existing or create new)
Folders Router (folders.*)
Section titled “Folders Router (folders.*)”folders.getFolders
Section titled “folders.getFolders”- Type: Query
- Input:
{ accountIds: string[] } - Returns: Folders with nested
chatsandaccount.accountNumber
folders.createFolder
Section titled “folders.createFolder”- Type: Mutation
- Input:
{ name: string, color: string, accountId: string }
Subscriptions Router (subscriptions.*)
Section titled “Subscriptions Router (subscriptions.*)”subscriptions.verifyPayment
Section titled “subscriptions.verifyPayment”- Type: Mutation
- Auth: Required
- Input:
{request_id: string | nullcheckout_id: stringorder_id?: stringcustomer_id?: stringsubscription_id?: stringproduct_id?: stringsignature: stringplanType: 'pro_monthly' | 'pro_yearly'}
- Behavior:
- Check for duplicate checkout_id
- Verify Creem signature (SHA256)
- Create subscription record
- Idempotent: Returns existing subscription if already processed
subscriptions.cancelSubscription
Section titled “subscriptions.cancelSubscription”- Type: Mutation
- Behavior:
- Find active subscription for user
- Cancel with Creem API
- Update local status to ‘canceled’
Templates Router (templates.*)
Section titled “Templates Router (templates.*)”templates.createTemplate
Section titled “templates.createTemplate”- Type: Mutation
- Input:
{ name: string, content: string, accountId: string } - Limits: Free accounts limited to
FREE_TEMPLATE_LIMITtemplates per account - Middleware:
subscriptionLimitscheckscanCreateTemplate
Authentication Context
Section titled “Authentication Context”interface TRPCContext { session: { user?: { id: string } } | null user: { id: string subscription: Subscription | null } | null}Middleware
Section titled “Middleware”subscriptionLimits
Section titled “subscriptionLimits”Injected on account and template mutations. Provides:
ctx.canAddAccount: booleanctx.canCreateTemplate: Record<accountId, boolean>
Error Codes
Section titled “Error Codes”| Code | Meaning |
|---|---|
UNAUTHORIZED | No valid session |
FORBIDDEN | Subscription limit reached |
NOT_FOUND | Resource doesn’t exist or no permission |
BAD_REQUEST | Invalid signature or payment verification failed |
CONFLICT | Database constraint violation |
INTERNAL_SERVER_ERROR | Unexpected error |