---
sidebar:
  hidden: true
title: roboto.domain.tokens.token
---
## Module Contents

### Token

```python
class roboto.domain.tokens.token.Token(
    record: roboto.domain.tokens.record.TokenRecord,
    roboto_client: Optional[roboto.http.RobotoClient] = None,
)
```

`from roboto.domain.tokens import Token`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/tokens/token.py#L17-L212)

A personal access token: a credential that authenticates requests to Roboto as the user who owns it.

Create a personal access token with `create`, and a device's token with [`create_token()`](/docs/reference/python-sdk/roboto/domain/devices/device#roboto.domain.devices.device.Device.create_token). Creating a token returns its secret, the value that authenticates requests, once; Roboto does not store it, so a lost secret cannot be recovered. Delete the token and create another.

Get an existing `Token` from `from_id` or `for_self`. To wrap a [`TokenRecord`](/docs/reference/python-sdk/roboto/domain/tokens/record#roboto.domain.tokens.record.TokenRecord) already fetched from Roboto, build a `Token` from it directly.

**Parameters**

- **record** (`roboto.domain.tokens.record.TokenRecord`): Record of the token.
- **roboto_client** (`Optional[roboto.http.RobotoClient]`): Client to make requests to Roboto with. Defaults to one configured from the environment, by [`from_env()`](/docs/reference/python-sdk/roboto/http/roboto_client#roboto.http.roboto_client.RobotoClient.from_env).

**Properties**

- **Token.record** (`roboto.domain.tokens.record.TokenRecord`): Record of the token that this instance holds.

- **Token.token_id** (`str`): Unique ID of the token, beginning with `to_`.

  It returns [`token_id`](/docs/reference/python-sdk/roboto/domain/tokens/record#roboto.domain.tokens.record.TokenContext.token_id) from the record. Reading it raises `ValueError` when the record has no `context`. Roboto sets it in every response, so this happens only for a record built some other way.

- **Token.user_id** (`str`): ID of the user who owns the token, which for a person is their email address.

  It returns [`user_id`](/docs/reference/python-sdk/roboto/domain/tokens/record#roboto.domain.tokens.record.TokenRecord.user_id). Reading it raises `ValueError` when the record has no `user_id`. Roboto sets it in every response, so this happens only for a record built some other way.

#### Token.create()

```python
@classmethod
def create(
    name: str,
    description: Optional[str] = None,
    expiry_days: int = 30,
    api_scopes: Optional[set[roboto.auth.scope.ApiScope]] = None,
    roboto_client: Optional[roboto.http.RobotoClient] = None,
) -> tuple[Token, str]
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/tokens/token.py#L38-L87)

Create a personal access token for the caller.

The token authenticates requests as the calling user. Roboto rejects the request when a device or an action's invocation makes it.

**Parameters**

- **name** (`str`): Human-readable name of the token.
- **description** (`Optional[str]`): Human-readable description of the token.
- **expiry_days** (`int`): Number of days the token stays valid, counted from its creation.
- **api_scopes** (`Optional[set[roboto.auth.scope.ApiScope]]`): API scopes the token is limited to. `None` or an empty set grants every scope.
- **roboto_client** (`Optional[roboto.http.RobotoClient]`): Client to make requests to Roboto with. Defaults to one configured from the environment, by [`from_env()`](/docs/reference/python-sdk/roboto/http/roboto_client#roboto.http.roboto_client.RobotoClient.from_env).

**Returns**

- `tuple[Token, str]`: `(token, secret)`: the new token and its secret. Roboto returns the secret only in this response, so save it now.

**Raises**

- [`RobotoUnauthorizedException`](/docs/reference/python-sdk/roboto/exceptions/domain#roboto.exceptions.domain.RobotoUnauthorizedException): The token would hold a scope the credential making the request does not. `None` asks for every scope, so a token limited to some scopes must name the scopes when it creates another.

**Usage**

Create a token that expires in 90 days, and keep its secret:

```python
token, secret = Token.create(name="ci-uploads", expiry_days=90)
```

#### Token.delete()

```python
def delete() -> None
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/tokens/token.py#L169-L177)

Delete the token, so its secret no longer authenticates requests.

Deleting a token that no longer exists succeeds and changes nothing.

**Usage**

```python
Token.from_id("to_12345").delete()
```

**Returns**

- `None`

#### Token.disable()

```python
def disable() -> Token
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/tokens/token.py#L179-L189)

Disable the token, so Roboto rejects requests made with it until it is enabled again.

**Returns**

- `Token`: This instance. Its record is unchanged until you fetch it again.

**Usage**

```python
token = Token.from_id("to_12345").disable()
```

#### Token.enable()

```python
def enable() -> Token
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/tokens/token.py#L191-L201)

Enable a disabled token, so it authenticates requests again until it expires.

**Returns**

- `Token`: This instance. Its record is unchanged until you fetch it again.

**Usage**

```python
token = Token.from_id("to_12345").enable()
```

#### Token.for_self()

```python
@classmethod
def for_self(
    roboto_client: Optional[roboto.http.RobotoClient] = None,
) -> collections.abc.Collection[Token]
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/tokens/token.py#L111-L129)

List the tokens that authenticate as the calling user.

The tokens returned carry no secret.

**Parameters**

- **roboto_client** (`Optional[roboto.http.RobotoClient]`): Client to make requests to Roboto with. Defaults to one configured from the environment, by [`from_env()`](/docs/reference/python-sdk/roboto/http/roboto_client#roboto.http.roboto_client.RobotoClient.from_env).

**Returns**

- `collections.abc.Collection[Token]`

**Usage**

Print the name and expiry of each of your tokens:

```python
for token in Token.for_self():
    print(token.record.context.name, token.record.context.expires)
```

#### Token.from_id()

```python
@classmethod
def from_id(
    token_id: str,
    roboto_client: Optional[roboto.http.RobotoClient] = None,
) -> Token
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/tokens/token.py#L90-L108)

Fetch a token by its ID.

The token returned carries no secret.

**Parameters**

- **token_id** (`str`): Unique ID of the token, beginning with `to_`.
- **roboto_client** (`Optional[roboto.http.RobotoClient]`): Client to make requests to Roboto with. Defaults to one configured from the environment, by [`from_env()`](/docs/reference/python-sdk/roboto/http/roboto_client#roboto.http.roboto_client.RobotoClient.from_env).

**Raises**

- [`RobotoNotFoundException`](/docs/reference/python-sdk/roboto/exceptions/domain#roboto.exceptions.domain.RobotoNotFoundException): No token has this ID.

**Returns**

- `Token`

**Usage**

```python
token = Token.from_id("to_12345")
```

#### Token.to_dict()

```python
def to_dict() -> dict[str, Any]
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/tokens/token.py#L203-L212)

Return the token's record as a JSON-compatible dictionary.

**Returns**

- `dict[str, Any]`: [`TokenRecord`](/docs/reference/python-sdk/roboto/domain/tokens/record#roboto.domain.tokens.record.TokenRecord) of the token, as a JSON-compatible dictionary.

**Usage**

```python
token_dict = Token.from_id("to_12345").to_dict()
```
