Skip to content

1. Getting started (connection, API key, SSL) ​

This page covers creating a client that connects to eLabFTW. The elab object created here is used on every other page.

The short version ​

python
import elabftw_toolkit

# Set ELABFTW_HOST_URL and ELABFTW_API_KEY (or ELABFTW_KEY_FILE) as environment variables first
elab = elabftw_toolkit.client("your-team-name", check=True)

With check=True, the client checks right away that the API is reachable and that the team name is correct.

Requirements ​

  • Python 3.10 or later
  • An eLabFTW API key
  • The URL of the eLabFTW API v2

Tested with eLabFTW 6.0.x (6.0.6). With other versions, locks and some other behavior may differ (chapter 8, chapter 12).

Dependencies are listed in pyproject.toml (elabapi-python, pandas, python-dotenv, requests, beautifulsoup4, pillow, urllib3, ipython). The package can be imported without IPython and Pillow (display falls back to print; only image size calculation needs Pillow).

Installation ​

bash
python -m pip install elabftw-toolkit            # basic
python -m pip install "elabftw-toolkit[maiml]"   # also install pymaiml for MaiML export
python -m pip install "elabftw-toolkit[mcp]"     # also for the MCP server (chapter 16)
python -m pip install "elabftw-toolkit[benten]"  # also for BENTEN metadata (YAML, Excel)

To pin a version, write elabftw-toolkit==0.1.7.

From source (for development) ​

bash
git clone https://github.com/jasri-jp/elabftw-toolkit.git
cd elabftw-toolkit
python -m pip install -e .            # for development (editable install)
python -m pip install -e ".[maiml]"   # also install pymaiml for MaiML export

To install it as a plain local package, python -m pip install . also works.

Command line ​

You can also use these commands without writing Python. Every command lists its arguments with --help. If a command is not found (the folder where pip puts commands is not on your PATH), python -m elabftw_toolkit.maiml, python -m elabftw_toolkit.benten_schema and python -m elabftw_toolkit.mcp_server work the same way.

CommandWhat it doesMore
elabftw-mcpStart the MCP server (needs [mcp])Chapter 16
elabftw-maiml check <folders…>Check MaiML files and the links between them (needs [maiml])Chapter 10
elabftw-maiml graph <folders…>Draw the links between MaiML files in several places as a Mermaid diagramChapter 10
elabftw-benten check <folder>Check BENTEN metadata (metadata.all.yml) against the schema (needs [benten])Chapter 16

Host and key settings ​

When not given as arguments, these environment variables are read:

bash
ELABFTW_HOST_URL="https://your-elabftw.example.jp"   # /api/v2 may be omitted (added when the URL has no path)
ELABFTW_API_KEY="your-api-key"
# To keep the key in a file, use this instead of ELABFTW_API_KEY
ELABFTW_KEY_FILE="/path/to/elabftw_api_key.txt"      # a text file containing only the key

The API key is looked up in this order:

  1. the api_key argument
  2. the key_file argument
  3. the ELABFTW_API_KEY environment variable
  4. the ELABFTW_KEY_FILE environment variable

The key value is never printed (errors only show the file location).

Passing arguments ​

python
elab = elabftw_toolkit.client(
    "your-team-name",
    host_url="https://your-elabftw.example.jp/api/v2",
    api_key="your-api-key",
    verify_ssl=True,
    check=True,
)

# Read the key from a file (host_url may also be the second positional argument)
elab = elabftw_toolkit.client(
    "your-team-name",
    "https://your-elabftw.example.jp",
    key_file="~/.elabftw/api_key.txt",
)

Using a .env file ​

The library does not load .env automatically. If you use one, load it explicitly in your program, for example with python-dotenv.

python
from dotenv import load_dotenv
import elabftw_toolkit

load_dotenv()
elab = elabftw_toolkit.client("your-team-name")

You can copy .env.example from the repository as a starting point.

SSL certificate verification (verify_ssl) ​

The default is verify_ssl=False. That is convenient on internal test servers, but consider verify_ssl=True in production.

With verify_ssl=False, a warnings filter is registered so that urllib3's InsecureRequestWarning is not shown (limited to that category and to warnings raised by urllib3; the filter applies to the whole process).

What the client offers ​

python
elab.check_connection()       # check the connection later
api_client = elab.api_client  # the underlying elabapi_python.ApiClient (property)
team_id = elab.team_id        # ID of the team given when creating the client (property)

elab.api_client and elab.team_id are properties, not methods.

Calling APIs without a dedicated function (elab.get / elab.patch) ​

APIs that the toolkit has no dedicated function for can be called directly with elab.get() / elab.patch(). They use the client's host, key, verify_ssl, and timeout, and GET requests are retried up to 4 times with a pause on temporary failures (connection errors, timeouts, 429, 5xx).

python
me = elab.get("/users/me")
item_types = elab.get("/items_types")
elab.patch("/items_types/3", {"title": "New name"})

Holding the kind in a variable (elab.entry) ​

When the kind ("experiments" / "items") is in a variable, use elab.entry(kind, id) (same as elab.items.entry(5)).

python
for kind, entry_id in [("experiments", 12), ("items", 5)]:
    print(elab.entry(kind, entry_id).raw()["title"])

Looking up team / category / status IDs ​

python
team_id = elab.teams.current_id()
exp_category_id = elab.experiments.categories.id("Measurement")
exp_status_id = elab.experiments.statuses.id("Planned")
item_category_id = elab.items.categories.id("Sample")
item_status_id = elab.items.statuses.id("To check")

print(team_id, exp_category_id, exp_status_id, item_category_id, item_status_id)

Lists are available as a DataFrame from elab.teams.list(), elab.experiments.categories.list(), elab.items.statuses.list(), and so on. To get a team ID from its name, use elab.teams.id(team_name).

Because the team name is given in client(team_name=...), you can usually omit team_id for experiments.categories, experiments.statuses, items.categories, and items.statuses (to look at another team, use the *_for(team_id) variants such as elab.items.categories_for(team_id)).

The basic pattern ​

Create a client with elabftw_toolkit.client() and work through elab.experiments (lab notebook entries) and elab.items (Resource / Database items). Operations on a single entry start from entry(id).

python
elab.experiments.init(...)                  # create an Experiment
elab.experiments.entry(experiment_id).metadata
elab.items.entry(item_id).files
elab.items.entry(item_id).fields["purity"]

Experiments and Resource items are written the same way for almost every operation.

Notes ​

  • The API key is a secret. Handle .env, tests/live/.env.test, log output, and shared zip files with care.
  • This is an unofficial eLabFTW toolkit. Behavior may differ depending on the eLabFTW version and server settings. Try it on test entries before using it in production.

Next: 2. Entries and fields.

Apache License 2.0