Skip to content

API keys Management #1961

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 3, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libs/labelbox/src/labelbox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,5 @@
)
from lbox.exceptions import *
from labelbox.schema.taskstatus import TaskStatus
from labelbox.schema.api_key import ApiKey
from labelbox.schema.timeunit import TimeUnit
51 changes: 51 additions & 0 deletions libs/labelbox/src/labelbox/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
from labelbox.schema.task import DataUpsertTask, Task
from labelbox.schema.user import User
from labelbox.schema.taskstatus import TaskStatus
from labelbox.schema.api_key import ApiKey
from labelbox.schema.timeunit import TimeUnit

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -2456,3 +2458,52 @@ def cancel_task(self, task_id: str) -> bool:
"""
res = self.execute(mutation_str, {"id": task_id})
return res["cancelBulkOperationJob"]["success"]

def create_api_key(
self,
name: str,
user: Union[User, str],
role: Union[Role, str],
validity: int = 0,
time_unit: TimeUnit = TimeUnit.SECOND,
) -> Dict[str, str]:
"""Creates a new API key.

Args:
name (str): The name of the API key.
user (Union[User, str]): The user object or user ID to associate with the API key.
role (Union[Role, str]): The role object or role ID to assign to the API key.
validity (int, optional): The validity period of the API key. Defaults to 0 (no expiration).
time_unit (TimeUnit, optional): The time unit for the validity period. Defaults to TimeUnit.SECOND.

Returns:
Dict[str, str]: A dictionary containing the created API key information.
"""
warnings.warn(
"The creation of API keys is currently in alpha and its behavior may change in future releases.",
)
return ApiKey.create_api_key(
self, name, user, role, validity, time_unit
)

def get_api_keys(self, include_expired: bool = False) -> List[ApiKey]:
"""Retrieves all API keys accessible to the current user.

Args:
include_revoked (bool, optional): Whether to include revoked API keys. Defaults to True.

Returns:
List[ApiKey]: A list of ApiKey objects.
"""
return ApiKey.get_api_keys(self, include_expired)

def get_api_key(self, api_key_id: str) -> Optional[ApiKey]:
"""Retrieves a single API key by its ID.

Args:
api_key_id (str): The unique ID of the API key.

Returns:
Optional[ApiKey]: The corresponding ApiKey object if found, otherwise None.
"""
return ApiKey.get_api_key(self, api_key_id)
2 changes: 2 additions & 0 deletions libs/labelbox/src/labelbox/schema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@
import labelbox.schema.ontology_kind
import labelbox.schema.project_overview
import labelbox.schema.taskstatus
import labelbox.schema.api_key
import labelbox.schema.timeunit
Loading
Loading