> For the complete documentation index, see [llms.txt](https://docs.unitlab.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.unitlab.ai/api-sdk-cli/get-started/authentication-and-api-keys.md).

# Authentication and API keys

Create a workspace API key, limit what it can do, and configure the SDK, CLI and HTTP clients to use it.

The SDK, CLI and HTTP API authenticate with a **workspace API key**. A key works only in the workspace that created it and can be limited to the permissions a job needs.

{% hint style="info" %}
AI assistants connected through [Unitlab MCP](https://docs.unitlab.ai/api-sdk-cli/mcp/overview.md) sign in with OAuth and do not use API keys.
{% endhint %}

## Before you begin

* Your role needs the **Manage API keys** permission. The Owner role has it by default.

## Create an API key

{% stepper %}
{% step %}

### Open the API keys page

In the app, open **Settings** > **API keys**, then click **Create new key**.

![API keys page with three enabled keys and the Create new key button](https://1625055228-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1fWt922WFnUJmXYAzZfh%2Fuploads%2FdshI4k6EIbe2fWrKDhuH%2Fapi-keys-page.webp?alt=media)
{% endstep %}

{% step %}

### Name the key and choose its permissions

Give the key a name that says where it runs, such as `CI upload script`. Every permission your role has is switched on. Switch off anything the job does not need.

![Create API key dialog with a name and permission switches](https://1625055228-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1fWt922WFnUJmXYAzZfh%2Fuploads%2F0yYCyFbNjos4X1bo8d1r%2Fapi-keys-create-dialog.webp?alt=media)
{% endstep %}

{% step %}

### Create and copy the key

Click **Create key**. The key appears in the list. Click the eye icon to show it or the copy icon to copy it, then store it in your secret manager.
{% endstep %}
{% endstepper %}

The **Access** column shows **Full access** or the number of permissions the key has. Hover it to see their names.

## Choose permissions for a key

A key with every switch on has **Full access**. A key with some switches off is refused, with HTTP 403, on any route that needs a missing permission.

| Permission              | Lets the key                                                                                                      |
| ----------------------- | ----------------------------------------------------------------------------------------------------------------- |
| Manage projects         | Create, rename and delete projects; attach library data                                                           |
| Manage data             | Upload, import, manage Assets, folders, datasets and embeddings; detach sources; run workflow actions on any task |
| View labeling interface | Read tasks, annotations and project data                                                                          |
| Create labels           | Save annotations, import labels, report Agent results                                                             |
| Assign project members  | Assign, move, release and prioritize tasks                                                                        |
| View ontology           | Read ontologies                                                                                                   |
| Manage ontologies       | Create and edit ontologies                                                                                        |
| Manage releases         | List, create and download releases                                                                                |
| Manage cloud storage    | List and browse cloud storage connections                                                                         |
| View statistics         | Read statistics                                                                                                   |
| View team statistics    | Include the team's numbers in statistics                                                                          |

Common minimal sets:

| Job                                    | Turn on                                       |
| -------------------------------------- | --------------------------------------------- |
| Upload data into existing projects     | Manage data                                   |
| Export releases nightly                | Manage releases                               |
| Import model predictions               | Manage data, Create labels                    |
| Agent stage service                    | View labeling interface, Create labels        |
| Full pipeline (create, upload, export) | Manage projects, Manage data, Manage releases |

You can only give a key permissions your own role has. You can see, edit and delete only keys within your role's permissions.

## Manage existing keys

| Task                       | How                                                                     |
| -------------------------- | ----------------------------------------------------------------------- |
| Pause a key                | Switch **Status** off. The key is refused until you switch it on again. |
| Change permissions or name | Open the row menu (**⋮**) > **Edit permissions**.                       |
| Revoke a key               | Row menu > **Delete**. This cannot be undone.                           |
| Rotate a key               | Create a new key, deploy it, then delete the old one.                   |

A key keeps working after the member who created it leaves the workspace. Two actions still check the creator: deleting a project needs a key created by the workspace Owner or the project's creator, and committing an annotation import needs a key created by a member.

## Configure the client

The SDK and CLI read the key and API URL in this order. The first value found wins.

| Order   | API key                                       | API URL                                |
| ------- | --------------------------------------------- | -------------------------------------- |
| 1       | `UnitlabClient(api_key=...)` or `--api-key`   | `UnitlabClient(api_url=...)`           |
| 2       | `UNITLAB_API_KEY` environment variable        | `UNITLAB_API_URL` environment variable |
| 3       | Saved by `unitlab configure`                  | Saved by `unitlab configure`           |
| Default | None: the client raises `AuthenticationError` | `https://api.unitlab.ai`               |

{% tabs %}
{% tab title="Environment variables" %}
Recommended for CI, containers and servers.

```bash
export UNITLAB_API_KEY="YOUR_API_KEY"
# Only for a dedicated deployment:
# export UNITLAB_API_URL="https://api.example-deployment.com"
```

{% endtab %}

{% tab title="Saved configuration" %}
Convenient on a workstation. The values are saved in `~/.unitlab/credentials`, readable only by your user.

```bash
unitlab configure --api-key "YOUR_API_KEY"
unitlab configure --api-url "https://api.unitlab.ai"
```

{% endtab %}

{% tab title="In code" %}
Use when your application loads secrets itself.

```python
import os
from unitlab import UnitlabClient

client = UnitlabClient(api_key=os.environ["MY_SECRET_STORE_KEY"])
```

{% endtab %}

{% tab title="HTTP" %}
Send the key in the `Authorization` header with the `Api-Key` scheme.

```bash
curl https://api.unitlab.ai/api/sdk/projects/ \
  -H "Authorization: Api-Key $UNITLAB_API_KEY"
```

{% endtab %}
{% endtabs %}

## Check the connection

```bash
unitlab project list
```

The command prints your projects, or nothing in an empty workspace. In Python:

```python
from unitlab import UnitlabClient

client = UnitlabClient()
print([project.name for project in client.projects.list()])
client.close()
```

| Result                        | Fix                                                                                                       |
| ----------------------------- | --------------------------------------------------------------------------------------------------------- |
| `Error: No API key provided`  | Set `UNITLAB_API_KEY` or run `unitlab configure --api-key`.                                               |
| `AuthenticationError`         | The key is wrong, deleted or switched off.                                                                |
| `PermissionDeniedError` (403) | The key lacks a permission for this action. Edit its permissions.                                         |
| `SubscriptionError` (403)     | The workspace plan or quota does not allow the action.                                                    |
| Saved values have no effect   | An environment variable or argument takes precedence. Check for `UNITLAB_` variables in your environment. |

## Keep keys safe

* Store keys in a secret manager or CI secret, never in code or notebooks.
* Use one key per job so you can revoke one without stopping the others.
* Give each key only the permissions its job needs.
* Delete keys you no longer use. The workspace [audit log](https://docs.unitlab.ai/documentation/security/audit-log) records key creation, changes and reveals.

## Related

[Install the SDK and CLI](https://docs.unitlab.ai/api-sdk-cli/get-started/install-the-python-sdk-and-cli.md) · [Errors, rate limits and pagination](https://docs.unitlab.ai/api-sdk-cli/apis/errors-rate-limits-and-pagination.md) · [Python SDK quickstart](https://docs.unitlab.ai/api-sdk-cli/get-started/python-sdk-quickstart.md)
