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

# ロールを作成する

> 新規ロールを作成し、ロールグループに追加して企業の権限を管理できます

このインターフェースを呼び出して、新しいロールを作成します。

## リクエスト

| **基本情報**      |                                                                                   |
| ------------- | --------------------------------------------------------------------------------- |
| 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 を取得する](/ja/open/development/obtain-orgapp-token)インターフェースで取得します。 |

### リクエストボディ

| 名前       | タイプ    | 必須 | 例          | 説明                                                                                                                                                                                                                                                              |
| -------- | ------ | -- | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| roleName | String | はい | サービス       | ロール名です。                                                                                                                                                                                                                                                         |
| groupId  | Number | はい | 1507113595 | ロールグループ ID です。   - 追加先のロールグループが既に存在する場合は、[ロールリストを取得する](/ja/open/development/obtains-a-list-of-enterprise-roles)インターフェースを呼び出して取得します。 - ロールグループがまだ作成されていない場合は、まず[ロールグループを作成する](/ja/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"
}
```

### エラーコード

このインターフェースの呼び出しでエラーが発生した場合は、エラーメッセージをもとに[グローバルエラーコード](/ja/open/development/server-api-error-codes-1)ドキュメントで解決策を検索してください。

| エラーコード（errcode） | エラーコードの説明（errmsg） | 解決策                          |
| --------------- | ----------------- | ---------------------------- |
| 34018           | ロールを空にすることはできません  | roleName と groupId を入力してください |
| 34020           | 無効な groupId です    | groupId が正しいかご確認ください         |
| 34021           | ロール名が既に存在します      | roleName が正しいかご確認ください        |
| -1              | システムビジー           | 後でもう一度お試しください                |
