Skip to main content
Call this API to get the list of IDs of all parent departments of a specified department. This API is not restricted by the Contacts permission scope.

Request

Basic information
HTTP URLhttps://oapi.dingtalk.io/topapi/v2/department/listparentbydept
HTTP MethodPOST
Supported app typesappType-Internal app, appType-Third-party enterprise app
Permissions requiredpermission-qyapi_get_department_list-Read permission for Contacts department information

Query parameters

NameTypeRequiredExampleDescription
access_tokenStringYesbE74xxxxThe app credential used to call this API. - For an internal app, obtain it through the API for getting the access_token of an internal app. - For a third-party enterprise app, obtain it through the API for getting the access_token of a third-party enterprise.

Request body

NameTypeRequiredExampleDescription
dept_idNumberYes42xxxx36The ID of the department to query. Call the Get department list API to obtain the value of dept_id.

Request example

curl -X POST "https://oapi.dingtalk.io/topapi/v2/department/listparentbydept" \
-H 'Content-Type:application/x-www-form-urlencoded;charset=utf-8' \
-d 'access_token=a456xxxx13da5' \
-d 'dept_id=100'
Java
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.io/topapi/v2/department/listparentbydept");
OapiV2DepartmentListparentbydeptRequest req = new OapiV2DepartmentListparentbydeptRequest();
req.setDeptId(100L);
OapiV2DepartmentListparentbydeptResponse rsp = client.execute(req, access_token);
System.out.println(rsp.getBody());
Python
import dingtalk.api

req=dingtalk.api.OapiV2DepartmentListparentbydeptRequest("https://oapi.dingtalk.io/topapi/v2/department/listparentbydept")

req.dept_id=100
try:
  resp= req.getResponse(access_token)
  print(resp)
except Exception,e:
  print(e)
PHP
include "TopSdk.php";
date_default_timezone_set('Asia/Shanghai');

$c = new DingTalkClient(DingTalkConstant::$CALL_TYPE_OAPI, DingTalkConstant::$METHOD_POST , DingTalkConstant::$FORMAT_JSON);
$req = new OapiV2DepartmentListparentbydeptRequest;
$req->setDeptId("100");
$resp = $c->execute($req, $access_token, "https://oapi.dingtalk.io/topapi/v2/department/listparentbydept");
C#
IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.io/topapi/v2/department/listparentbydept");
OapiV2DepartmentListparentbydeptRequest req = new OapiV2DepartmentListparentbydeptRequest();
req.DeptId = 100L;
OapiV2DepartmentListparentbydeptResponse rsp = client.Execute(req, access_token);
Console.WriteLine(rsp.Body);

Response

Response body

NameTypeExampleDescription
request_idString10u9dj0853fweThe request ID.
errcodeNumber0The return code.
errmsgStringInvalid department IDThe error message returned when the call failed.
resultDeptListParentByDeptIdResponseThe list of parent department IDs.
parent_id_listNumber[][789, 456, 123, 1]The list of IDs of all parent departments of the department. Note In the returned list, the first element is the ID of the current department, and the last element is the ID of the top-level parent department. The department tier increases from left to right.

Response body example

{
  "errcode": 0,
  "errmsg": "ok",
  "result": {
    "parent_id_list": [
      42xxxx36,
      1
    ]
  },
  "request_id": "ov7bc964j9pe"
}

Error codes

If an error is returned when you call this API, refer to the Global error codes document to find a solution based on the error message.
Error code (errcode)Error message (errmsg)Solution
43007Authorization requiredMake sure the access_token has the required permissions.
60003Department not foundMake sure dept_id is correct.
400002Invalid parameterMake sure the parameters are entered as required.
-1System busyTry again later.