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

# Fine-grained authorization

> Give people roles on specific parts of your organization, like a region or a branch, and ask Aiveth whether they may act on one.

A person's role applies across the whole organization. Sometimes that isn't enough: a branch approver should approve payouts for their branch, not every branch. Fine-grained authorization lets you give a role on one **part** of your organization. It covers that part and everything under it, never the parts beside it.

> [!NOTE]
>
> Fine-grained authorization is available on the Business plan and above. Reading and the check keep working after a move to a lower plan, and so does removing things.

## The model

* **Types of part** form a tree, for example `region` → `branch`. Each type has a key your apps use (fixed once made), a name and one parent type. The top type sits under your organization. You can have up to 20 types, at most 4 levels deep.
* **Roles for parts** are made for one type, for example `branch_approver` on each `branch`. A role for parts carries your [app permissions](https://docs.aiveth.com/access/roles-and-permissions.md#app-permissions) only, never Aiveth powers, so it never makes anyone an admin. Its key must differ from your whole-organization role keys. You can have up to 100.
* **Parts** are the actual regions, branches or projects. Each has your own id (`external_id`, 1 to 128 characters), a name, and a parent part of the parent type. An organization can have up to 50,000 parts.
* **Access** gives a role for parts on one part to a person or a group. A group's access reaches every member, including people who join later.

Types and roles for parts are made in the console. Parts and access can be managed in the console or from your server through the admin API.

## Set it up in the console

**1. Add types**

Go to **Parts & access** and choose **Type**. Start at the top, like region, then add the types under it, like branch.

**2. Add roles for parts**

Choose **Role**, pick the type it applies to, and tick the app permissions it carries, like `claim:approve`.

**3. Add parts**

Choose **Part**, or let your app register parts as it makes them (below).

**4. Give access**

Open a part and give a role on it to a person or a group.

Changing this needs the **Manage roles** power. Seeing it needs **See people**.

## The check

Your server asks: may this person do this permission on this part? The answer is yes when either:

1. the person's whole-organization role carries the permission (what their token already says), or
2. the person, or a group they are in, holds a role for parts carrying the permission on this part or any part above it.

Everything else is no, including an unknown part, another organization's part, and a suspended or removed person. A malformed person id is also simply no.

The answer says why it allowed the request in `via`:

```json theme={null}
{ "allowed": true, "via": { "kind": "resource_role", "role": "branch_approver", "resource_type": "branch", "external_id": "mumbai-01", "group": "Mumbai Branch" } }
```

`kind` is `organization_role` (with `role`) or `resource_role`. `group` appears only when the access came through a group. `via` is `null` when `allowed` is `false`.

## Admin API

Your server calls the admin API with an [API key](https://docs.aiveth.com/account/api-keys.md) in the `Authorization: Bearer aiveth_sk_...` header. Each endpoint needs one of these key powers:

| Key power | Allows | The key's maker needs |
| - | - | - |
| `authorization:read` | Check who may do what on a part, and list who reaches what. | See people |
| `resources:write` | Register, rename, move and remove parts. | Manage roles |
| `authorization:write` | Give and take roles on parts (never to the key's maker). | Manage roles |

| Method and path | Key power |
| - | - |
| `POST /v1/admin/authorization/check` | `authorization:read` |
| `GET /v1/admin/authorization/people/{id}/resources?permission=&type=` | `authorization:read` |
| `GET /v1/admin/authorization/people/{id}/access` | `authorization:read` |
| `GET /v1/admin/authorization/resources?type=&external_id=&parent_id=&search=&offset=` | `authorization:read` |
| `GET /v1/admin/authorization/resources/{id}` | `authorization:read` |
| `GET /v1/admin/authorization/resources/{id}/people?permission=` | `authorization:read` |
| `POST /v1/admin/authorization/resources` | `resources:write` |
| `PATCH /v1/admin/authorization/resources/{id}` | `resources:write` |
| `DELETE /v1/admin/authorization/resources/{id}?everything_below=true` | `resources:write` |
| `POST /v1/admin/authorization/access` | `authorization:write` |
| `DELETE /v1/admin/authorization/access/{id}` | `authorization:write` |

### Register a part

**type** `string` *required*

The type key, like `branch`.

**external\_id** `string` *required*

Your own id for the part.

**name** `string` *required*

Up to 200 characters.

**parent\_type** `string`

The parent part's type. Required unless the type sits at the top.

**parent\_external\_id** `string`

The parent part's id.

### Give access

**resource\_type** `string` *required*

The part's type key.

**resource\_external\_id** `string` *required*

The part's id.

**role** `string` *required*

The role for parts. It must be made for this part's type.

**person\_id** `string`

The person to give it to. Send this or `group_id`, not both.

**group\_id** `string`

The group to give it to.

### Examples

```bash cURL theme={null}
curl -X POST https://accounts.aiveth.com/v1/admin/authorization/check \
  -H "Authorization: Bearer aiveth_sk_live_..." \
  -H "Content-Type: application/json" \
  -d '{"person_id":"PERSON_ID","permission":"payout:approve","resource_type":"branch","resource_external_id":"mumbai-01"}'
```

```ts Node theme={null}
import { Aiveth } from '@gooclaim/aiveth';

const aiveth = new Aiveth({ baseUrl: 'https://accounts.aiveth.com', apiKey: process.env.AIVETH_API_KEY });

await aiveth.authorization.register({ type: 'branch', external_id: 'mumbai-01', name: 'Mumbai', parent_type: 'region', parent_external_id: 'west' });
await aiveth.authorization.give('branch', 'mumbai-01', 'branch_approver', { person_id: personId });

const { allowed } = await aiveth.authorization.check(personId, 'payout:approve', 'branch', 'mumbai-01');
```

The Node SDK also has `resourcesFor`, `accessOf`, `find`, `get`, `peopleWith`, `change`, `remove` and `take`. See [Node SDK](https://docs.aiveth.com/sdks/node.md).

## Rules

* Nobody gives access to themselves, or to a group they are in, unless they are an Owner. An API key never gives access to its maker.
* A group that holds roles on parts hands them to anyone who joins, so changing its members needs **Manage roles**, and each change is recorded.
* A part can move only to a new parent of the parent type. Removing a part is refused while parts sit under it, unless you pass `everything_below=true`.
* Nothing is removed while something stands on it: a type with parts, child types or roles; a role anyone holds; a group that holds roles on parts.
* Lists of parts return at most 500 at a time.
* Every change is recorded in the activity log and sends a [webhook](https://docs.aiveth.com/events/webhooks.md).

## Errors

| Status | Example message |
| - | - |
| 403 | You can't give access to yourself — ask an Owner |
| 403 | You can't give access to a group you are in — ask an Owner |
| 404 | No such part |
| 409 | A part of this type with that id already exists |
| 409 | They already hold this role on this part |
| 422 | Name its parent part (parent\_type and parent\_external\_id) |
| 422 | Parts go at most 4 levels deep |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.