> For the complete documentation index, see [llms.txt](https://v2.dataos.info/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://v2.dataos.info/references/interfaces/apis/core-apis/api-reference/user-tokens.md).

# User tokens

Create, list, retrieve, and delete user API tokens through platform-api to automate token management.

Create and manage API tokens for a DataOS user programmatically, without using the **API Tokens** panel or the CLI. Use these endpoints to automate token provisioning in onboarding, deployment, or CI workflows. For how tokens work, including scope and expiry, see [API tokens](https://v2.dataos.info/references/key-concepts/api-tokens.md).

| Method   | Path                                      | Use it to            |
| -------- | ----------------------------------------- | -------------------- |
| `POST`   | `/v1/users/{user_id}/tokens`              | Create a token       |
| `GET`    | `/v1/users/{user_id}/tokens`              | List a user's tokens |
| `GET`    | `/v1/users/{user_id}/tokens/{token_name}` | Retrieve one token   |
| `DELETE` | `/v1/users/{user_id}/tokens/{token_name}` | Delete a token       |

All paths are relative to `$BASE_URL` (`<DATAOS_FQDN>/platform-api`). See [Authentication](https://v2.dataos.info/references/interfaces/apis/core-apis/authentication.md).

{% hint style="warning" %}
Tokens can't be updated after creation. You can't change a token's name, description, or expiry. To change any of these, delete the token and create a new one.
{% endhint %}

### Authentication and access

Authenticate each request with an `apikey` header (or a Bearer token). You can manage only the tokens of the user that your API key belongs to. If `user_id` in the path is a different user, the request returns `403 Forbidden`.

### Path Parameters

<table><thead><tr><th width="137.524169921875">Name</th><th width="136.20068359375">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>user_id</code></td><td>string</td><td>Required. DataOS user ID, e.g. <code>iamgroot</code></td></tr><tr><td><code>token_name</code></td><td>string</td><td>Required for retrieve and delete. Token name, e.g. <code>cli-token</code></td></tr></tbody></table>

### Headers

<table><thead><tr><th width="225.6763916015625">Name</th><th>Value</th></tr></thead><tbody><tr><td><code>apikey</code> or <code>Authorization</code></td><td><code>&#x3C;your-apikey></code> or <code>Bearer &#x3C;access_token></code>. Required when gateway auth is enabled. See <a href="https://v2.dataos.info/references/interfaces/apis/core-apis/authentication.md">Authentication</a></td></tr><tr><td><code>Accept</code></td><td><code>application/json</code> (recommended)</td></tr><tr><td><code>Content-Type</code></td><td><code>application/json</code>. Required for create</td></tr><tr><td><code>Dataos-Correlation-Id</code></td><td>Optional tracing ID. Echoed back in the response when provided</td></tr></tbody></table>

## Create a token

`POST` `/v1/users/{user_id}/tokens`

Creates an API token for the user. The token value is returned only in this response.

### Request Body

<table><thead><tr><th width="168.124267578125">Field</th><th width="113.1383056640625">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>name</code></td><td>string</td><td>Required. Token name. Use a name you'll recognize later, because you retrieve and delete tokens by name</td></tr><tr><td><code>description</code></td><td>string</td><td>Token description</td></tr><tr><td><code>duration</code></td><td>string</td><td>How long the token stays valid, e.g. <code>24h</code> or <code>720h</code> (30 days)</td></tr><tr><td><code>type</code></td><td>string</td><td>Token type, e.g. <code>apikey</code></td></tr><tr><td><code>use_existing</code></td><td>boolean</td><td>If <code>true</code> and a token with the same name already exists, return that token instead of creating a new one</td></tr><tr><td><code>user_id</code></td><td>string</td><td>User ID. Optional; the <code>user_id</code> in the path always takes precedence</td></tr></tbody></table>

**cURL:**

{% code title="Create a 30-day token" expandable="true" collapsedlinecount="5" %}

```bash
curl -X POST \
  "https://$BASE_URL/v1/users/iamgroot/tokens" \
  -H "Accept: application/json" \
  -H "Content-Type: application/json" \
  -H "apikey: <your-apikey>" \
  -d '{
    "name": "cli-token",
    "description": "Personal access token",
    "duration": "720h",
    "type": "apikey"
  }'
```

{% endcode %}

### Response

{% tabs %}
{% tab title="201" %}
{% code expandable="true" collapsedlinecount="5" %}

```json
{
  "name": "cli-token",
  "description": "Personal access token",
  "type": "apikey",
  "user_id": "iamgroot",
  "expiration": "2026-10-25T12:00:00Z",
  "data": {
    "apikey": "<generated-token>"
  }
}
```

{% endcode %}

`data.apikey` holds the token value. `expiration` is when the token stops working.

{% hint style="warning" %}
Copy and store `data.apikey` securely right away. The token value is returned only when you create the token; list and retrieve don't return it.
{% endhint %}
{% endtab %}

{% tab title="400" %}
{% code expandable="true" collapsedlinecount="5" %}

```json
{
  "status": 400,
  "error": { "code": "...", "message": "..." }
}
```

{% endcode %}

Invalid request body, for example a missing `name`.
{% endtab %}

{% tab title="401" %}
{% code expandable="true" collapsedlinecount="5" %}

```json
{
  "status": 401,
  "error": { "code": "COMMON.AUTH.UNAUTHORIZED", "message": "..." }
}
```

{% endcode %}

Missing or invalid API key.
{% endtab %}

{% tab title="403" %}
{% code expandable="true" collapsedlinecount="5" %}

```json
{
  "status": 403,
  "error": { "code": "COMMON.AUTH.FORBIDDEN", "message": "..." }
}
```

{% endcode %}

The API key is valid but belongs to a different user than `user_id`.
{% endtab %}
{% endtabs %}

## List tokens

`GET` `/v1/users/{user_id}/tokens`

Lists the user's active tokens. Expired and deleted tokens don't appear.

**cURL:**

{% code title="List tokens" expandable="true" collapsedlinecount="5" %}

```bash
curl -X GET \
  "https://$BASE_URL/v1/users/iamgroot/tokens" \
  -H "Accept: application/json" \
  -H "apikey: <your-apikey>"
```

{% endcode %}

### Response

{% tabs %}
{% tab title="200" %}
{% code expandable="true" collapsedlinecount="5" %}

```json
[
  {
    "name": "cli-token",
    "description": "Personal access token",
    "type": "apikey",
    "user_id": "iamgroot",
    "expiration": "2026-10-25T12:00:00Z",
    "data": {}
  }
]
```

{% endcode %}

An array of token objects. `data` is empty or omitted, because the token value is not returned after creation.
{% endtab %}

{% tab title="401" %}
{% code expandable="true" collapsedlinecount="5" %}

```json
{
  "status": 401,
  "error": { "code": "COMMON.AUTH.UNAUTHORIZED", "message": "..." }
}
```

{% endcode %}

Missing or invalid API key.
{% endtab %}

{% tab title="403" %}
{% code expandable="true" collapsedlinecount="5" %}

```json
{
  "status": 403,
  "error": { "code": "COMMON.AUTH.FORBIDDEN", "message": "..." }
}
```

{% endcode %}

The API key belongs to a different user than `user_id`.
{% endtab %}

{% tab title="404" %}
{% code expandable="true" collapsedlinecount="5" %}

```json
{
  "status": 404,
  "error": { "code": "...", "message": "..." }
}
```

{% endcode %}

The user doesn't exist.
{% endtab %}
{% endtabs %}

## Retrieve a token

`GET` `/v1/users/{user_id}/tokens/{token_name}`

Returns the metadata for one token: name, description, type, user, and expiration.

**cURL:**

{% code title="Retrieve a token" expandable="true" collapsedlinecount="5" %}

```bash
curl -X GET \
  "https://$BASE_URL/v1/users/iamgroot/tokens/cli-token" \
  -H "Accept: application/json" \
  -H "apikey: <your-apikey>"
```

{% endcode %}

### Response

{% tabs %}
{% tab title="200" %}
{% code expandable="true" collapsedlinecount="5" %}

```json
{
  "name": "cli-token",
  "description": "Personal access token",
  "type": "apikey",
  "user_id": "iamgroot",
  "expiration": "2026-10-25T12:00:00Z",
  "data": {}
}
```

{% endcode %}

Same fields as the create response. `data` is empty or omitted, because the token value is not returned after creation.
{% endtab %}

{% tab title="403" %}
{% code expandable="true" collapsedlinecount="5" %}

```json
{
  "status": 403,
  "error": { "code": "COMMON.AUTH.FORBIDDEN", "message": "..." }
}
```

{% endcode %}

The API key belongs to a different user than `user_id`.
{% endtab %}

{% tab title="404" %}
{% code expandable="true" collapsedlinecount="5" %}

```json
{
  "status": 404,
  "error": { "code": "...", "message": "..." }
}
```

{% endcode %}

The user or token doesn't exist, or the token was deleted.
{% endtab %}
{% endtabs %}

## Delete a token

`DELETE` `/v1/users/{user_id}/tokens/{token_name}`

Deletes the token. Anything using it can no longer authenticate. Deletion can't be undone.

**cURL:**

{% code title="Delete a token" expandable="true" collapsedlinecount="5" %}

```bash
curl -X DELETE \
  "https://$BASE_URL/v1/users/iamgroot/tokens/cli-token" \
  -H "apikey: <your-apikey>"
```

{% endcode %}

### Response

{% tabs %}
{% tab title="204" %}
No content. The token is deleted. A later request to retrieve it returns `404`.
{% endtab %}

{% tab title="403" %}
{% code expandable="true" collapsedlinecount="5" %}

```json
{
  "status": 403,
  "error": { "code": "COMMON.AUTH.FORBIDDEN", "message": "..." }
}
```

{% endcode %}

The API key belongs to a different user than `user_id`.
{% endtab %}

{% tab title="404" %}
{% code expandable="true" collapsedlinecount="5" %}

```json
{
  "status": 404,
  "error": { "code": "...", "message": "..." }
}
```

{% endcode %}

The user or token doesn't exist.
{% endtab %}
{% endtabs %}

## Token expiry

Set `duration` when you create a token, for example `24h` or `720h`. The response's `expiration` field shows when the token expires. After that time:

* The token stops authenticating requests.
* The token no longer appears in [List tokens](#list-tokens).

Because tokens can't be updated, you can't extend an expiry. To rotate a token, create a new one, switch your integrations to it, then delete the old one.

## Errors

All endpoints return errors in this shape:

```json
{
  "status": 403,
  "error": { "code": "COMMON.AUTH.FORBIDDEN", "message": "..." }
}
```

| Status | When it occurs                                                       |
| ------ | -------------------------------------------------------------------- |
| `400`  | The create request body is invalid                                   |
| `401`  | The API key is missing or invalid                                    |
| `403`  | The API key is valid, but belongs to a different user than `user_id` |
| `404`  | The user or token doesn't exist, or the token was deleted            |
| `500`  | Internal error                                                       |
| `502`  | The token service is unavailable. Retry the request                  |

## Notes

* The token value is returned only once, in the create response. Store it securely.
* Tokens can't be updated after creation. Delete and recreate a token to change it.
* These endpoints manage tokens. They don't change how you authenticate to `platform-api`.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://v2.dataos.info/references/interfaces/apis/core-apis/api-reference/user-tokens.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
