Overview
The Clerk system uses FastAPI to expose a simple, focused web API with two main endpoints that handle all chatbot interactions.API Architecture
The FastAPI application follows a unified architecture where all requests are processed through a centralMainHandler:
- Unified Processing - Both endpoints route to the same
MainHandlerclass - Session Builder - Uses
SessionBuilderto create tools and context for each request - Dependency Injection - FastAPI’s dependency system provides all required services
- Consistent Response Format - Both endpoints return similar response structures
Core Endpoints
Message Processing
Event Processing
Processing Flow
Both endpoints follow the same processing pattern:- Initialize MainHandler with required dependencies (Nexus client, session builder, settings, etc.)
- Process through MainHandler - Calls
handler.process_event()with the input payload - Return unified response - Success status, response content, customer message, and debug information
Key Features
Error Handling
- Standardized HTTP exceptions with appropriate status codes
- Comprehensive error logging with conversation context
- Graceful degradation for processing failures
Observability
- Structured logging with conversation IDs
- Request tracing through the processing pipeline
- Debug information in response payloads
Development Support
- Auto-generated OpenAPI documentation
- Interactive Swagger UI at
/docs - Type-safe request/response models using Pydantic
- Development routes for health monitoring and maintenance
Response Models
MessageResponse
| Field | Type | Description |
|---|---|---|
success | boolean | Whether message processing succeeded |
response | string | Internal response for Nexus (usually <nexus_response>OK</nexus_response>) |
customer_response | string | Message to send to the customer via WhatsApp |
thinking | string | Internal reasoning and context for debugging |
scratchpad | string | Additional processing notes and conversation context |
EventResponse
| Field | Type | Description |
|---|---|---|
status | "processed" | "failed" | Processing status of the event |
event_type | string | The type of event that was processed |
response | string | Internal response for Nexus (usually <nexus_response>OK</nexus_response>) |
customer_response | string | Message to send to the customer via WhatsApp |
thinking | string | Internal reasoning about the event |
scratchpad | string | Event processing details and context |