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"
}
'
import requests

url = "http://localhost:2000/department/new"

payload = {
"name": "Engineering",
"description": "Handles all software engineering operations",
"code": "ENG-001",
"location": "San Francisco HQ",
"parentDepartment": "68c87b38e0c86b191d1b3c5b"
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}

response = requests.post(url, json=payload, headers=headers)

print(response.text)
const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
name: 'Engineering',
description: 'Handles all software engineering operations',
code: 'ENG-001',
location: 'San Francisco HQ',
parentDepartment: '68c87b38e0c86b191d1b3c5b'
})
};

fetch('http://localhost:2000/department/new', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));
<?php

$curl = curl_init();

curl_setopt_array($curl, [
CURLOPT_PORT => "2000",
CURLOPT_URL => "http://localhost:2000/department/new",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'name' => 'Engineering',
'description' => 'Handles all software engineering operations',
'code' => 'ENG-001',
'location' => 'San Francisco HQ',
'parentDepartment' => '68c87b38e0c86b191d1b3c5b'
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
package main

import (
"fmt"
"strings"
"net/http"
"io"
)

func main() {

url := "http://localhost:2000/department/new"

payload := strings.NewReader("{\n \"name\": \"Engineering\",\n \"description\": \"Handles all software engineering operations\",\n \"code\": \"ENG-001\",\n \"location\": \"San Francisco HQ\",\n \"parentDepartment\": \"68c87b38e0c86b191d1b3c5b\"\n}")

req, _ := http.NewRequest("POST", url, payload)

req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")

res, _ := http.DefaultClient.Do(req)

defer res.Body.Close()
body, _ := io.ReadAll(res.Body)

fmt.Println(string(body))

}
HttpResponse<String> response = Unirest.post("http://localhost:2000/department/new")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"name\": \"Engineering\",\n \"description\": \"Handles all software engineering operations\",\n \"code\": \"ENG-001\",\n \"location\": \"San Francisco HQ\",\n \"parentDepartment\": \"68c87b38e0c86b191d1b3c5b\"\n}")
.asString();
require 'uri'
require 'net/http'

url = URI("http://localhost:2000/department/new")

http = Net::HTTP.new(url.host, url.port)

request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"name\": \"Engineering\",\n \"description\": \"Handles all software engineering operations\",\n \"code\": \"ENG-001\",\n \"location\": \"San Francisco HQ\",\n \"parentDepartment\": \"68c87b38e0c86b191d1b3c5b\"\n}"

response = http.request(request)
puts response.read_body
{
  "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": [
          "user@example.com"
        ],
        "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