Skip to content

swanlab.Api

Version Note

This documentation applies to swanlab >= 0.7.8.

Based on SwanLab's cloud capabilities, it provides access to OpenAPI on the SDK side, allowing users to programmatically manipulate cloud experiment/project/workspace/metric resources in their local environment.

Through the OpenAPI form, users can in their local programming environment:

  • Retrieve experiment data, personal information, workspace details, project lists, etc.
  • Perform automated experiment management (e.g., querying, organizing, metadata editing, etc.)
  • More easily integrate with other tools (e.g., CI/CD, experiment scheduling, etc.)

Leveraging this feature can greatly enhance the flexibility and extensibility of the SDK, facilitating the construction of advanced usage patterns or extension systems.

Core Terminology

  • Workspace: A collection of projects, corresponding to a development team (e.g., "SwanLab"). It is divided into Personal Workspace (PERSON) and Organizational Workspace (TEAM).
  • Project: A collection of experiments, corresponding to a development task (e.g., "Image Classification").
  • Run (Experiment): A single training/inference task, containing metrics, configurations, logs, and other data.
  • Experiment ID: The unique identifier for an experiment, used to precisely locate a single experiment. It can be found in the "Environment" tab of the experiment on the WebUI or in the URL, with the format https://swanlab.cn/@username/project_name/runs/experiment_id/....

Introduction

Prerequisite: You must have logged into a SwanLab account within your programming environment.

To use SwanLab's OpenAPI, simply instantiate an Api object.

python
import swanlab

api = swanlab.Api() # Uses local login information (from `swanlab login`)

If you need to access data for a specific user:

python
import swanlab

other_api = swanlab.Api(api_key='other_api_key') # Uses a specific user's api_key

Specifically, the authentication logic for OpenAPI is as follows:

  1. If the api_key parameter is explicitly provided, that api_key is prioritized for authentication. You can view your API key here.
  2. Otherwise, local authentication information is used.

Using OpenAPI with Private Deployment

For privately deployed SwanLab, you can pass the host parameter to Api.

python
import swanlab

api = swanlab.Api(api_key='your-api-key', host='your-host')

workspace

python
import swanlab

api = swanlab.Api()

workspaces = api.workspaces()

for workspace in workspaces:
    print(workspace.username)
    print(workspace.name)
    print(workspace.profile)
python
import swanlab

api = swanlab.Api()

workspace = api.workspace(username='username')

print(workspace.username)
print(workspace.name)
print(workspace.profile)
python
import swanlab

api = swanlab.Api()

workspace = api.workspace(username='username')
projects = workspace.projects()

for project in projects:
    print(project.name)
    print(project.url)

Parameters for workspaces:

ParameterTypeDescription
usernamestrWorkspace username, i.e., the unique ID. Defaults to the currently logged-in user.

Parameters for workspace:

ParameterTypeDescription
usernamestrWorkspace username, i.e., the unique ID. Defaults to the currently logged-in user.

Attributes and Methods of a workspace object:

Attribute/MethodTypeDescription
usernamestrWorkspace username, i.e., the unique ID
namestrWorkspace name
rolestrThe role of the current logged-in user in this workspace: OWNER or MEMBER
profiledictWorkspace introduction information, includes bio, url, institution, email
workspace_typestrWorkspace type, either Personal or Organizational: PERSON or TEAM
projects()-Gets project objects under this workspace
json()-Gets all workspace information, returns a dict

project

python
import swanlab

api = swanlab.Api()

projects = api.projects(path='username')

for project in projects:
    print(project.name)
    print(project.created_at)
    print(project.url)
    print(project.visibility)
python
import swanlab

api = swanlab.Api()

project = api.project(path='username/project_name')

print(project.name)
print(project.created_at)
print(project.url)
print(project.visibility)
python
import swanlab

api = swanlab.Api()

project = api.project(path='username/project_name')
runs = project.runs()

for run in runs:
    print(run.name)
    print(run.url)

Parameters for projects:

ParameterTypeDescription
pathstrWorkspace path (username), format: username. Used to filter all projects under a specified workspace.
sortstrSorting method, options: created_at (creation time), updated_at (update time)
searchstrSearch keyword, fuzzy matches project names
detailboolWhether to return detailed project information (e.g., description, tags). Default is True.

Parameters for project:

ParameterTypeDescription
pathstrProject path, format: username/project_name

Attributes and Methods of a project object:

Attribute/MethodTypeDescription
namestrProject name
pathstrProject path, format: username/project_name
descriptionstrProject description
labelslistProject tags, format: [label1, label2, ...]
created_atstrProject creation time, format: ISO 8601 UTC, e.g., 2025-12-09T17:57:38.224Z
updated_atstrProject update time, format same as created_at
urlstrProject URL
visibilitystrProject visibility: PUBLIC or PRIVATE
countdictProject statistics, includes number of runs, collaborators, etc.
runs()-Gets run objects under this project
json()-Gets all project information, returns a dict

run

python
import swanlab

api = swanlab.Api()

runs = api.runs(path='username/project_name')

for run in runs:
    print(run.name)
    print(run.id)
    print(run.created_at)
    print(run.url)
python
import swanlab

api = swanlab.Api()

run = api.run(path='username/project_name/experiment_id')

print(run.name)
print(run.id)
print(run.created_at)
print(run.url)
python
import swanlab

api = swanlab.Api()

run = api.run(path='username/project_name/experiment_id')

print(run.profile.config)
python
import swanlab

api = swanlab.Api()

run = api.run(path='username/project_name/experiment_id')

# Get the run's Python version, hardware info, etc.
print(run.profile.metadata)
# Get the run's Python environment information
print(run.profile.requirements)

Parameters for runs:

ParameterTypeDescription
pathstrProject path, format: username/project_name
filtersdictFilter conditions, e.g., {'state': 'FINISHED', 'config.batch_size': '64'}

Parameters for run:

ParameterTypeDescription
pathstrRun path, format: username/project_name/experiment_id

Attributes of a run object:

Attribute/MethodTypeDescription
namestrRun name
pathstrRun path, format: username/project_name/experiment_id
descriptionstrRun description
idstrRun ID (Experiment ID)
statestrRun state: FINISHED, RUNNING, CRASHED, ABORTED
grouplistRun group, format: ['A', 'B', 'C']
labelslistRun tags, format: [label1, label2, ...]
created_atstrRun creation time, format: ISO 8601 UTC, e.g., 2025-12-09T17:57:38.224Z
finished_atstrRun finish time, format same as created_at; if the run has not finished, this field is None
urlstrRun URL
job_typestrJob type, format: job_name
profiledictRun configuration information, includes conda, config, metadata, requirements attributes
showboolRun visibility in the chart comparison view: True or False
userdictRun user, format: {'is_self': True, 'username': 'username'}
metrics()-Gets the run's metric data, returns a pd.DataFrame
json()-Gets all run information, returns a dict

runs filters

Using the conditional filter parameter filters, you can filter runs that meet specific criteria within a project.

python
import swanlab

api = swanlab.Api()

runs = api.runs(
    path='username/project_name',
    filters={
        'state': 'FINISHED',
        'config.batch_size': '64',
        },
    )

The filters parameter supports the following conditions:

  • state: Run state, options: FINISHED, RUNNING, CRASHED, ABORTED
  • config.<config_name>: Configuration name, requires config. prefix; filters runs where the config value equals the specified value. Supports nested keys, e.g., config.data.run_id

metrics

Gets the metric data for a run. The return value type is pd.DataFrame.

python
import swanlab

api = swanlab.Api()

run = api.run(path='username/project_name/experiment_id')
metrics = run.metrics(keys=['loss'])

print(metrics)
python
import swanlab

api = swanlab.Api()

run = api.run(path='username/project_name/experiment_id')
metrics = run.metrics(keys=['loss', 'acc'])

print(metrics)
python
import swanlab

api = swanlab.Api()

run = api.run(path='username/project_name/experiment_id')
metrics = run.metrics(keys=['loss'], x_axis='acc')

print(metrics)
python
import swanlab

api = swanlab.Api()

run = api.run(path='username/project_name/experiment_id')
metrics = run.metrics(keys=['loss'], sample=100)

print(metrics)
ParameterTypeDefaultDescription
keyslist[str]NoneList of metric names to retrieve, e.g., ['loss', 'acc']. If not provided, returns an empty DataFrame.
x_axisstrstepX-axis dimension, options: step (step number), or a metric name (e.g., acc)
sampleintNoneSample count, limits the number of returned rows. If not provided, returns all data.

user

This operation is limited to super administrators on private deployments.

python
import swanlab

# Ensure you are logged into a privately deployed SwanLab as a super administrator
api = swanlab.Api()

users = api.users()

for user in users:
    print(user.username)
    print(user.is_self)
    print(user.teams)
python
import swanlab

# Ensure you are logged into a privately deployed SwanLab as a super administrator
api = swanlab.Api()

user = api.user()

# Create a user with username 'testuser' and password 'test123456'
user.create(username='testuser', password='test123456')

"""For batch creation
for i in range(3):
    user.create(username=f'testuser{i}', password='test123456')
"""
python
import swanlab

# Ensure you are logged into a privately deployed SwanLab as a super administrator
api = swanlab.Api()

user = api.user()

# The following operations currently only support the currently logged-in account.
# Print API Keys
print(user.api_keys)

# Generate a new API Key
new_api_key = user.generate_api_key()

# Delete an API Key
user.delete_api_key(api_key=new_api_key)

Parameters for user:

ParameterTypeDescription
usernamestrUsername, defaults to the currently logged-in user.

Attributes of a user object:

AttributeTypeDescription
usernamestrUsername
is_selfboolWhether this user is the currently logged-in user
teamslistTeams the user belongs to
api_keyslistList of the user's API keys. Only supports getting API keys for the currently logged-in user.