> ## Documentation Index
> Fetch the complete documentation index at: https://help.dingtalk.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Create a role

> Call this API to create a new role under a specified role group, after which members can be assigned to the role and permissions configured.

Call this API to create a new role.

## Request

| **Basic information** |                                                                                   |
| --------------------- | --------------------------------------------------------------------------------- |
| HTTP URL              | [https://oapi.dingtalk.io/role/add\_role](https://oapi.dingtalk.io/role/add_role) |
| HTTP Method           | POST                                                                              |
| Supported app types   | appType-Internal app                                                              |
| Required permissions  | permission-qyapi\_manage\_addresslist-Contacts data management permission         |

### Query parameters

| Name          | Type   | Required | Example  | Description                                                                                                                                                       |
| ------------- | ------ | -------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| access\_token | String | Yes      | 6d1bxxxx | The app credential used to call this API. Obtain it by calling the API for getting the [access\_token of an internal app](/open/development/obtain-orgapp-token). |

### Request body

| Name     | Type   | Required | Example    | Description                                                                                                                                                                                                                                                                                                                             |
| -------- | ------ | -------- | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| roleName | String | Yes      | Service    | The name of the role.                                                                                                                                                                                                                                                                                                                   |
| groupId  | Number | Yes      | 1507113595 | The ID of the role group.   - If the target role group already exists, call the [Get the list of roles](/open/development/obtains-a-list-of-enterprise-roles) API to obtain it. - If the role group has not been created, first call the [Create a role group](/open/development/add-a-role-group) API to create one and obtain its ID. |

### Request example

```curl theme={"theme":{"light":"github-light","dark":"github-dark"}}
curl -X POST "https://oapi.dingtalk.io/role/add_role" \
-H 'Content-Type:application/x-www-form-urlencoded;charset=utf-8' \
-d 'access_token=2e3f55xxxx6e34' \
-d 'groupId=11' \
-d 'roleName=test'
```

Java

```text theme={"theme":{"light":"github-light","dark":"github-dark"}}
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.io/role/add_role");
OapiRoleAddRoleRequest req = new OapiRoleAddRoleRequest();
req.setRoleName("Test1");
req.setGroupId(1507113595L);
OapiRoleAddRoleResponse rsp = client.execute(req, access_token);
System.out.println(rsp.getBody());
```

Python

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import dingtalk.api

req=dingtalk.api.OapiRoleAddRoleRequest("https://oapi.dingtalk.io/role/add_role")

req.roleName="test"
req.groupId=11
try:
  resp= req.getResponse(access_token)
  print(resp)
except Exception,e:
  print(e)
```

PHP

```php theme={"theme":{"light":"github-light","dark":"github-dark"}}
include "TopSdk.php";
date_default_timezone_set('Asia/Shanghai');

$c = new DingTalkClient(DingTalkConstant::$CALL_TYPE_OAPI, DingTalkConstant::$METHOD_POST , DingTalkConstant::$FORMAT_JSON);
$req = new OapiRoleAddRoleRequest;
$req->setRoleName("test");
$req->setGroupId("11");
$resp = $c->execute($req, $access_token, "https://oapi.dingtalk.io/role/add_role");
```

C#

```text theme={"theme":{"light":"github-light","dark":"github-dark"}}
IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.io/role/add_role");
OapiRoleAddRoleRequest req = new OapiRoleAddRoleRequest();
req.RoleName = "test";
req.GroupId = 11L;
OapiRoleAddRoleResponse rsp = client.Execute(req, access_token);
Console.WriteLine(rsp.Body);
```

## Response

### Response body

| Name    | Type   | Example    | Description                         |
| ------- | ------ | ---------- | ----------------------------------- |
| roleId  | Number | 1688611118 | The ID of the role.                 |
| errcode | Number | 0          | The return code.                    |
| errmsg  | String | ok         | The description of the return code. |

### Response body example

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "errcode":0,
  "roleId":1688611118,
  "errmsg":"ok"
}
```

### Error codes

If an error is returned when you call this API, find the solution in the [Global error codes](/open/development/server-api-error-codes-1) document based on the error message.

| Error code (errcode) | Error message (errmsg)       | Solution                           |
| -------------------- | ---------------------------- | ---------------------------------- |
| 34018                | The role cannot be empty     | Provide both roleName and groupId. |
| 34020                | Invalid groupId              | Check whether groupId is correct.  |
| 34021                | The role name already exists | Check whether roleName is correct. |
| -1                   | System busy                  | Try again later.                   |
