Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/tutosrive/fastapi-CRUD-MongoDB/llms.txt

Use this file to discover all available pages before exploring further.

The POST /tasks/ endpoint accepts a JSON request body that conforms to the Task model and inserts a new document into the MongoDB task collection. On success, the controller re-fetches the newly inserted document by its generated _id, converts it with task_entity(), and returns the full task object — including the MongoDB-assigned id — with an HTTP 200 response. If the insert operation fails, a 400 Bad Request is returned. POST /tasks/

Request Body

title
string
required
A short, human-readable title for the task (e.g. "Buy groceries").
description
string
required
A detailed description of what the task involves (e.g. "Milk, eggs, bread").
created_by
string
required
An identifier for the user creating the task. Typically an email address or username (e.g. "alice@example.com").
completed
boolean
Whether the task is already completed at creation time. Defaults to false if omitted.

Example Request

curl -X POST http://localhost:5000/tasks/ \
  -H "Content-Type: application/json" \
  -d '{
    "title": "Buy groceries",
    "description": "Milk, eggs, bread",
    "created_by": "alice@example.com"
  }'

Example Request Body

{
  "title": "Buy groceries",
  "description": "Milk, eggs, bread",
  "created_by": "alice@example.com"
}

Example Response

{
  "id": "64a1b2c3d4e5f6789abcde01",
  "title": "Buy groceries",
  "description": "Milk, eggs, bread",
  "completed": false,
  "created_by": "alice@example.com"
}

Response Fields

id
string
MongoDB ObjectId assigned to the new document, serialised as a 24-character hex string. Use this value in subsequent GET, PUT, or DELETE calls.
title
string
The task title as provided in the request body.
description
string
The task description as provided in the request body.
completed
boolean
Reflects the completed value from the request. Always false when the field is omitted.
created_by
string
The user identifier as provided in the request body.
The completed field defaults to false at the model level (Optional[bool] = False), so you never need to include it in the request body when creating a new, incomplete task.

Error Responses

StatusDetailCause
400 Bad Request"The task can't bee added!"MongoDB insert_one() did not return a result.
422 Unprocessable EntityValidation error detailsOne or more required fields (title, description, created_by) are missing or have the wrong type.
500 Internal Server ErrorServer error messageDatabase connection failure or unexpected exception.

Build docs developers (and LLMs) love