Skip to main content
Version: 1.0

aixplain.factories.api_key_factory

APIKeyFactory Objects

class APIKeyFactory()

[view_source]

Factory class for managing API keys in the aiXplain platform.

This class provides functionality for creating, retrieving, updating, and monitoring API keys, including their usage limits and budgets.

Attributes:

  • backend_url str - Base URL for the aiXplain backend API.

get

@classmethod
def get(cls, api_key: Text) -> APIKey

[view_source]

Retrieve an API key by its value.

This method searches for an API key by matching the first and last 4 characters of the provided key.

Arguments:

  • api_key Text - The API key value to search for.

Returns:

  • APIKey - The matching API key object.

Raises:

  • Exception - If no matching API key is found.

list

@classmethod
def list(cls) -> List[APIKey]

[view_source]

List all API keys accessible to the current user.

This method retrieves all API keys that the authenticated user has access to, using the configured TEAM_API_KEY.

Returns:

  • List[APIKey] - List of API key objects.

Raises:

  • Exception - If the API request fails or returns an error, including cases where authentication fails or the service is unavailable.

create

@classmethod
def create(cls, name: Text, budget: int, global_limits: Union[Dict,
APIKeyLimits],
asset_limits: List[Union[Dict, APIKeyLimits]],
expires_at: datetime) -> APIKey

[view_source]

Create a new API key with specified limits and budget.

This method creates a new API key with configured usage limits, budget, and expiration date.

Arguments:

  • name Text - Name or description for the API key.
  • budget int - Total budget allocated to this API key.
  • global_limits Union[Dict, APIKeyLimits] - Global usage limits for the key, either as a dictionary or APIKeyLimits object.
  • asset_limits List[Union[Dict, APIKeyLimits]] - List of per-asset usage limits, each either as a dictionary or APIKeyLimits object.
  • expires_at datetime - Expiration date and time for the API key.

Returns:

  • APIKey - Created API key object with its access key and configuration.

Raises:

  • Exception - If the API request fails or returns an error, including cases where validation fails or the service is unavailable.

update

@classmethod
def update(cls, api_key: APIKey) -> APIKey

[view_source]

Update an existing API key's configuration.

This method updates an API key's settings such as limits, budget, and expiration date. The API key must be validated before update.

Arguments:

  • api_key APIKey - API key object with updated configuration. Must have a valid ID of an existing key.

Returns:

  • APIKey - Updated API key object with new configuration.

Raises:

  • Exception - If:
    • API key validation fails
    • API key ID is invalid
    • Update request fails
    • Service is unavailable

get_usage_limits

@classmethod
def get_usage_limits(
cls,
api_key: Text = config.TEAM_API_KEY,
asset_id: Optional[Text] = None) -> List[APIKeyUsageLimit]

[view_source]

Retrieve current usage limits and counts for an API key.

This method fetches the current usage statistics and limits for an API key, optionally filtered by a specific asset.

Arguments:

  • api_key Text, optional - API key to check usage for. Defaults to config.TEAM_API_KEY.
  • asset_id Optional[Text], optional - Filter usage limits for a specific asset. Defaults to None, showing all assets.

Returns:

  • List[APIKeyUsageLimit] - List of usage limit objects containing:
    • daily_request_count: Current number of requests today
    • daily_request_limit: Maximum allowed requests per day
    • daily_token_count: Current number of tokens used today
    • daily_token_limit: Maximum allowed tokens per day
    • model: Asset ID if limit is asset-specific, None if global

Raises:

  • Exception - If:
    • API key is invalid
    • User is not the key owner
    • Service is unavailable