API Reference (tRPC)
Relevant source files
The following files were used as context for generating this wiki page:
The Analog API is built using tRPC, a framework for creating typesafe APIs. It serves as the backend for the Analog application, handling data fetching, mutations, and interactions with external services like Google Calendar and Microsoft Graph. The API is structured into multiple routers, each responsible for a specific domain of the application, such as events, calendars, and user accounts.
The API leverages Zod for schema validation, ensuring that all incoming data conforms to expected types. All procedures are protected, meaning they require user authentication. The API also supports generation of an OpenAPI specification, allowing for easier integration and documentation.
Sources: packages/api/src/root.ts, packages/api/src/routers/api-keys.ts, packages/api/src/openapi.ts
Architecture
The core of the API is the appRouter, which merges several domain-specific routers into a single, unified API endpoint. This modular approach keeps the codebase organized and scalable.
Sources: packages/api/src/root.ts:10-16
Router Structure
The appRouter is composed of the following sub-routers:
accounts: Manages user accounts and connections to third-party providers.apiKeys: Handles the lifecycle of API keys for programmatic access.calendars: Manages calendar data from connected accounts.events: Manages calendar events.users: Manages user-specific data.webhooks: Handles incoming webhooks from external services.
Sources: packages/api/src/root.ts:11-16
The following diagram illustrates the hierarchy of the appRouter.
Sources: packages/api/src/root.ts:10-17
Core Concepts
createTRPCRouter: A factory function used to create a new router instance (e.g.,apiKeysRouter).protectedProcedure: A builder for creating tRPC procedures that require authentication. It ensures that a valid user session exists before executing the procedure's logic. The authenticated user's data is available in the context (ctx.user).- Context (
ctx): An object available to all procedures, containing request-specific information like headers (ctx.headers) and the authenticated user (ctx.user).
Sources: packages/api/src/routers/api-keys.ts:6,8, packages/api/src/routers/accounts.ts:10,29
Routers and Procedures
API Keys Router
The apiKeysRouter manages API keys for users. It relies on an external authentication service, accessed via auth.api, to perform CRUD operations. All procedures in this router are protected.
Sources: packages/api/src/routers/api-keys.ts
| Procedure | Type | Description | Input Schema |
|---|---|---|---|
list | query | Lists all API keys for the current user. | N/A |
create | mutation | Creates a new API key. | createApiKeySchema |
update | mutation | Updates the name of an existing API key. | updateApiKeySchema |
delete | mutation | Deletes an API key. | deleteApiKeySchema |
Sources: packages/api/src/routers/api-keys.ts:8-51
The sequence diagram below shows the flow for creating a new API key.
Accounts Router
The accountsRouter handles connected third-party accounts (e.g., Google, Microsoft). It interacts with both the authentication service and the database (ctx.db).
Sources: packages/api/src/routers/accounts.ts
| Procedure | Type | Description | Input Schema |
|---|---|---|---|
get | query | Retrieves a single account by its ID. | getAccountSchema |
list | query | Lists all accounts connected by the current user. | N/A |
delete | mutation | Deletes a connected account and all associated data (calendars, events). | deleteAccountSchema |
Sources: packages/api/src/routers/accounts.ts:10-41
Deleting an account involves multiple steps, including revoking tokens via the auth service and cleaning up related data in the local database.
Calendars Router
This router is responsible for managing calendars from connected provider accounts. It uses a provider pattern to abstract the logic for different services like Google and Microsoft.
Sources: packages/api/src/routers/calendars.ts
A getProvider helper function is used to instantiate the correct client (e.g., GoogleCalendar) based on the account's provider type.
| Procedure | Type | Description | Input Schema |
|---|---|---|---|
list | query | Lists calendars for a given account. | listCalendarsSchema |
create | mutation | Creates a new calendar in a given account. | createCalendarSchema |
update | mutation | Updates an existing calendar. | updateCalendarSchema |
delete | mutation | Deletes a calendar. | deleteCalendarSchema |
Sources: packages/api/src/routers/calendars.ts:30-101
Events Router
Similar to the calendars router, the eventsRouter manages events within a specific calendar and abstracts provider-specific logic.
Sources: packages/api/src/routers/events.ts
| Procedure | Type | Description | Input Schema |
|---|---|---|---|
list | query | Lists events in a given calendar. | listEventsSchema |
create | mutation | Creates a new event in a given calendar. | createEventSchema |
update | mutation | Updates an existing event. | updateEventSchema |
delete | mutation | Deletes an event. | deleteEventSchema |
Sources: packages/api/src/routers/events.ts:33-146
Schemas (Zod)
Input validation is handled using Zod schemas. These schemas define the expected shape and types of data for each API procedure.
Sources: packages/api/src/schemas/index.ts, packages/api/src/routers/events.ts:25-31
Example: createEventSchema
This schema defines the complex structure required to create a new calendar event. It includes details like summary, description, start/end times, attendees, and recurrence rules.
// A simplified representation of the schema
export const createEventSchema = z.object({
accountId: z.string(),
calendarId: z.string(),
summary: z.string().optional(),
description: z.string().optional(),
start: z.object({
dateTime: z.string(), // PlainDate, PlainDateTime, or ZonedDateTime
timeZone: z.string(),
}),
end: z.object({
dateTime: z.string(), // PlainDate, PlainDateTime, or ZonedDateTime
timeZone: z.string(),
}),
attendees: z.array(z.object({ email: z.string() })).optional(),
recurrence: z.array(z.string()).optional(), // RRULE strings
// ... and other fields
});
Sources: packages/api/src/schemas/index.ts:100-141
OpenAPI Generation
The Analog API can generate an OpenAPI v3 specification from the tRPC router definition. This is useful for documentation, generating client SDKs, or integrating with other tools that support the OpenAPI standard.
The generation is configured in packages/api/src/openapi.ts.
Sources: packages/api/src/openapi.ts
Configuration
The generateOpenApiDocument function is used with the following configuration:
| Option | Value |
|---|---|
title | "Analog API" |
version | "1.0.0" |
baseUrl | http://localhost:3000 (dev) or Vercel URL (prod) |
securitySchemes | apiKey in header x-api-key |
Sources: packages/api/src/openapi.ts:7-18
This setup indicates that the API can be authenticated using an API key passed in the x-api-key header.
Sources: packages/api/src/openapi.ts:13-17