Implementation:Bentoml BentoML Api Token Module
| Knowledge Sources | |
|---|---|
| Domains | Cloud, Authentication, API |
| Last Updated | 2026-02-13 15:00 GMT |
Overview
Provides user-facing Python API functions for managing BentoCloud API tokens: listing, creating, retrieving, and deleting tokens.
Description
This module exposes four public functions -- list, create, get, and delete -- that delegate to the underlying BentoCloudClient.api_token service. Each function is decorated with @inject from simple_di to receive the cloud client dependency from BentoMLContainer. The list function supports optional search filtering by token name. The create function accepts a name, optional description, optional scopes (api, read_organization, write_organization, read_cluster, write_cluster), and optional expiration datetime, returning an ApiToken object that includes the token value (shown only once at creation). The get function retrieves a token by UID, and delete removes a token by UID.
Usage
Use this module via bentoml.api_token to manage API tokens for BentoCloud authentication. It is the primary programmatic interface for token CRUD operations.
Code Reference
Source Location
- Repository: Bentoml_BentoML
- File: src/bentoml/api_token.py
- Lines: 1-125
Signature
@inject
def list(
search: str | None = None,
_cloud_client: "BentoCloudClient" = Provide[BentoMLContainer.bentocloud_client],
) -> t.List[ApiToken]: ...
@inject
def create(
name: str,
description: str | None = None,
scopes: t.List[str] | None = None,
expired_at: datetime | None = None,
_cloud_client: "BentoCloudClient" = Provide[BentoMLContainer.bentocloud_client],
) -> ApiToken: ...
@inject
def get(
token_uid: str,
_cloud_client: "BentoCloudClient" = Provide[BentoMLContainer.bentocloud_client],
) -> ApiToken | None: ...
@inject
def delete(
token_uid: str,
_cloud_client: "BentoCloudClient" = Provide[BentoMLContainer.bentocloud_client],
) -> None: ...
Import
import bentoml
# Use via the bentoml.api_token namespace
tokens = bentoml.api_token.list()
token = bentoml.api_token.create(name="my-token")
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| search | str or None | No | Optional filter string for listing tokens by name |
| name | str | Yes (create) | Name for the new API token |
| description | str or None | No | Optional description for the token |
| scopes | List[str] or None | No | List of permission scopes (api, read_organization, write_organization, read_cluster, write_cluster) |
| expired_at | datetime or None | No | Optional expiration datetime for the token |
| token_uid | str | Yes (get/delete) | The UID of the token to retrieve or delete |
Outputs
| Name | Type | Description |
|---|---|---|
| tokens | List[ApiToken] | List of API token objects matching the search criteria (list) |
| token | ApiToken | The newly created token including the token value (create) |
| token | ApiToken or None | The retrieved token or None if not found (get) |
| (none) | None | No return value (delete) |
Usage Examples
import bentoml
from datetime import datetime, timedelta
# List all tokens
tokens = bentoml.api_token.list(search="ci")
for token in tokens:
print(f"{token.name}: {token.uid}")
# Create a new token
token = bentoml.api_token.create(
name="ci-token",
description="CI/CD pipeline token",
scopes=["api", "read_cluster"],
expired_at=datetime.now() + timedelta(days=30),
)
print(f"Token value: {token.token}") # Save this value
# Get a token by UID
token = bentoml.api_token.get("token_abc123")
# Delete a token
bentoml.api_token.delete("token_abc123")