Hi! This is my AI Email Generator backend API, now rebuilt using NestJS. It helps people write emails faster using AI. It uses MongoDB to save all the data and Google Gemini to generate emails. It's really cool because it remembers the emails you already sent and can reply to them with full context!
- NestJS (TypeScript) — modular backend framework with Dependency Injection
- MongoDB + Mongoose — to save the emails and threads
- Google Gemini AI — the LLM that writes emails
- class-validator — runtime DTO validation with decorators
- @nestjs/throttler — built-in rate limiting
- @nestjs/config — environment variable management
- First, download or clone my code.
- Open your terminal and type:
npm install - Make a file named
.envinside the folder and paste this. You need your own keys!MONGODB_URI=your_mongo_db_url_here GEMINI_API_KEY=your_google_ai_key_here
- Run the code:
npm run start:dev
I used Postman to test these out:
- Create a new email:
POST /api/threadsSend JSON with aprompt(what the email is about),recipientName, andtone. - See all emails:
GET /api/threads - Get thread details:
GET /api/threads/:id - Reply to an email:
POST /api/threads/:id/reply(Replace:idwith your actual thread ID from the database) and send it areplyInstruction.
Sometimes I get stuck trying to figure out what to say in an email, and remembering the whole conversation gets confusing. I wanted to build an API that saves the entire conversation so the AI can write super accurate replies for me automatically. Plus, I really wanted to practice building APIs with NestJS and MongoDB!
flowchart TD
A[Client Frontend] -->|POST /api/threads| B[NestJS Controller]
B --> C[ValidationPipe + DTO]
C -->|Invalid| D[400 Bad Request]
D --> A
C -->|Valid| E[ThreadsService]
E --> F[AiService → Google Gemini API]
F -->|Email JSON| E
E --> G[MongoDB via Mongoose]
G -->|Saved| E
E --> H[Email Response]
H --> B
B -->|201 Created| A
src/
├── ai/ # AI module (Gemini integration)
│ ├── ai.module.ts
│ ├── ai.service.ts
│ └── ai.service.spec.ts
├── common/ # Shared utilities
│ ├── exceptions/
│ │ └── api.exception.ts
│ └── filters/
│ └── http-exception.filter.ts
├── schemas/ # Mongoose schemas
│ ├── email.schema.ts
│ └── email-thread.schema.ts
├── threads/ # Threads feature module
│ ├── dto/
│ │ ├── create-thread.dto.ts
│ │ └── reply-thread.dto.ts
│ ├── threads.controller.ts
│ ├── threads.module.ts
│ └── threads.service.ts
├── app.module.ts # Root module
└── main.ts # Entry point
- Add WebSocket support for real-time email generation status
- Implement background job processing with Bull queues
- Add Swagger/OpenAPI documentation
- Add authentication and user management
If you know how to use Docker, I also made it ready for containers!
- Build the image:
docker build -t my-email-api . - Run it (don't forget to put your real keys!):
docker run -p 3000:3000 -e GEMINI_API_KEY="your_ai_key" -e MONGODB_URI="your_db_url" my-email-api
Thanks for checking out my project! I learned a lot about NestJS, Dependency Injection, modular architecture, and prompting AI while making it.