|
| 1 | +--- |
| 2 | +title: Access tokens |
| 3 | +short: Access tokens |
| 4 | +tier: enterprise |
| 5 | +type: guide |
| 6 | +order: 381 |
| 7 | +order_enterprise: 359 |
| 8 | +meta_title: Access tokens |
| 9 | +meta_description: Access tokens to interact with the Label Studio API and SDK. |
| 10 | +section: "Manage Your Organization" |
| 11 | +date: 2025-02-18 12:03:59 |
| 12 | +--- |
| 13 | + |
| 14 | +Label Studio has personal access tokens and legacy tokens. The options available to users are set at the Organization level. Se [Access settings for orgs](#Access-token-settings-for-orgs) below. |
| 15 | + |
| 16 | +<table> |
| 17 | +<thead> |
| 18 | + <tr> |
| 19 | + <th>Personal Access Token</th> |
| 20 | + <th>Legacy Token</th> |
| 21 | + </tr> |
| 22 | + </thead> |
| 23 | + <tr> |
| 24 | + <td> |
| 25 | + <ul> |
| 26 | + <li>Have a TTL that can be set at the org level. (Label Studio Enterprise only) |
| 27 | + <li>Are only visible to users once |
| 28 | + <li>Can be manually revoked |
| 29 | + <li>Require extra steps when used with HTTP API |
| 30 | + <li>Only need to be set once when used SDK |
| 31 | + </ul> |
| 32 | + </td> |
| 33 | + <td> |
| 34 | + <ul> |
| 35 | + <li>Does not expire |
| 36 | + <li>Remains listed and available in your account settings |
| 37 | + <li>Can be manually revoked |
| 38 | + <li>Do not need to be refreshed with used with HTTP API |
| 39 | + <li>Only need to be set once when used SDK |
| 40 | + </ul> |
| 41 | + </td> |
| 42 | + </tr> |
| 43 | +</table> |
| 44 | + |
| 45 | +## Personal access tokens and the API |
| 46 | + |
| 47 | +### SDK |
| 48 | + |
| 49 | +Personal access tokens can be used with the Python SDK the same way in which legacy tokens were set: |
| 50 | + |
| 51 | +```python |
| 52 | +# Define the URL where Label Studio is accessible and the API key for your user account |
| 53 | +LABEL_STUDIO_URL = 'http://localhost:8080' |
| 54 | +# API key is available at the Account & Settings > Access Tokens page in Label Studio UI |
| 55 | +API_KEY = 'd6f8a2622d39e9d89ff0dfef1a80ad877f4ee9e3' |
| 56 | + |
| 57 | +# Import the SDK and the client module |
| 58 | +from label_studio_sdk.client import LabelStudio |
| 59 | + |
| 60 | +# Connect to the Label Studio API and check the connection |
| 61 | +ls = LabelStudio(base_url=LABEL_STUDIO_URL, api_key=API_KEY) |
| 62 | + |
| 63 | +``` |
| 64 | + |
| 65 | +### HTTP API |
| 66 | + |
| 67 | +If you are interacting directly via HTTP, the personal access token functions as a JWT refresh token. |
| 68 | + |
| 69 | +You must use your personal access token (refresh token) to generate a short-lived access token. This access token is then used for API authentication. |
| 70 | + |
| 71 | +To generate this access token, make a POST request with your personal access token in the JSON body. For example: |
| 72 | + |
| 73 | +```bash |
| 74 | +curl -X POST <your-label-studio-url>/api/token/refresh \ |
| 75 | +-H "Content-Type: application/json" \ |
| 76 | +-d '{"refresh": "your-personal-access-token"}' |
| 77 | +``` |
| 78 | + |
| 79 | +In response, you will receive a JSON payload similar to: |
| 80 | + |
| 81 | +```json |
| 82 | +{ |
| 83 | + "access": "your-new-access-token" |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +Use this access token by including it in your API requests via the Authorization header: |
| 88 | + |
| 89 | +```http |
| 90 | +Authorization: Bearer your-new-access-token |
| 91 | +``` |
| 92 | + |
| 93 | +When that access token expires (after around 5 minutes) you’ll get a 401 response, and will need to use your personal access token again to acquire a new one. This adds an extra layer of security. |
| 94 | + |
| 95 | +You can also check when the token is set to expire using the following script: |
| 96 | + |
| 97 | +```python |
| 98 | +# pip install pyjwt |
| 99 | +from datetime import datetime, timezone |
| 100 | +import jwt |
| 101 | + |
| 102 | +decoded = jwt.decode(token) |
| 103 | +exp = decoded.get("exp") |
| 104 | +token_is_expired = (exp <= datetime.now(timezone.utc).timestamp()) |
| 105 | +``` |
| 106 | + |
| 107 | +## Access token settings for orgs |
| 108 | + |
| 109 | +The options that are available to users depend on options selected at the organization level. |
| 110 | + |
| 111 | +From the **Organization** page, click **Access Token Settings** in the upper right. |
| 112 | + |
| 113 | +<div class="enterprise-only"> |
| 114 | + |
| 115 | +!!! note |
| 116 | + The **Organization** page is only available to users in the Admin and Owner role. |
| 117 | + |
| 118 | +</div> |
| 119 | + |
| 120 | +From here you can enable and disable token types. |
| 121 | + |
| 122 | +* When a certain token type is disabled, existing tokens will not be able to authenticate to the Label Studio platform. |
| 123 | + |
| 124 | +* Use the Personal Access Token Time-to-Live to set an expiration date for personal access tokens. This is only available for Label Studio Enterprise users. |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | +## Finding your access token |
| 131 | + |
| 132 | +You can create/manage your access token from your [**Account & Settings** page](user_account) (click your username in the upper right in Label Studio). |
| 133 | + |
| 134 | + |
0 commit comments