API Overview
Relevant source files
The following files were used as context for generating this wiki page:
The Inbox Zero project utilizes a monorepo architecture with two primary applications that expose APIs: the main Next.js web application (apps/web) and a dedicated Unsubscribe Automation Service (apps/unsubscriber). The apps/web application provides a comprehensive set of API endpoints to support the user interface, handle webhooks from external services like Google and Lemon Squeezy, and offer a versioned API for third-party integrations. The apps/unsubscriber service is a lightweight Fastify application with a specific API for automating email unsubscriptions.
This document provides an overview of the different APIs, their structure, development patterns, and authentication mechanisms within the Inbox Zero ecosystem.
Sources: ARCHITECTURE.md
Main Web Application API (apps/web)
The core API functionality resides within the apps/web Next.js application. These APIs are built using Next.js API Routes and are located under the apps/web/app/api/ directory.
Sources: ARCHITECTURE.md
API Endpoint Structure
The API is organized into several logical groups based on functionality.
| Endpoint Prefix | Description |
|---|---|
/api/ai/* | Endpoints for AI-related tasks like categorization, summarization, and model interactions. |
/api/auth/* | Authentication endpoints managed by Better Auth. |
/api/google/* | Proxy endpoints for the Gmail API, including webhooks for real-time notifications. |
/api/lemon-squeezy/* | Webhook and integration endpoints for the Lemon Squeezy payment platform. |
/api/resend/* | Integration with the Resend service for sending transactional and summary emails. |
/api/user/* | Endpoints for user-specific data and actions, such as settings, rules, and usage statistics. |
/api/v1/* | Versioned API for external integrations, including an OpenAPI specification. |
/api/reply-tracker/* | Internal endpoints for managing the reply tracking feature. |
/api/watch/* | Cron job endpoints to refresh Google's watch on user inboxes. |
Sources: ARCHITECTURE.md
Development Patterns
The project follows specific patterns for creating backend logic, distinguishing between data fetching (GET requests) and data mutations (writes).
GET API Routes for Data Fetching
For fetching data, the standard approach is to create a GET API route. These routes are protected by middleware to ensure the user is authenticated and has a linked email account.
A key convention is to export the response type from the route file, which allows for type-safe data fetching on the client using hooks like SWR.
This diagram illustrates the flow for a typical GET request.
Sources: apps/web/CLAUDE.md:41-63
Server Actions for Mutations
For data mutations (create, update, delete), the project uses Next.js Server Actions instead of traditional POST/PUT/DELETE API routes. This approach is recommended for all mutation operations. Server Actions are integrated with next-safe-action and use Zod schemas for robust validation.
Sources: apps/web/CLAUDE.md:200-201, apps/web/CLAUDE.md:65-129
Webhooks and Internal APIs
The application exposes several endpoints designed to be called by external services (webhooks) or internal processes.
Google Pub/Sub Webhook
To handle incoming emails in real-time, the application uses Google Pub/Sub. A webhook endpoint at /api/google/webhook is configured to receive notifications from a Pub/Sub topic. This endpoint is secured with a verification token (GOOGLE_PUBSUB_VERIFICATION_TOKEN).
The following diagram shows the real-time email processing flow.
Sources: README.md:225-231, ARCHITECTURE.md:170-177
Internal API Routes
Some API routes are intended for internal use, such as cron jobs or backend processes. These are secured using a secret key (INTERNAL_API_KEY) passed in the headers. The isValidInternalApiKey utility function validates this key.
An example is the POST /api/reply-tracker/process-previous route, which processes previously sent emails for the reply tracker feature.
Sources: apps/web/app/api/reply-tracker/process-previous/route.ts:15-17, apps/web/env.ts:121
Versioned Public API (v1)
For external developers and integrations, Inbox Zero provides a versioned API under the /api/v1/ path. To facilitate its use, an OpenAPI (Swagger) specification is available at the /api/v1/openapi endpoint. This endpoint dynamically generates the OpenAPI JSON schema for the v1 routes.
Sources: ARCHITECTURE.md:165-166, apps/web/app/api/v1/openapi/route.ts
Unsubscribe Automation Service API (apps/unsubscriber)
The Unsubscribe Automation Service is a separate microservice built with Fastify and Playwright. Its purpose is to handle the complex process of unsubscribing from email lists by automating browser interactions.
It exposes a simple REST API with a primary endpoint:
POST /unsubscribe: Triggers the unsubscribe process for a given URL.
This diagram shows the unsubscribe flow.
To use the service, a client sends a POST request with the unsubscribe URL in the body.
curl -X POST http://localhost:5000/unsubscribe \
-H "Content-Type: application/json" \
-d '{
"url": "https://example.com/unsubscribe"
}'
Sources: apps/unsubscriber/README.md, ARCHITECTURE.md:73-86
API Authentication
Different authentication and authorization strategies are used across the APIs depending on the context.
| Method | Description | Used For | Source Files |
|---|---|---|---|
| Middleware | withAuth and withEmailAccount wrappers for Next.js API routes to verify user session and email account linkage. | Client-facing GET API routes in the main web app. | apps/web/CLAUDE.md:43-44 |
| Internal API Key | A shared secret (INTERNAL_API_KEY) passed in request headers to secure communication between internal services. | Cron jobs, internal backend processes. | apps/web/app/api/reply-tracker/process-previous/route.ts:15-17, apps/web/env.ts:121 |
| Webhook Tokens | Unique tokens (GOOGLE_PUBSUB_VERIFICATION_TOKEN) used to verify the authenticity of incoming webhooks. | Receiving webhooks from Google Pub/Sub. | README.md:229-231, apps/web/env.ts:63 |
| User API Keys | A system for generating user-specific API keys, likely using a salt (API_KEY_SALT) for hashing. | External integrations using the versioned /api/v1/ endpoints. | apps/web/env.ts:98 |
Sources: apps/web/CLAUDE.md, apps/web/app/api/reply-tracker/process-previous/route.ts, README.md, apps/web/env.ts