Skip to main content
POST
/
department
/
new
Create a new department
curl --request POST \
  --url http://localhost:2000/department/new \
  --header 'Authorization: Bearer <token>' \
  --header 'Content-Type: application/json' \
  --data '
{
  "name": "Engineering",
  "description": "Handles all software engineering operations",
  "code": "ENG-001",
  "location": "San Francisco HQ",
  "parentDepartment": "68c87b38e0c86b191d1b3c5b"
}
'
{
  "message": "<string>",
  "data": {
    "department": {
      "_id": "64ef3c29f9a1c27e1b2c3a4d",
      "name": "Engineering",
      "parentDepartment": {
        "_id": "64ef3c29f9a1c27e1b2c3aaa",
        "name": "Technology",
        "description": "Some description for this department",
        "code": "TECH"
      },
      "manager": {
        "_id": "64ef3c29f9a1c27e1b2c3a4d",
        "emails": [
          "[email protected]"
        ],
        "personalInformation": {
          "firstName": "John",
          "lastName": "Doe",
          "name": "John Doe"
        }
      },
      "description": "Handles all software engineering operations"
    }
  }
}
Admin-only endpoint to create a new department.

Request

Headers

NameTypeRequiredDescription
AuthorizationstringYesBearer token
Content-TypestringYesapplication/json

Request Body

{
  "name": "Quality Assurance",
  "description": "Ensures product quality and testing standards",
  "parentId": "64b7f2b3e4b0a5d3f9c54321"
}

Request Body Schema

FieldTypeRequiredDescription
namestringYesDepartment name
descriptionstringNoDepartment description
parentIdstringNoParent department ID (for sub-departments)

Field Details

  • name: Must be unique within the organization
  • description: Optional but recommended for clarity
  • parentId: Optional - omit for root-level department

Response

200 OK - Successfully created department

{
  "message": "Department created successfully",
  "data": {
    "department": {
      "_id": "64b7f7g8h9i0j1k2l3m4n5o",
      "name": "Quality Assurance",
      "description": "Ensures product quality and testing standards",
      "parentId": "64b7f2b3e4b0a5d3f9c54321",
      "createdAt": "2024-01-15T10:30:00.000Z",
      "updatedAt": "2024-01-15T10:30:00.000Z"
    }
  }
}

400 Bad Request

{
  "error": {
    "code": "VALIDATION_ERROR",
    "message": "Department name already exists"
  }
}

401 Unauthorized

{
  "error": {
    "code": "UNAUTHORIZED",
    "message": "Missing or invalid token or insufficient permissions"
  }
}

500 Internal Server Error

{
  "error": {
    "code": "SERVER_ERROR",
    "message": "Internal server error"
  }
}

Examples

Create a root-level department

curl -X POST 'http://localhost:2000/department/new' \
  -H 'Authorization: Bearer your-jwt-token' \
  -H 'Content-Type: application/json' \
  -d '{
    "name": "Marketing",
    "description": "Marketing and promotional activities"
  }'

Create a sub-department

curl -X POST 'http://localhost:2000/department/new' \
  -H 'Authorization: Bearer your-jwt-token' \
  -H 'Content-Type: application/json' \
  -d '{
    "name": "Digital Marketing",
    "description": "Online marketing and social media",
    "parentId": "64b7f7g8h9i0j1k2l3m4n5o"
  }'

Notes

  • This is an admin-only endpoint - requires administrative privileges
  • Department names must be unique across the entire organization
  • Parent department ID must be a valid MongoDB ObjectId
  • Creating a sub-department requires the parent to exist
  • The created department is returned in the response
  • Timestamps are automatically generated (createdAt, updatedAt)
  • Department names are case-sensitive
  • Consider the organizational structure before creating departments

Best Practices

  1. Planning: Map out your department hierarchy before creation
  2. Naming: Use clear, descriptive names
  3. Descriptions: Provide meaningful descriptions for clarity
  4. Hierarchy: Keep hierarchy levels reasonable (recommended: max 5 levels)
  5. Review: Review department structure periodically

Common Use Cases

  • Creating new functional departments (e.g., “Data Science”)
  • Adding regional offices as departments
  • Setting up project-based departments
  • Restructuring organizational units

Authorizations

Authorization
string
header
required

Bearer authentication header of the form Bearer <token>, where <token> is your auth token.

Body

application/json
name
string
required

Department name

Example:

"Engineering"

description
string
required

Department description

Example:

"Handles all software engineering operations"

code
string
required

Department code

Example:

"ENG-001"

location
string
required

Department location

Example:

"San Francisco HQ"

parentDepartment
string | null

Parent department info

Example:

"68c87b38e0c86b191d1b3c5b"

Response

Successfully created department

message
string
data
object