Create Document
Document Operations
Create Document
Create a new document in a collection with automatic field population and validation
POST
Create Document
Overview
Creates a new document in the specified collection. The endpoint automatically:- Validates the document against the collection schema
- Populates
created_at,updated_at,created_byfields - Applies tenant isolation if configured
- Executes pre/post-create hooks
- Enforces RBAC permissions
- Logs the operation to audit trail
Request
Path Parameters
The name of the collection to create the document in
Headers
Bearer token for authentication
Body
The request body should contain a JSON object representing the document to create. All fields will be validated against the collection’s schema.Custom fields as defined in your collection schema. The system automatically adds:
created_at(timestamp)updated_at(timestamp)created_by(user ID from auth context)- Tenant/owner fields if configured in the collection’s access settings
Response
The unique identifier of the newly created document
ISO 8601 timestamp when the document was created
ISO 8601 timestamp when the document was last updated
User ID of the user who created the document
All other fields from your request body, filtered by field-level permissions
Examples
Basic Create
Response
Implementation Details
MongoDB Query Pattern
The handler uses MongoDB’sinsertOne operation:
Workflow
- Extract collection name from URL path (handlers_crud.go:67)
- Authenticate request and extract auth context (handlers_crud.go:74)
- Validate collection exists in schema config (handlers_crud.go:81)
- Check RBAC permissions for create action (handlers_crud.go:90)
- Parse JSON body and validate structure (handlers_crud.go:99)
- Auto-populate fields:
created_by,created_at,updated_at(handlers_crud.go:108-111)- Tenant field (e.g.,
company_id) from auth context (handlers_crud.go:114) - Owner field if configured (handlers_crud.go:117)
- Validate against schema using validator (handlers_crud.go:122)
- Execute pre-create hooks if configured (handlers_crud.go:134)
- Insert into MongoDB using store.Create (handlers_crud.go:155)
- Execute post-create hooks (best-effort) (handlers_crud.go:168)
- Log audit event with success/failure (handlers_crud.go:183)
- Apply field policy to filter response fields (handlers_crud.go:186)
- Return 201 Created with document (handlers_crud.go:189)
Automatic Field Population
Error Responses
400 Bad Request
Returned when:- Collection name is missing
- Invalid JSON body
- Schema validation fails
- Pre-create hook fails
401 Unauthorized
403 Forbidden
Returned when user doesn’t have create permission:404 Not Found
Returned when collection doesn’t exist in schema:500 Internal Server Error
Hooks
You can intercept the create operation using hooks:Pre-Create Hook
Executed before the document is inserted. Can modify the document or reject the operation:Post-Create Hook
Executed after successful creation (best-effort, won’t fail the request):Related Endpoints
- Update Document - Modify an existing document
- Get Document - Retrieve a single document
- List Documents - Query multiple documents
- Delete Document - Remove a document