Web API#
The DataLab Web API provides a modern HTTP/JSON interface for external access to the DataLab workspace. It is the recommended integration path for new projects, especially for Jupyter notebooks and web-based tools.
Note
The Web API is available starting from DataLab 1.1. The required dependencies (FastAPI, Uvicorn, Pydantic) are included in the standard DataLab installation.
Overview#
The Web API enables:
Jupyter notebook integration via DataLab-Kernel
WASM/Pyodide compatibility (no XML-RPC dependency)
External tool integration (any HTTP client)
Unlike the XML-RPC interface, the Web API:
Uses JSON for metadata and NPZ for binary data (efficient large array transfer)
Provides bearer token authentication for security
Follows REST conventions with OpenAPI documentation
Works in browser environments (WASM/Pyodide)
Feature |
XML-RPC |
Web API |
|---|---|---|
Protocol |
XML |
JSON + Binary (NPZ) |
WASM Support |
β |
β |
Large Arrays |
Slow (base64) |
Fast (NPZ) |
Authentication |
None |
Bearer Token |
Standards |
XML-RPC |
REST + OpenAPI |
Quick Start#
Enabling the Web API#
There are several ways to enable the Web API server:
Via UI: File β Web API β Start Web API Server
Via environment variable: Set
DATALAB_WEBAPI_ENABLED=1before starting DataLab
When started, DataLab displays the server URL and authentication token in a dialog. The status bar also shows the Web API port when the server is running.
The default port is 18080 (or the next available port if busy).
Auto-Discovery#
DataLab-Kernel can automatically discover and connect to a running DataLab instance without any manual configuration. When the Web API starts, DataLab writes connection information to a file that DataLab-Kernel reads automatically.
Just load the extension β no environment variables or explicit connection needed:
# In your notebook (JupyterLab, VS Code, etc.)
%load_ext datalab_kernel
# DataLab-Kernel automatically finds and connects to DataLab
workspace.list() # Already connected!
The auto-discovery mechanism tries the following methods in order:
Environment variables (
DATALAB_WORKSPACE_URL,DATALAB_WORKSPACE_TOKEN)Connection file written by DataLab (native Python only)
URL query parameters (JupyterLite:
?datalab_url=...&datalab_token=...)Well-known port probing (
http://127.0.0.1:18080)
If discovery fails, DataLab-Kernel starts in standalone mode and you can connect
later using workspace.connect().
Manual Connection (Legacy)#
If auto-discovery doesnβt work (e.g., running on a different machine), you can set environment variables before starting your notebook kernel:
export DATALAB_WORKSPACE_URL=http://127.0.0.1:18080
export DATALAB_WORKSPACE_TOKEN=<your-token>
Then in your notebook using DataLab-Kernel:
from datalab_kernel import workspace
# List objects in DataLab
workspace.list()
# Retrieve an object
signal = workspace.get("my_signal")
# Add a processed result back to DataLab
workspace.add("processed", result)
API Reference#
Base URL: http://127.0.0.1:<port>/api/v1
Status Endpoint (No Auth Required)#
Method |
Endpoint |
Description |
|---|---|---|
GET |
|
Server status and version information |
Object Operations (Auth Required)#
All endpoints below require the Authorization: Bearer <token> header.
Method |
Endpoint |
Description |
|---|---|---|
GET |
|
List all objects with metadata (JSON) |
GET |
|
Get object metadata (JSON) |
DELETE |
|
Delete an object |
PATCH |
|
Update object metadata |
Binary Data Transfer (Auth Required)#
Method |
Endpoint |
Description |
|---|---|---|
GET |
|
Download object as NPZ archive |
PUT |
|
Upload object from NPZ archive |
Data Format#
Objects are transferred using NumPyβs NPZ format for efficiency.
SignalObj NPZ Structure#
my_signal.npz
βββ x.npy # X coordinates (float64)
βββ y.npy # Y data (float64)
βββ dx.npy # X uncertainties (optional)
βββ dy.npy # Y uncertainties (optional)
βββ metadata.json # Labels, units, title
ImageObj NPZ Structure#
my_image.npz
βββ data.npy # 2D image array (preserves dtype)
βββ metadata.json # Labels, units, coordinates (x0, y0, dx, dy)
Authentication#
All API endpoints except /status require a bearer token:
Authorization: Bearer <token>
The token is generated when the server starts and displayed in the connection dialog.
Environment Variables#
Variable |
Description |
Default |
|---|---|---|
|
Bind address |
|
|
Server port |
|
|
Auth token |
Generated |
Security Model#
The Web API implements the following security measures:
Localhost binding: By default, the server only accepts connections from the local machine (127.0.0.1).
Token authentication: Every request (except status) must include a valid bearer token.
Explicit opt-in: Remote binding (0.0.0.0) requires explicit configuration.
Localhost Token Bypass#
For simplified local development, you can disable token verification for localhost connections in Edit β Settings β Web API localhost bypass.
When enabled, clients connecting from 127.0.0.1 do not need to provide a token.
This makes auto-discovery work seamlessly even when the connection file cannot be
read (e.g., in JupyterLite or sandboxed environments).
Warning
Only enable localhost bypass when you control all applications running on your machine. Malicious local software could access your data without a token.
Warning
Exposing the API to the network allows anyone with the token to access your data. Use with caution on trusted networks only.
Architecture#
βββββββββββββββββββββββββββββββββββββββββββ
β DataLab Main Window β
β (Qt) β
βββββββββββββββββββββββββββββββββββββββββββ€
β WebApiController β
β (Server Lifecycle) β
βββββββββββββββββββββββββββββββββββββββββββ€
β WorkspaceAdapter β
β (Thread-safe access) β
βββββββββββββββββββββββββββββββββββββββββββ€
β FastAPI + Uvicorn β
β (Separate thread) β
βββββββββββββββββββββββββββββββββββββββββββ
β
β HTTP/JSON
β
βββββββββββββββββββββββββββββββββββββββββββ
β DataLab-Kernel / Client β
βββββββββββββββββββββββββββββββββββββββββββ
The Web API runs in a separate thread from the Qt GUI. The WorkspaceAdapter
ensures thread-safe access to DataLab objects by marshalling calls to the main
thread using Qtβs signal/slot mechanism.
Installation#
The Web API dependencies are included in the standard DataLab installation:
FastAPI (web framework)
Uvicorn (ASGI server)
Pydantic (data validation)
No additional installation steps are required.
Programmatic Control#
The Web API can also be started and controlled programmatically via the XML-RPC interface, which is useful for automated workflows:
from datalab.control.remote import RemoteClient
proxy = RemoteClient()
proxy.connect()
# Start the Web API server
info = proxy.start_webapi_server()
print(f"URL: {info['url']}")
print(f"Token: {info['token']}")
# Check status
status = proxy.get_webapi_status()
print(f"Running: {status['running']}")
# Stop the server
proxy.stop_webapi_server()
See Also#
Remote controlling (XML-RPC) for the XML-RPC interface (legacy)
DataLab-Kernel documentation for Jupyter integration