Skip to main content
Version: 1.13.0

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

ParameterParameter TypeDescriptionRequired
nameStringThis is the name of the API key.Mandatory
userNameStringThe user associated with the key. The default value is admin.Optional
descriptionStringDescription for the API key.Optional
roleStringThis is the role for the API key. The supported values are Owner, Editor, and Viewer.Mandatory
validUntilStringThis is the validity period of the API key.Mandatory
workspaceNameStringThe name of the workspace name if applicable. For Editorand 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

ReturnsDescription
StringThe response returns the created API Key.

Exceptions Raised

RaisesDescription
exceptions.Unhandled400: "Bad Request: Invalid request format."
exceptions.Unhandled401: "Unauthorized: Invalid authentication."
exceptions.Unhandled403: "Forbidden: You lack required permissions."
exceptions.Unhandled404: "Not Found: Resource does not exist."
exceptions.Unhandled409: "Conflict: Resource already exists."
exceptions.Unhandled422: "Unprocessable Entity: Invalid request parameters."
exceptions.Unhandled500: "Internal Server Error: Server-side issue."
exceptions.Unhandled503: "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

ParameterParameter TypeDescriptionRequired
workspaceStringThe 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

ReturnsDescription
DictionaryThe response returns a list of API Keys for a given workspace.

Exceptions Raised

RaisesDescription
exceptions.Unhandled401: "Unauthorized: Authentication failed."
exceptions.Unhandled403: "Forbidden: Insufficient permissions."
exceptions.Unhandled404: "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

ParameterParameter TypeDescriptionRequired
api-keyStringThe 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

ReturnsDescription
StringThe response returns the confirmation of deletion.

Exceptions Raised

RaisesDescription
exceptions.Unhandled401: "Unauthorized: Authentication failed."
exceptions.Unhandled403: "Forbidden: Insufficient permissions."
exceptions.Unhandled404: "API Key not found."