FastAPI Routers & Endpoints
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
The TrueLens backend architecture utilizes FastAPI to provide a high-performance, ML-powered content verification API. The system is structured into a modular routing system that handles diverse verification tasks, including text scans, document analysis, domain trust assessment, and fact-checking. This API serves as the core engine for detecting AI-generated content and fraudulent documents.
The architecture follows a proxy pattern where the Next.js frontend routes requests through internal API routes (e.g., /api/v1/scans) to the FastAPI backend, allowing for seamless communication between the TypeScript-based UI and the Python-based ML services.
Core API Architecture
The application is initialized in app/main.py, which configures the FastAPI instance, sets up CORS middleware to allow requests from the frontend, and includes the versioned API router.
Middleware and Configuration
The backend includes specific configurations for integration and security:
- CORS Middleware: Configured to allow
http://localhost:3000with support for credentials and all methods/headers. - Database Integration:
Base.metadata.create_allis called at startup to ensure the schema is initialized. - Versioning: All ML-related endpoints are prefixed with
/api/v1.
Sources: truelens-backend/app/main.py:10-25
Endpoint Modules
Content Scans
The scans module handles the analysis of URLs and raw text to determine authenticity. The frontend proxies these requests to ensure proper header handling and data mapping between snake_case (Backend) and camelCase (Frontend).
| Method | Path | Description | Data Requirements |
|---|---|---|---|
| POST | /api/v1/scans/ | Create a new content scan | url, text, and content_type |
| GET | /api/v1/scans/ | List all previous scans | None |
Sources: truelens-backend/app/api/v1/api.py:5, truelens-app/src/app/api/v1/scans/route.ts:7-50
Document Verification
The document endpoints handle multi-part form data for file uploads. The backend performs tamper detection, OCR, and cryptographic signing.
Fact-Checking and Live Analysis
TrueLens provides specialized endpoints for verifying claims against known data and live web information.
- Fact-Check: Processes standard queries to verify claims using stored knowledge or heuristics.
- Live Fact-Check: Executes real-time AI fact-checking by querying external sources.
Sources: truelens-app/src/app/api/v1/fact-check/route.ts:8-25, truelens-app/src/app/api/v1/fact-check/live/route.ts:8-25
Data Mapping and Proxying
Because the frontend uses Next.js and the backend uses FastAPI, a proxy layer is implemented in the truelens-app/src/app/api/ directory. This layer is responsible for translating request/response formats.
| Backend Field (Snake Case) | Frontend Field (Camel Case) | Source |
|---|---|---|
trust_score | trustScore | scans/route.ts |
content_type | contentType | scans/route.ts |
scan_duration | scanDuration | scans/route.ts |
file_size | fileSize | documents/route.ts |
verified_at | timestamp | documents/route.ts |
Sources: truelens-app/src/app/api/v1/scans/route.ts:40-47, truelens-app/src/app/api/v1/documents/route.ts:55-65
Technical Summary
The FastAPI routing system in TrueLens serves as the glue between high-level user interfaces and deep ML analysis services. By utilizing APIRouter, the project maintains a clean separation of concerns between text analysis, document forensics, and fact-checking. The use of proxy route handlers in the Next.js layer ensures that the internal API structure remains decoupled from the frontend's specific data requirements.