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

# Membuat peran

> Panggil API ini untuk membuat peran baru dalam grup peran tertentu di DingTalk, lalu tetapkan anggota ke peran tersebut dan konfigurasikan izin sesuai kebutuhan organisasi.

Panggil API ini untuk membuat peran baru.

## Permintaan

| **Informasi dasar**         |                                                                                   |
| --------------------------- | --------------------------------------------------------------------------------- |
| URL HTTP                    | [https://oapi.dingtalk.io/role/add\_role](https://oapi.dingtalk.io/role/add_role) |
| Metode HTTP                 | POST                                                                              |
| Tipe aplikasi yang didukung | appType-Internal app                                                              |
| Izin yang diperlukan        | permission-qyapi\_manage\_addresslist-Contacts data management permission         |

### Parameter kueri

| Nama          | Tipe   | Wajib | Contoh   | Deskripsi                                                                                                                                                                                    |
| ------------- | ------ | ----- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| access\_token | String | Ya    | 6d1bxxxx | Kredensial aplikasi yang digunakan untuk memanggil API ini. Dapatkan dengan memanggil API untuk memperoleh [access\_token dari aplikasi internal](/id/open/development/obtain-orgapp-token). |

### Isi permintaan

| Nama     | Tipe   | Wajib | Contoh     | Deskripsi                                                                                                                                                                                                                                                                                                                                      |
| -------- | ------ | ----- | ---------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| roleName | String | Ya    | Layanan    | Nama peran.                                                                                                                                                                                                                                                                                                                                    |
| groupId  | Number | Ya    | 1507113595 | ID grup peran.   - Jika grup peran yang dituju sudah ada, panggil API [Memperoleh daftar peran](/id/open/development/obtains-a-list-of-enterprise-roles) untuk mendapatkannya. - Jika grup peran belum dibuat, panggil terlebih dahulu API [Membuat grup peran](/id/open/development/add-a-role-group) untuk membuatnya dan memperoleh ID-nya. |

### Contoh permintaan

```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);
```

## Respons

### Isi respons

| Nama    | Tipe   | Contoh     | Deskripsi                    |
| ------- | ------ | ---------- | ---------------------------- |
| roleId  | Number | 1688611118 | ID peran.                    |
| errcode | Number | 0          | Kode pengembalian.           |
| errmsg  | String | ok         | Deskripsi kode pengembalian. |

### Contoh isi respons

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

### Kode error

Jika terjadi error saat Anda memanggil API ini, temukan solusinya dalam dokumen [Kode error global](/id/open/development/server-api-error-codes-1) berdasarkan pesan error.

| Kode error (errcode) | Pesan error (errmsg)         | Solusi                               |
| -------------------- | ---------------------------- | ------------------------------------ |
| 34018                | The role cannot be empty     | Sertakan roleName dan groupId.       |
| 34020                | Invalid groupId              | Periksa apakah groupId sudah benar.  |
| 34021                | The role name already exists | Periksa apakah roleName sudah benar. |
| -1                   | System busy                  | Coba lagi nanti.                     |
