Get department hierarchy
curl --request GET \
--url http://localhost:2000/department/hierarchy \
--header 'Authorization: Bearer <token>'import requests
url = "http://localhost:2000/department/hierarchy"
headers = {"Authorization": "Bearer <token>"}
response = requests.get(url, headers=headers)
print(response.text)const options = {method: 'GET', headers: {Authorization: 'Bearer <token>'}};
fetch('http://localhost:2000/department/hierarchy', 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/hierarchy",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "http://localhost:2000/department/hierarchy"
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", "Bearer <token>")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.get("http://localhost:2000/department/hierarchy")
.header("Authorization", "Bearer <token>")
.asString();require 'uri'
require 'net/http'
url = URI("http://localhost:2000/department/hierarchy")
http = Net::HTTP.new(url.host, url.port)
request = Net::HTTP::Get.new(url)
request["Authorization"] = 'Bearer <token>'
response = http.request(request)
puts response.read_body{
"message": "<string>",
"data": {
"departments": [
{
"_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",
"employeesCount": 25,
"children": "<array>"
}
]
}
}Department Information
Get Department Hierarchy
Admin-only endpoint that retrieves the full hierarchical structure of all departments. Each department may include nested child departments inside the children array.
GET
/
department
/
hierarchy
Get department hierarchy
curl --request GET \
--url http://localhost:2000/department/hierarchy \
--header 'Authorization: Bearer <token>'import requests
url = "http://localhost:2000/department/hierarchy"
headers = {"Authorization": "Bearer <token>"}
response = requests.get(url, headers=headers)
print(response.text)const options = {method: 'GET', headers: {Authorization: 'Bearer <token>'}};
fetch('http://localhost:2000/department/hierarchy', 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/hierarchy",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "http://localhost:2000/department/hierarchy"
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", "Bearer <token>")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.get("http://localhost:2000/department/hierarchy")
.header("Authorization", "Bearer <token>")
.asString();require 'uri'
require 'net/http'
url = URI("http://localhost:2000/department/hierarchy")
http = Net::HTTP.new(url.host, url.port)
request = Net::HTTP::Get.new(url)
request["Authorization"] = 'Bearer <token>'
response = http.request(request)
puts response.read_body{
"message": "<string>",
"data": {
"departments": [
{
"_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",
"employeesCount": 25,
"children": "<array>"
}
]
}
}Admin-only endpoint that retrieves the full hierarchical structure of all departments. Each department may include nested child departments inside the
children array.
Request
Headers
| Name | Type | Required | Description |
|---|---|---|---|
| Authorization | string | Yes | Bearer token |
Response
200 OK - Successfully retrieved department hierarchy
{
"message": "Department hierarchy retrieved successfully",
"data": {
"departments": [
{
"_id": "64b7f2b3e4b0a5d3f9c54321",
"name": "Customer Support",
"description": "Handles customer inquiries and support tickets",
"parentId": null,
"createdAt": "2024-01-15T10:30:00.000Z",
"updatedAt": "2024-01-15T10:30:00.000Z",
"children": [
{
"_id": "64b7f3c4e4b0a5d3f9c98765",
"name": "Tier 1 Support",
"description": "First level customer support",
"parentId": "64b7f2b3e4b0a5d3f9c54321",
"createdAt": "2024-01-15T10:30:00.000Z",
"updatedAt": "2024-01-15T10:30:00.000Z",
"children": [
{
"_id": "64b7f4d5e4b0a5d3f9c11111",
"name": "Email Support",
"description": "Email-based customer support",
"parentId": "64b7f3c4e4b0a5d3f9c98765",
"createdAt": "2024-01-15T10:30:00.000Z",
"updatedAt": "2024-01-15T10:30:00.000Z",
"children": []
}
]
},
{
"_id": "64b7f5e6e4b0a5d3f9c22222",
"name": "Tier 2 Support",
"description": "Second level escalated support",
"parentId": "64b7f2b3e4b0a5d3f9c54321",
"createdAt": "2024-01-15T10:30:00.000Z",
"updatedAt": "2024-01-15T10:30:00.000Z",
"children": []
}
]
},
{
"_id": "64b7f6f7e4b0a5d3f9c33333",
"name": "Engineering",
"description": "Software development and technical operations",
"parentId": null,
"createdAt": "2024-01-15T10:30:00.000Z",
"updatedAt": "2024-01-15T10:30:00.000Z",
"children": []
}
]
}
}
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"
}
}
Example
curl -X GET 'http://localhost:2000/department/hierarchy' \
-H 'Authorization: Bearer your-jwt-token'
Notes
- This is an admin-only endpoint - requires administrative privileges
- Returns departments in a hierarchical tree structure
- Root departments have
parentId: null - Child departments are nested in the
childrenarray - The hierarchy can have multiple levels of nesting
- Use this for organizational charts or department trees
- Departments are sorted alphabetically within each level
- Empty
childrenarrays indicate leaf departments (no sub-departments)
Use Cases
- Organizational Charts: Display company structure
- Department Selection: Create hierarchical dropdown menus
- Reporting Structure: Show management hierarchy
- Access Control: Implement department-based permissions
- Analytics: Analyze departmental data with hierarchy context
Performance Considerations
- Large organizational hierarchies may take longer to load
- Consider caching the hierarchy for frequently accessed data
- The endpoint returns the complete hierarchy in a single request
- For very large organizations, consider using the list endpoint with pagination
Was this page helpful?