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 /users/ endpoint accepts a JSON request body containing name, age, and email, validates it against the User Pydantic model, and inserts the resulting document into the MongoDB local.user collection. After a successful insertion, the controller fetches the newly created document by its inserted_id and returns it — including the MongoDB-assigned id — as a JSON object. If the insertion fails, the API returns HTTP 400 Bad Request. POST /users/

Request Body

name
string
required
The user’s full name.
age
integer
required
The user’s age.
email
string
required
The user’s email address.
Do not include id in the request body. MongoDB generates the ObjectId automatically on insertion, and it is returned in the response.

Response Fields

id
string
The MongoDB-assigned ObjectId serialized as a 24-character hexadecimal string.
name
string
The user’s full name, echoed from the request body.
age
integer
The user’s age, echoed from the request body.
email
string
The user’s email address, echoed from the request body.

Example Request

curl -X POST http://localhost:5000/users/ \
  -H "Content-Type: application/json" \
  -d '{"name": "Alice", "age": 30, "email": "alice@example.com"}'

Request Body

{
  "name": "Alice",
  "age": 30,
  "email": "alice@example.com"
}

Example Response

A successful request returns HTTP 200 OK with the newly created user, including its assigned id.
{
  "id": "64a1b2c3d4e5f6789abcdef0",
  "name": "Alice",
  "age": 30,
  "email": "alice@example.com"
}

Error Responses

400 Bad Request

Returned when the MongoDB insertion does not produce a result.
{
  "detail": "The user can't bee added!"
}

Build docs developers (and LLMs) love