API Key
This topic describes how to create and manage API keys that you can use in other Python SDK requests.
Create an API Key
Use this API to create an API key for a user.
Syntax
egs.create_api_key(
name="<name>",
role="<role>",
validity="<validity>",
username="<username>",
description="<description>",
workspace_name="<workspace_name>",
authenticated_session=None
)
Parameters
Parameter | Parameter Type | Description | Required |
---|---|---|---|
name | String | This is the name of the API key. | Mandatory |
userName | String | The user associated with the key. The default value is admin . | Optional |
description | String | Description for the API key. | Optional |
role | String | This is the role for the API key. The supported values are Owner , Editor , and Viewer . | Mandatory |
validUntil | String | This is the validity period of the API key. | Mandatory |
workspaceName | String | The name of the workspace name if applicable. For Editor and Viewer roles, the workspace name is mandatory. | Optional |
authenticated_session | [AuthenticatedSession] | The authenticated session with the EGS Controller. The default value is None . If no authenticated session is set, SDK tries to use the SDK default. If no SDK default is found, an exception is raised. | Optional |
Example
Example: Create an Editor API Key
import egs
auth = egs.authenticate("https://egs-core-apis.example.com", "5067bd55-1aef-4c84-8987-3e966e917f07")
from egs.exceptions import (
ApiKeyInvalid, ApiKeyNotFound, WorkspaceAlreadyExists
)
try:
response = egs.create_api_key(
name=cur_ws["name"],
role="Editor",
validity=cur_ws["apiKeyValidity"],
username=cur_ws["username"],
description=f"API Key for {cur_ws['name']}",
workspace_name=cur_ws["name"],
authenticated_session=auth,
)
try:
apikey_path = os.path.join(workspace_dir, "apikey.txt")
with open(apikey_path,
"w",
encoding="utf-8") as apikey_file:
apikey_file.write(response)
print(
f"✅ Successfully Saved API key: {cur_ws['name']} "
f"api-key {response}"
)
except Exception as e:
print(f"Failed to save token for {cur_ws['name']}")
raise ValueError(
f"Failed to save token for {cur_ws['name']}: {str(e)}"
) from e
except (ApiKeyInvalid, ApiKeyNotFound, ValueError) as e:
print(f"⚠️ Error creating API key {cur_ws['name']}: {e}")
except Exception as e:
print(f"❌ Unexpected error for {cur_ws['name']}: {e}")
Response Returned
Returns | Description |
---|---|
String | The response returns the created API Key. |
Exceptions Raised
Raises | Description |
---|---|
exceptions.Unhandled | 400: "Bad Request: Invalid request format." |
exceptions.Unhandled | 401: "Unauthorized: Invalid authentication." |
exceptions.Unhandled | 403: "Forbidden: You lack required permissions." |
exceptions.Unhandled | 404: "Not Found: Resource does not exist." |
exceptions.Unhandled | 409: "Conflict: Resource already exists." |
exceptions.Unhandled | 422: "Unprocessable Entity: Invalid request parameters." |
exceptions.Unhandled | 500: "Internal Server Error: Server-side issue." |
exceptions.Unhandled | 503: "Service Unavailable: Temporary server issue." |
List API Keys
Use this API to get the API keys. You can also list API keys per workspace optionally.
Syntax
egs.list_api_keys(workspace_name: "<workspace>", authenticated_session: [AuthenticatedSession] = None)
Parameters
Parameter | Parameter Type | Description | Required |
---|---|---|---|
workspace | String | The workspace for which you want to get the API keys. | Optional |
authenticated_session | [AuthenticatedSession] | The authenticated session with the EGS Controller. The default value is None . If no authenticated session is set, SDK tries to use the SDK default. If no SDK default is found, an exception is raised. | Optional |
Example
import egs
auth = egs.authenticate("https://egs-core-apis.example.com", "5067bd55-1aef-4c84-8987-3e966e917f07")
egs.list_api_key(workspace = "red")
Response Returned
Returns | Description |
---|---|
Dictionary | The response returns a list of API Keys for a given workspace. |
Exceptions Raised
Raises | Description |
---|---|
exceptions.Unhandled | 401: "Unauthorized: Authentication failed." |
exceptions.Unhandled | 403: "Forbidden: Insufficient permissions." |
exceptions.Unhandled | 404: "No API Keys found." |
Delete an API Key
Use this API to delete an API key created for a given workspace.
Syntax
delete_api_key(api_key: "<API key>", authenticated_session: [AuthenticatedSession] = None)
Parameters
Parameter | Parameter Type | Description | Required |
---|---|---|---|
api-key | String | The API key that you want to delete. | Mandatory |
authenticated_session | [AuthenticatedSession] | The authenticated session with the EGS Controller. The default value is None . If no authenticated session is set, SDK tries to use the SDK default. If no SDK default is found, an exception is raised. | Optional |
Example
import egs
auth = egs.authenticate("https://egs-core-apis.example.com", "5067bd55-1aef-4c84-8987-3e966e917f07")
egs.delete_api_key(api-key = "06de7aa7-01db-4510-91f6-ba591aad70a9")
Response Returned
Returns | Description |
---|---|
String | The response returns the confirmation of deletion. |
Exceptions Raised
Raises | Description |
---|---|
exceptions.Unhandled | 401: "Unauthorized: Authentication failed." |
exceptions.Unhandled | 403: "Forbidden: Insufficient permissions." |
exceptions.Unhandled | 404: "API Key not found." |