# Change an employee's role

Source: [Change an employee's role](<https://docs.gptunnel.ru/en/access/change-role/>)

<a id="_top"></a>
> **Note: Organization API key**
>
> Use an API key for your organization with the Full dashboard access permission. The key must have been created by an owner or administrator. [Get an API key](<https://docs.gptunnel.ru/en/>).

<a id="change-of-role"></a>
## Change of Role

`POST https://gptunnel.ru/api/v1/access/changeRole`

Assign a role to each employee by login. Send an array of up to 25 `{ login, role }` pairs. Each item is processed independently, so a failed item does not affect the rest. If a login appears more than once, the first entry is used.

<a id="roles-and-permissions"></a>
## Roles and permissions

The main organization roles in descending order are **owner**, **admin**, and **member**.

The **manager** role is scoped to a team. In the dashboard, managers can add or remove members of their own teams and configure their access. Those rights do not extend to other teams. A manager key cannot change roles and receives HTTP `403`. Through the API, managers can only configure access and limits for their own teams and their members, and can only make rules stricter. See [Access management](<https://docs.gptunnel.ru/en/access/>). Owners and admins can assign the manager role and change a manager’s role.

Role changes use the permissions of the person who created the API key. The key can act on employees with a role equal to or lower than the creator’s role. It cannot change the creator’s own role.

| Key creator’s role | Whose role it can change | Roles it can assign |
| --- | --- | --- |
| **owner** | owner, admin, member, manager | admin / member / manager |
| **admin** | admin, member, manager | admin / member / manager |
| **member**, **manager** | none | — |

- A login with a higher role returns `forbidden`. An owner can change another owner’s role, and an admin can change another admin’s role.
- The key creator’s own login returns `forbidden`.
- An item requesting `role = owner` returns `forbidden` for any key. The remaining items are still processed. Ownership can be transferred only in the web interface.
- Assigning the same role again is idempotent and is counted in `changed`.

> **Danger: Key permission**
>
> Enable **Full dashboard access** in the API key settings. A key without this permission receives HTTP `403` with code `31`.
>
>
>
> The key uses its creator’s privileges, even when held by someone else. Share this permission only with trusted integrations. A holder can raise the role of their own account if that account did not create the key and its current role is no higher than the key creator’s role.

> **Caution: Business organizations only**
>
> Personal accounts and keys with the member or manager role receive HTTP `403`.

### Request headers

| Parameter | Type | Description |
| --- | --- | --- |
| `Authorization` (required) | `string` | API key |

### Request parameters

| Parameter | Type | Description |
| --- | --- | --- |
| `items` (required) | `object[]` | Array of 1–25 items. Each item contains { login, role }. |
| `items[].login` (required) | `string` | Employee login |
| `items[].role` (required) | `string` | Assignable roles: admin, member, or manager. The schema accepts owner, but that item returns forbidden. |

In response: `changed` — number of processed logins, including those already assigned the requested role, `failed` - the number of failed items, `failedLogins` - A list of errors (cause and explanation for each).

Error reasons in `failedLogins`:

### Error reasons

| Parameter | Type | Description |
| --- | --- | --- |
| `not_found` | `string` | No employee in the organization has this login |
| `forbidden` | `string` | The target role is higher than the key creator’s role, the target login is the creator’s own login, or the requested role is owner |
| `error` | `string` | An unexpected error occurred while processing this employee |

<a id="example-request"></a>
### Example request

Set `GPTUNNEL_API_KEY` and replace the placeholders with values for your organization.

<a id="tab-panel-0-0"></a>
**Python**

```python
import json
import os
from urllib.request import Request, urlopen

key = os.environ.get("GPTUNNEL_API_KEY")
if not key:
    raise SystemExit("Set GPTUNNEL_API_KEY")

body = {
  "items": [
    {
      "login": "ivanov",
      "role": "admin"
    },
    {
      "login": "petrov",
      "role": "member"
    }
  ]
}
request = Request(
    "https://gptunnel.ru/api/v1/access/changeRole",
    method="POST",
    headers={
        "Authorization": key,
        "Content-Type": "application/json",
    },
    data=json.dumps(body).encode("utf-8"),
)
with urlopen(request, timeout=30) as response:
    print(json.load(response))
```

<a id="tab-panel-0-1"></a>
**cURL**

```bash
curl --request POST \
  --url 'https://gptunnel.ru/api/v1/access/changeRole' \
  --header "Authorization: ${GPTUNNEL_API_KEY:?Set GPTUNNEL_API_KEY}" \
  --header 'Content-Type: application/json' \
  --data '{
  "items": [
    {
      "login": "ivanov",
      "role": "admin"
    },
    {
      "login": "petrov",
      "role": "member"
    }
  ]
}'
```

<a id="tab-panel-0-2"></a>
**JavaScript (Node.js)**

```javascript
const key = process.env.GPTUNNEL_API_KEY
if (!key) throw new Error("Set GPTUNNEL_API_KEY")

const body = {
  "items": [
    {
      "login": "ivanov",
      "role": "admin"
    },
    {
      "login": "petrov",
      "role": "member"
    }
  ]
}
const response = await fetch("https://gptunnel.ru/api/v1/access/changeRole", {
  method: "POST",
  headers: {
    Authorization: key,
    "Content-Type": "application/json",
  },
  body: JSON.stringify(body),
})
if (!response.ok) throw new Error(await response.text())
console.log(await response.json())
```

<a id="example-response"></a>
### Example response

<a id="tab-panel-1-0"></a>
**200 OK**

```json
{
  "code": 0,
  "changed": 1,
  "failed": 1,
  "failedLogins": [
    {
      "login": "petrov",
      "reason": "not_found",
      "message": "Employee not found in the organization."
    }
  ]
}
```

<a id="tab-panel-1-1"></a>
**400 Invalid input**

```json
{
  "code": 3,
  "message": "Invalid input, check schema issues.",
  "details": []
}
```

<a id="tab-panel-1-2"></a>
**401 Unauthorized**

```json
{
  "error": {
    "message": "Unauthorized",
    "type": "unauthorized_request_error",
    "param": null,
    "code": null
  }
}
```

<a id="tab-panel-1-3"></a>
**403 Forbidden**

```json
{
  "code": 7,
  "message": "Not allowed to perform this action for the organization."
}
```

<a id="tab-panel-1-4"></a>
**403 Personal account**

```json
{
  "code": 13,
  "message": "This section is not available from a personal account. Use a business organization API key."
}
```

<a id="tab-panel-1-5"></a>
**500 Server error**

```json
{
  "code": 1,
  "message": "Internal server error.",
  "trace_id": "66a346f64dcb57627c03d596"
}
```
