POST /api/queue/batch
Submit a batch of email generation requests to the queue. All items will be processed sequentially to avoid API rate limits. Maximum 100 items per batch.Queue items are processed with
concurrency=1 in Celery to prevent rate limiting from external APIs. Frontend should poll GET /api/queue/ every 2 seconds for status updates.Authentication
Requires valid JWT token from Supabase Auth in theAuthorization header.
Request Body
Array of batch items to queue. Must contain 1-100 items. Each item has:
recipient_name(string, 2-255 chars): Full name of recipientrecipient_interest(string, 2-500 chars): Research area for personalization
Email template to use for all items in the batch. Must be 10-5000 characters with placeholders like
{{name}} and {{research}}.Request Example
Response
Array of UUID strings for the created queue items. Use these IDs to track individual items via GET
/api/queue/.Success confirmation message with item count
Response Example
Status Code
201 Created - Queue items created successfully and Celery tasks dispatchedProcessing Flow
- Database Insert: All queue items created with status
PENDING - Task Dispatch: Celery tasks dispatched for each item
- Sequential Processing: Celery worker processes items one at a time (FIFO order)
- Status Updates: Each item transitions:
PENDING→PROCESSING→COMPLETEDorFAILED
Error Responses
400 Bad Request
Invalid batch size or field validation errors
401 Unauthorized
Invalid or missing JWT token
422 Validation Error
Invalid request structure or field constraints violated
GET /api/queue/
Get all queue items from the last 24 hours for the current user, including their status and position in the queue.Poll this endpoint every 2 seconds while queue items are active. Position calculation uses efficient SQL window functions to avoid N+1 query problems.
Authentication
Requires valid JWT token from Supabase Auth in theAuthorization header.
Request Example
Response
Returns an array of queue item objects sorted by creation time (oldest first, FIFO order).Queue item’s unique identifier
Name of the email recipient
Current status:
pending, processing, completed, or failedPosition in queue (1-indexed). Only present for
pending items. null for processing/completed/failed items.ID of the generated email. Only present when status is
completed.Error details if status is
failed. Truncated to 1000 characters.Current pipeline step being executed. Only present when status is
processing.Timestamp when the item was queued
Response Example
Queue Status Values
pending
Item is waiting in queue. Check
position field for queue position.processing
Item is currently being processed. Check
current_step for pipeline progress.completed
Email generation completed successfully. Use
email_id to retrieve the email via GET /api/email/{email_id}.failed
Email generation failed. Check
error_message for details.Implementation Notes
- 24-Hour Window: Only items from the last 24 hours are returned to avoid returning stale data
- FIFO Ordering: Items are processed in order of
created_attimestamp (oldest first) - Position Calculation: Uses SQL window function
ROW_NUMBER()for efficient position calculation without N+1 queries - Polling Strategy: Frontend polls every 2 seconds while
pendingorprocessingitems exist
Error Responses
401 Unauthorized
Invalid or missing JWT token
DELETE /api/queue/
Cancel a pending queue item. Only items with statuspending can be cancelled. Processing, completed, or failed items cannot be cancelled.
Authentication
Requires valid JWT token from Supabase Auth in theAuthorization header.
Path Parameters
The unique identifier of the queue item to cancel
Request Example
Response
Confirmation message that the queue item was cancelled
Response Example
Cancellation Flow
- Validation: Checks that item exists, belongs to user, and has status
pending - Celery Revoke: Revokes the Celery task (graceful, terminate=false)
- Database Delete: Removes the queue item from the database
- Response: Returns confirmation message
Error Responses
400 Bad Request (Invalid UUID)**
Invalid queue item ID format
400 Bad Request (Cannot Cancel)
Item status is not
pending (already processing, completed, or failed)401 Unauthorized
Invalid or missing JWT token
404 Not Found
Queue item not found or user doesn’t have permission to access it
Queue Implementation Details
Database Schema
Queue items are stored in thequeue_items table with the following key fields:
- Status Enum:
pending|processing|completed|failed - Relationships: Foreign keys to
users(CASCADE delete) andemails(SET NULL) - Indexes: Composite index on
(user_id, status, created_at)for efficient queries
Celery Configuration
- Queue Name:
email_default - Concurrency: 1 (sequential processing to avoid rate limits)
- Task Timeout: 180 seconds (3 minutes) per email
- Retry Policy: 3 attempts with exponential backoff
Error Handling
- Truncation: Error messages truncated to 1000 characters to prevent database bloat
- Step Tracking:
current_stepfield updated as pipeline progresses - Graceful Degradation: Failed items remain in queue with error details for debugging
Performance Optimization
- Single Query Position Calculation: Uses SQL window function instead of N separate queries
- 24-Hour Filter: Reduces query size by only showing recent items
- Index Usage: Leverages composite indexes for fast user-scoped queries
