Overview
Reportr uses Next.js 14 App Router API routes for a RESTful backend. All API endpoints follow consistent patterns for authentication, validation, error handling, and response formatting. Base Path:/apiLocation:
src/app/api/Pattern: File-based routing with route handlers
Directory Structure
Route Patterns
Resource Routes
RESTful resource-based routing:Dynamic Routes
Next.js file-based dynamic segments:Authentication Pattern
Protected Routes
All API routes (except auth endpoints) require authentication:Auth Helper
File:src/lib/auth-helpers.ts
Validation Pattern
Zod Schema Validation
All POST/PATCH requests validate input with Zod:Common Validation Schemas
File:src/lib/validations.ts
Error Handling
Standard Error Response
Error Codes
Key API Endpoints
Clients API
GET /api/clients
List all clients for authenticated user. Response:POST /api/clients
Create new client. Request:- Email verification check
- Plan limit validation
- Trial expiry check
PATCH /api/clients/[id]
Update client details. Request:DELETE /api/clients/[id]
Delete client and cascade delete all reports.Reports API
GET /api/reports
List all reports for user, optionally filtered by client. Query Parameters:clientId(optional) - Filter by client ID
POST /api/reports
Generate new report. Request:- Email verification required
- Plan limit enforcement
- Upgrade prompts when nearing limit
- Client ownership validation
User API
GET /api/user/profile
Get authenticated user profile. Response:PATCH /api/user/profile
Update user profile and branding. Request:Payments API
POST /api/payments/create-subscription
Create PayPal subscription. Request:POST /api/payments/webhook
Handle PayPal webhook events. Events:BILLING.SUBSCRIPTION.ACTIVATEDBILLING.SUBSCRIPTION.CANCELLEDBILLING.SUBSCRIPTION.SUSPENDEDPAYMENT.SALE.COMPLETED
Google API Integration
GET /api/google/search-console/sites
List user’s GSC properties. Response:GET /api/clients/[id]/google/search-console
Fetch GSC data for client. Query Parameters:startDate- ISO date stringendDate- ISO date string
Rate Limiting & Usage Tracking
API Usage Tracking
All API calls logged toApiUsage model:
Plan Limits
File:src/lib/plan-limits.ts
Cron Jobs
Email Sequences
Endpoint:/api/cron/process-email-sequences
Processes automated email campaigns:
- Welcome email on signup
- Day 1 onboarding
- Day 3 trial reminder
- Trial expiry warning
Subscription Cleanup
Endpoint:/api/cron/process-cancellations
Downgrades users when subscription ends:
- Check
subscriptionEndDate - Downgrade to FREE plan
- Update
subscriptionStatus
Best Practices
- Always authenticate - Use
requireUser()for protected routes - Validate ownership - Check
userIdmatches resource owner - Use Zod schemas - Validate all request bodies
- Handle errors gracefully - Return appropriate HTTP status codes
- Log important events - Use
console.logfor debugging - Track API usage - Insert into
ApiUsagefor billing - Force dynamic - Add
export const dynamic = 'force-dynamic'for routes using auth - Return proper types - Use
NextResponse.json()with TypeScript
Testing API Endpoints
Using curl
Using Thunder Client / Postman
- Sign in via browser
- Copy
next-auth.session-tokencookie - Add to request headers: