A serverless API for a food diary application that helps users track their nutrition by analyzing meal photos and audio recordings using AI. The application automatically calculates nutritional information and helps users achieve their health goals.
- User Authentication: Secure signup and signin with JWT tokens
- Meal Tracking: Upload photos or audio recordings of meals
- AI-Powered Analysis: Uses OpenAI's GPT-4 Vision and Whisper models to analyze meals
- Nutritional Calculation: Automatic calculation of calories, proteins, carbohydrates, and fats
- Goal Setting: Personalized nutrition goals based on user profile (height, weight, activity level, goals)
- Serverless Architecture: Built on AWS Lambda with S3 for file storage and SQS for async processing
- Node.js 22.x - Runtime environment
- TypeScript - Type-safe JavaScript
- Serverless Framework - AWS Lambda deployment
- Drizzle ORM - Database ORM with PostgreSQL
- Neon Database - Serverless PostgreSQL
- AWS Lambda - Serverless compute
- Amazon S3 - File storage for meal images and audio
- Amazon SQS - Message queuing for async meal processing
- API Gateway - HTTP API endpoints
- OpenAI GPT-4 Vision - Image analysis for meal identification
- OpenAI Whisper - Audio transcription
- bcryptjs - Password hashing
- jsonwebtoken - JWT authentication
- Serverless Offline - Local development
- ESBuild - TypeScript compilation and bundling
- Node.js 22.x or higher
- AWS CLI configured with appropriate permissions
- A Neon PostgreSQL database
- OpenAI API key
git clone <repository-url>
cd foodiary-apinpm installCreate a .env file in the root directory with the following variables:
DATABASE_URL=your_neon_database_url
JWT_SECRET=your_jwt_secret_key
OPENAI_API_KEY=your_openai_api_keyRun the database migrations:
npx drizzle-kit pushStart the local development server:
npm run devThe API will be available at http://localhost:3000
Deploy to AWS:
serverless deployCreate a new user account.
Request Body:
{
"name": "John Doe",
"email": "john@example.com",
"password": "password123",
"goal": "lose",
"gender": "male",
"birthDate": "1990-01-01",
"height": 175,
"weight": 70,
"activityLevel": 3
}Authenticate user and get JWT token.
Request Body:
{
"email": "john@example.com",
"password": "password123"
}Get current user profile (requires authentication).
Create a new meal entry with file upload.
Request Body:
{
"inputType": "picture",
"inputFileKey": "user-uploads/meal-123.jpg"
}List all meals for the authenticated user.
Get specific meal details by ID.
The application follows a serverless architecture with the following components:
- API Gateway - Handles HTTP requests
- Lambda Functions - Process requests and business logic
- S3 Bucket - Stores uploaded meal images and audio files
- SQS Queue - Handles asynchronous meal processing
- PostgreSQL Database - Stores user data and meal information
- User uploads meal photo/audio → S3
- S3 triggers Lambda function → SQS
- SQS processes meal → AI analysis
- Results stored in database
src/
├── clients/ # AWS service clients
├── controllers/ # Request/response handling
├── db/ # Database schema and connection
├── functions/ # Lambda function handlers
├── lib/ # Utility libraries
├── queues/ # SQS queue processing
├── services/ # Business logic services
├── types/ # TypeScript type definitions
└── utils/ # Helper utilities
- Create Lambda function in
src/functions/ - Add controller in
src/controllers/ - Update
serverless.ymlwith new endpoint - Add database schema if needed
- Test locally with
npm run dev