Implementation:Bentoml BentoML ApiToken DataClass
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Cloud API, Authentication |
| Last Updated | 2026-02-13 15:00 GMT |
Overview
Defines the ApiToken data class and ApiTokenAPI client for managing BentoCloud API tokens through CRUD operations.
Description
The api_token module provides two main classes:
- ApiToken -- An attrs-based data class that extends ApiTokenSchema. It adds a created_by field and provides convenience methods: to_dict (using bentoml_cattr for unstructuring) and to_yaml (for YAML serialization). The class method from_schema converts an ApiTokenSchema (from the REST API response) into an ApiToken instance, mapping all schema fields including name, uid, resource_type, labels, timestamps, description, scopes, user, organization, expiration, and token value.
- ApiTokenAPI -- An attrs-based API client wrapper that holds a reference to a RestApiClient. It provides four operations:
- list(search) -- Lists all API tokens, optionally filtered by a search string.
- create(name, description, scopes, expired_at) -- Creates a new API token with the specified parameters.
- get(token_uid) -- Retrieves a specific token by its UID, returning None if not found.
- delete(token_uid) -- Deletes a token by its UID.
Usage
Use this module to programmatically manage BentoCloud API tokens for authentication, access control, and token lifecycle management.
Code Reference
Source Location
- Repository: Bentoml_BentoML
- File: src/bentoml/_internal/cloud/api_token.py
- Lines: 1-120
Signature
@attr.define(kw_only=True)
class ApiToken(ApiTokenSchema):
created_by: str = attr.field(default="")
def to_dict(self) -> dict[str, t.Any]: ...
def to_yaml(self) -> str: ...
@classmethod
def from_schema(cls, schema: ApiTokenSchema) -> ApiToken: ...
@attr.define
class ApiTokenAPI:
_client: RestApiClient
def list(self, search: str | None = None) -> t.List[ApiToken]: ...
def create(self, name: str, description: str | None = None, scopes: t.List[str] | None = None, expired_at: datetime | None = None) -> ApiToken: ...
def get(self, token_uid: str) -> ApiToken | None: ...
def delete(self, token_uid: str) -> None: ...
Import
from bentoml._internal.cloud.api_token import ApiToken, ApiTokenAPI
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| name | str | Yes | Name of the API token (for create) |
| description | str or None | No | Description of the token |
| scopes | List[str] or None | No | List of permission scopes for the token |
| expired_at | datetime or None | No | Expiration datetime for the token |
| token_uid | str | Yes | UID of the token (for get/delete) |
| search | str or None | No | Search filter string (for list) |
Outputs
| Name | Type | Description |
|---|---|---|
| ApiToken | ApiToken | A single API token object with all metadata |
| List[ApiToken] | List[ApiToken] | List of API token objects (from list operation) |
| None | None | Returned by delete, or by get when token is not found |
Usage Examples
from bentoml._internal.cloud.api_token import ApiTokenAPI
# Assuming a configured REST API client
api = ApiTokenAPI(client=rest_client)
# List all tokens
tokens = api.list()
for token in tokens:
print(token.name, token.created_by)
# Create a new token
new_token = api.create(
name="ci-token",
description="Token for CI pipeline",
scopes=["read", "write"],
)
print(new_token.token) # The actual token value
# Get a specific token
token = api.get(token_uid="abc123")
if token:
print(token.to_yaml())
# Delete a token
api.delete(token_uid="abc123")
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment