{"id":18068398,"url":"https://github.com/fahimfba/fastapi-mongo-todo","last_synced_at":"2026-04-16T04:02:38.224Z","repository":{"id":259420524,"uuid":"877784213","full_name":"FahimFBA/fastapi-mongo-todo","owner":"FahimFBA","description":"ToDo using FastAPI and MongoDB","archived":false,"fork":false,"pushed_at":"2024-10-24T13:22:09.000Z","size":9,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-19T16:48:53.008Z","etag":null,"topics":["fastapi","fastapi-mongodb","mongodb","todolist"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/FahimFBA.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-24T08:32:50.000Z","updated_at":"2024-10-24T13:22:12.000Z","dependencies_parsed_at":"2024-10-25T12:48:25.335Z","dependency_job_id":null,"html_url":"https://github.com/FahimFBA/fastapi-mongo-todo","commit_stats":null,"previous_names":["fahimfba/fastapi-mongo-todo"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/FahimFBA/fastapi-mongo-todo","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FahimFBA%2Ffastapi-mongo-todo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FahimFBA%2Ffastapi-mongo-todo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FahimFBA%2Ffastapi-mongo-todo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FahimFBA%2Ffastapi-mongo-todo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/FahimFBA","download_url":"https://codeload.github.com/FahimFBA/fastapi-mongo-todo/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FahimFBA%2Ffastapi-mongo-todo/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31870516,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-15T15:24:51.572Z","status":"online","status_checked_at":"2026-04-16T02:00:06.042Z","response_time":69,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fastapi","fastapi-mongodb","mongodb","todolist"],"created_at":"2024-10-31T08:06:18.967Z","updated_at":"2026-04-16T04:02:38.186Z","avatar_url":"https://github.com/FahimFBA.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# To-Do Backend API with FastAPI and MongoDB\n\nThis is a simple **To-Do backend** built with **FastAPI** and **MongoDB**. The API allows users to manage their to-do tasks through standard REST operations. It uses **MongoDB** as the database and **Pydantic** for request validation.\n\n## Project Structure\n\n```bash\n├── config/           # Contains configuration-related files (e.g., database connection)\n├── models/           # Defines MongoDB models\n├── routes/           # Contains route definitions (e.g., CRUD operations)\n├── schema/           # Defines the Pydantic models for request/response validation\n├── main.py           # The main entry point for the FastAPI app\n├── .env.example      # Environment variables example (MongoDB credentials)\n├── requirements.txt  # Python dependencies\n└── README.md         # Project documentation\n```\n\n## Features\n\n- **Create, Read, Update, Delete (CRUD)** to-do tasks.\n- **FastAPI** for fast, modern, and asynchronous Python API development.\n- **MongoDB** as the NoSQL database.\n- **Pydantic** for data validation.\n- Supports environment variables via `.env` for MongoDB credentials.\n\n## Requirements\n\n- **Python 3.9+**\n- **MongoDB Atlas** (or local MongoDB instance)\n- **FastAPI**\n- **Uvicorn**\n- **Pymongo**\n- **Pydantic**\n- **dotenv** for environment variables\n\n## Installation\n\n1. **Clone the repository:**\n\n   ```bash\n   git clone https://github.com/FahimFBA/fastapi-mongo-todo.git\n   cd fastapi-mongo-todo\n   ```\n\n2. **Create a virtual environment:**\n\n   ```bash\n   python -m venv venv\n   source venv/bin/activate   # On Windows, use `venv\\Scripts\\activate`\n   ```\n\n3. **Install the dependencies:**\n\n   ```bash\n   pip install -r requirements.txt\n   ```\n\n4. **Create a `.env` file in the project root and add your MongoDB credentials:**\n\n   **.env**\n   ```env\n    MONGO_USERNAME=\n    MONGO_PASSWORD=\n   ```\n\n## Configuration\n\nThe **config** directory contains the configuration for connecting to MongoDB. The database connection string is dynamically created using the environment variables.\n\n```python\n# config/database.py\n\nfrom pymongo import MongoClient\nfrom dotenv import load_dotenv\nimport os\n\n# Load environment variables from .env file\nload_dotenv()\n\n# Retrieve the MongoDB credentials from the environment variables\nusername = os.getenv(\"MONGO_USERNAME\")\npassword = os.getenv(\"MONGO_PASSWORD\")\ncluster = os.getenv(\"MONGO_CLUSTER_URL\")\n\n# Create the MongoDB connection URI using the loaded credentials\nuri = f\"mongodb+srv://{username}:{password}@{\n    cluster}/?retryWrites=true\u0026w=majority\u0026appName=Cluster0\"\n\n# Initialize MongoDB client and access the database and collection\nclient = MongoClient(uri)\ndb = client.todo_db\ncollection_name = db[\"todo_collection\"]\n```\n\n## Running the Application\n\n1. **Start the FastAPI server:**\n\n   You can run the FastAPI application using Uvicorn:\n\n   ```bash\n   uvicorn main:app --reload\n   ```\n\n   or,\n\n   ```bash\n   fastapi dev main.py\n   ```\n\n   The app will run on `http://127.0.0.1:8000`.\n\n2. **API Documentation:**\n\n   FastAPI automatically generates interactive API docs:\n   - **Swagger UI:** Visit `http://127.0.0.1:8000/docs`\n   - **Redoc UI:** Visit `http://127.0.0.1:8000/redoc`\n\n## API Endpoints\n\n### Base URL: `/todos`\n\n| Method | Endpoint       | Description              |\n|--------|----------------|--------------------------|\n| GET    | `/`            | Get all to-do items       |\n| POST   | `/`            | Create a new to-do        |\n| PUT    | `/{id}`        | Update a to-do by ID      |\n| DELETE | `/{id}`        | Delete a to-do by ID      |\n\n### Code Breakdown:\n- **GET `/todos/`**: Fetch all to-do items from the database.\n- **POST `/todos/`**: Create a new to-do item in the database.\n- **PUT `/todos/{id}`**: Update an existing to-do by its MongoDB object ID.\n- **DELETE `/todos/{id}`**: Delete an existing to-do by its MongoDB object ID.\n\n\n### Example To-Do Item Schema\n\n```json\n{\n  \"title\": \"Finish project\",\n  \"description\": \"Work on the FastAPI backend\",\n  \"completed\": false\n}\n```\n\n## Project Structure\n\n### `/config`\nContains configuration files, including the MongoDB connection setup.\n\n### `/models`\nDefines the MongoDB data models. This is where the schema for your MongoDB collections resides.\n\n### `/routes`\nContains the route definitions for handling HTTP requests such as creating, reading, updating, and deleting tasks.\n\nExample route for deleting a to-do:\n\n```python\n# routes/todo.py\n\nfrom fastapi import APIRouter\nfrom models.todos import Todo\nfrom config.database import collection_name\nfrom schema.schemas import list_serial\nfrom bson import ObjectId\n\nrouter = APIRouter()\n\n# DELETE Request Method\n@router.delete(\"/{id}\")\nasync def delete_todo(id: str):\n    collection_name.find_one_and_delete({\"_id\": ObjectId(id)})\n\n```\n\n### `/schema`\nDefines the Pydantic models for data validation and serialization of requests and responses.\n\n```python\n# schema/schemas.py\n\ndef individual_serial(todo) -\u003e dict:\n    return {\n        \"id\": str(todo[\"_id\"]),\n        \"name\": todo[\"name\"],\n        \"description\": todo[\"description\"],\n        \"complete\": todo[\"complete\"]\n    }\n\ndef list_serial(todos) -\u003e list:\n    return[individual_serial(todo) for todo in todos]\n```\n\n## Deployment\n\nYou can deploy this FastAPI app to platforms like:\n\n- **Deta**\n- **Render**\n- **Railway**\n- **Fly.io**\n\nYou can also use **MongoDB Atlas** as a managed cloud MongoDB instance.\n\n## Contributing\n\nContributions are welcome! Feel free to submit a pull request or open an issue if you have any suggestions or improvements.\n\n## License\n\nThis project is licensed under the [MIT](./LICENSE) License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffahimfba%2Ffastapi-mongo-todo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffahimfba%2Ffastapi-mongo-todo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffahimfba%2Ffastapi-mongo-todo/lists"}