Introduction
The Zarna backend is a modern FastAPI application providing a comprehensive REST API for CRM operations, document processing, AI-powered analysis, and external integrations. The backend powers all Zarna features, including the Zarna AI chatbot.Technology Stack
- FastAPI - Modern, high-performance web framework
- Python 3.8+ - Language runtime
- Uvicorn - ASGI server with auto-reload
- Pydantic - Data validation using Python type hints
- Supabase - PostgreSQL database client
- Anthropic Claude - AI-powered analysis
- Docling - Advanced document processing
- AutoGen - Multi-agent AI orchestration
Full Tech Stack
See the complete list of backend technologies
Architecture
Main Application
Location:api/app/main.py
The application entry point configures FastAPI with:
- OpenAPI Documentation: Auto-generated at
/docsand/redoc - CORS Middleware: Configured for
localhost:3000,localhost:3001,localhost:8000 - JWT Authentication: Middleware for protected routes
- Exception Handling: Custom handlers for clean error responses
- Startup/Shutdown Events: Agent pool initialization and cleanup
Project Structure
API Routers
The backend has 25+ routers organized by domain:CRM Routers
Companies
Company CRUD operations, search, filtering
Contacts
Contact management and relationships
Deals
Deal pipeline, stages, workflows
Interactions
Meetings, calls, notes tracking
Financials
Financial records and metrics
Notes
Notes management and search
File Management Routers
Files
Upload, process, extract documents
Google Drive
Drive sync and file operations
SharePoint
SharePoint integration
Egnyte
Egnyte file management
Basecamp
Basecamp project integration
Communication Routers
Emails
Email tracking and history
Email Bot
Automated email handling
Calendar
Google Calendar integration
AI & Analytics Routers
Reports
AI-powered report generation
Sourcing
AI company sourcing with Exa
Agentic Chat
Multi-agent AI chat system
Management Routers
Users
User management operations
Firms
Firm management
Prompts
AI prompt operations
Staffing
Team staffing management
Utilities
Utility endpoints
Chat History
Chat conversation history
Core Services
The backend includes several major services in thescripts/ directory:
CRM Agent
File:scripts/crm_agent.py
Size: 51KB
AI-powered CRM operations that understand natural language queries.
Learn More
Explore the CRM agent service
Document Processing
Files: Multiple services for document extractionenhanced_extraction_service.py- Main extraction servicebatched_cim_service.py- Batch CIM processingcim_analysis_service.py- CIM analysis
Learn More
Understand document processing
Email Agent
Directory:scripts/email_agent_microservice/
Automated email handling with Gmail/Outlook integration.
Learn More
Explore the email agent
Report Generation
Files: Report generation services with streaming AI-powered report generation with real-time streaming to frontend.Learn More
See report generation service
Agentic Chat
Directory:scripts/agentic_chat/
Files: Agent pool, orchestrator, tools
Multi-agent system with pool manager for performance.
Learn More
Dive into the agentic system
Authentication & Security
JWT Authentication
Middleware:api/app/middleware/JWTAuthMiddleware
All routes are protected by JWT authentication:
Environment Variables
Required variables in.env:
Authentication Guide
Learn about JWT authentication
Database Integration
Supabase Client
The backend uses Supabase for PostgreSQL database access:Row Level Security
All tables have RLS policies for firm-level data isolation.Database Guide
Explore the database architecture
Development Workflow
Starting the Server
API Documentation
Once running, access interactive API docs:- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
Hot Reload
Development server automatically reloads on file changes.Health Check
API Patterns
Request/Response Models
All endpoints use Pydantic models for validation:Error Handling
Consistent error responses:Async/Await
All endpoints use async for non-blocking I/O:Performance
Agent Pool System
Eliminates 5-11s cold start latency for AI agents.Agent Pool Guide
Learn about performance optimization
Background Tasks
Long-running operations use background tasks:Streaming Responses
Reports and chat use Server-Sent Events (SSE) for real-time streaming:Deployment
Production Server
File:api/run_prod.py
Uses Gunicorn with Uvicorn workers:
Environment Configuration
- Development:
run.pywith reload - Production:
run_prod.pywithout reload - Dockerized deployment ready
Next Steps
Authentication
Learn about JWT authentication
Routers
Explore all API routers
Services
Understand core services
Database
Database architecture
