> ## 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.

# 创建角色

> 调用本接口可在指定角色组下创建新角色,后续可向该角色分配成员并配置权限。

调用本接口，创建新角色。

![新增角色](https://help-static-aliyun-doc.aliyuncs.com/assets/img/zh-CN/9985197361/p358772.png)

## 请求

| **基本信息**    |                                                                                   |
| ----------- | --------------------------------------------------------------------------------- |
| HTTP URL    | [https://oapi.dingtalk.io/role/add\_role](https://oapi.dingtalk.io/role/add_role) |
| HTTP Method | POST                                                                              |
| 支持的应用类型     | appType-企业内部应用                                                                    |
| 权限要求        | permission-qyapi\_manage\_addresslist-通讯录数据管理权限                                   |

### 查询参数

| 名称            | 类型     | 是否必填 | 示例值      | 描述                                                                                   |
| ------------- | ------ | ---- | -------- | ------------------------------------------------------------------------------------ |
| access\_token | String | 是    | 6d1bxxxx | 调用该接口的应用凭证，通过[获取企业内部应用的access\_token](/zh/open/development/obtain-orgapp-token)接口获取。 |

### 请求体

| 名称       | 类型     | 是否必填 | 示例值        | 描述                                                                                                                                                                              |
| -------- | ------ | ---- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| roleName | String | 是    | 服务         | 角色名称。                                                                                                                                                                           |
| groupId  | Number | 是    | 1507113595 | 角色组ID。   - 如果要加入的角色组已存在，调用[获取角色列表](/zh/open/development/obtains-a-list-of-enterprise-roles)接口获取。 - 如果尚未创建角色组，先调用[创建角色组](/zh/open/development/add-a-role-group)接口创建角色组，并获取角色组ID。 |

### 请求示例

```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("测试1");
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);
```

## 响应

### 响应体

| 名称      | 类型     | 示例值        | 描述     |
| ------- | ------ | ---------- | ------ |
| roleId  | Number | 1688611118 | 角色ID。  |
| errcode | Number | 0          | 返回码。   |
| errmsg  | String | ok         | 返回码描述。 |

### 响应体示例

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

### 错误码

若调用该接口报错，可根据错误信息在[全局错误码](/zh/open/development/server-api-error-codes-1)文档中查找解决方案。

| 错误码（errcode） | 错误码描述（errmsg） | 解决方案                |
| ------------ | ------------- | ------------------- |
| 34018        | 角色不能为空        | 请填上roleName和groupId |
| 34020        | 无效的groupId    | 请检查groupId是否正确      |
| 34021        | 角色名称已存在       | 请检查roleName是否正确     |
| -1           | 系统繁忙          | 请稍后再试               |
