Lightly Python API Docs | dltHub

Build a Lightly-to-database pipeline in Python using dlt with AI Workbench support for Claude Code, Cursor, and Codex.

Last updated:

Lightly is a platform and Python library for self-supervised learning and dataset management that provides an HTTP API and client for dataset, embedding, tag, and worker management and exporting sample metadata and read URLs. The REST API base URL is https://api.lightly.ai and all requests require a user token (set via LIGHTLY_TOKEN or passed into the client).

dlt is an open-source Python library that handles authentication, pagination, and schema evolution automatically. dlthub provides AI context files that enable code assistants to generate production-ready pipelines. Install with uv pip install "dlt[workspace]" and start loading Lightly data in under 10 minutes.


What data can I load from Lightly?

Here are some of the endpoints you can load from Lightly:

ResourceEndpointMethodData selectorDescription
datasets/api/v1/datasetsGET(response is a list)Returns datasets accessible by the user (get_datasets)
datasets_by_name/api/v1/datasets?name={name}GET(response is a list)Fetch datasets by name (get_datasets_by_name/get_datasets_by_name)
embeddings/api/v1/datasets/{dataset_id}/embeddingsGET(response is a list)Embedding metadata for a dataset (get_all_embedding_data/get_embedding_data_by_name)
tags/api/v1/datasets/{dataset_id}/tagsGET(response is a list)Lists tags for current dataset (get_all_tags)
compute_worker_runs/api/v1/compute-worker/runsGET(response is a list)Lists Lightly Worker runs (get_compute_worker_runs)
compute_worker_run/api/v1/compute-worker/runs/{run_id}GET(object)Fetch a specific Lightly Worker run (get_compute_worker_run)
filenames_export_by_tag/api/v1/datasets/{dataset_id}/export/filenames?tagId={tag_id}GET(response is a list)Export filenames/read URLs for samples in a tag (export_filenames_and_read_urls_by_tag_id)
labelbox_export_by_tag/api/v1/datasets/{dataset_id}/export/labelbox?tagId={tag_id}GET(response is a list)Export Labelbox v3 compatible rows (export_label_box_data_rows_by_tag_id)
labelstudio_export_by_tag/api/v1/datasets/{dataset_id}/export/labelstudio?tagId={tag_id}GET(response is a list)Export Label Studio tasks for a tag (export_label_studio_tasks_by_tag_id)
workers/api/v1/compute-workerGET(response is a list)List registered Lightly Workers (get_compute_workers)

How do I authenticate with the Lightly API?

Authentication uses a user token read from the LIGHTLY_TOKEN environment variable or passed to the ApiWorkflowClient(token=...). The token must be sent in requests as the authorization credential used by the Python client (bearer-style token). The Python client will pick up LIGHTLY_TOKEN if token not passed explicitly.

1. Get your credentials

  1. Sign up / log in at https://app.lightly.ai or create an account on lightly.ai. 2) In the web app or Lightly dashboard, open your user/profile or API settings to create or copy a personal access token. 3) Locally set the token in the environment: export LIGHTLY_TOKEN="your_token_here" or pass token directly to ApiWorkflowClient(token="your_token_here").

2. Add them to .dlt/secrets.toml

[sources.lightly_ssl_source] api_key = "your_lightly_token_here"

dlt reads this automatically at runtime — never hardcode tokens in your pipeline script. For production environments, see setting up credentials with dlt for environment variable and vault-based options.


How do I set up and run the pipeline?

Set up a virtual environment and install dlt:

uv venv && source .venv/bin/activate uv pip install "dlt[workspace]"

1. Install the dlt AI Workbench:

dlt ai init --agent <your-agent> # <agent>: claude | cursor | codex

This installs project rules, a secrets management skill, appropriate ignore files, and configures the dlt MCP server for your agent. Learn more →

2. Install the rest-api-pipeline toolkit:

dlt ai toolkit rest-api-pipeline install

This loads the skills and context about dlt the agent uses to build the pipeline iteratively, efficiently, and safely. The agent uses MCP tools to inspect credentials — it never needs to read your secrets.toml directly. Learn more →

3. Start LLM-assisted coding:

Use /find-source to load data from the Lightly API into DuckDB.

The rest-api-pipeline toolkit takes over from here — it reads relevant API documentation, presents you with options for which endpoints to load, and follows a structured workflow to scaffold, debug, and validate the pipeline step by step.

4. Run the pipeline:

python lightly_ssl_pipeline.py

If everything is configured correctly, you'll see output like this:

Pipeline lightly_ssl_pipeline load step completed in 0.26 seconds 1 load package(s) were loaded to destination duckdb and into dataset lightly_ssl_data The duckdb destination used duckdb:/lightly_ssl.duckdb location to store data Load package 1749667187.541553 is LOADED and contains no failed jobs

Inspect your pipeline and data:

dlt pipeline lightly_ssl_pipeline show

This opens the Pipeline Dashboard where you can verify pipeline state, load metrics, schema (tables, columns, types), and query the loaded data directly.


Python pipeline example

This example loads datasets and tags from the Lightly API into DuckDB. It mirrors the endpoint and data selector configuration from the table above:

import dlt from dlt.sources.rest_api import RESTAPIConfig, rest_api_resources @dlt.source def lightly_ssl_source(token=dlt.secrets.value): config: RESTAPIConfig = { "client": { "base_url": "https://api.lightly.ai", "auth": { "type": "bearer", "token": token, }, }, "resources": [ {"name": "datasets", "endpoint": {"path": "api/v1/datasets"}}, {"name": "tags", "endpoint": {"path": "api/v1/datasets/{dataset_id}/tags"}} ], } yield from rest_api_resources(config) def get_data() -> None: pipeline = dlt.pipeline( pipeline_name="lightly_ssl_pipeline", destination="duckdb", dataset_name="lightly_ssl_data", ) load_info = pipeline.run(lightly_ssl_source()) print(load_info)

To add more endpoints, append entries from the resource table to the "resources" list using the same name, path, and data_selector pattern.


How do I query the loaded data?

Once the pipeline runs, dlt creates one table per resource. You can query with Python or SQL.

Python (pandas DataFrame):

import dlt data = dlt.pipeline("lightly_ssl_pipeline").dataset() sessions_df = data.tags.df() print(sessions_df.head())

SQL (DuckDB example):

SELECT * FROM lightly_ssl_data.tags LIMIT 10;

In a marimo or Jupyter notebook:

import dlt data = dlt.pipeline("lightly_ssl_pipeline").dataset() data.tags.df().head()

See how to explore your data in marimo Notebooks and how to query your data in Python with dataset.


What destinations can I load Lightly data to?

dlt supports loading into any of these destinations — only the destination parameter changes:

DestinationExample value
DuckDB (local, default)"duckdb"
PostgreSQL"postgres"
BigQuery"bigquery"
Snowflake"snowflake"
Redshift"redshift"
Databricks"databricks"
Filesystem (S3, GCS, Azure)"filesystem"

Change the destination in dlt.pipeline(destination="snowflake") and add credentials in .dlt/secrets.toml. See the full destinations list.


Troubleshooting

Authentication failures

Check that LIGHTLY_TOKEN is set or that ApiWorkflowClient(token=...) receives the correct token. Invalid or expired tokens will result in 401 responses from the API. Ensure token has appropriate project/dataset access.

Rate limits and 429 responses

If you receive HTTP 429, slow down request rate or implement exponential backoff. The docs do not list explicit rate limits; treat the platform as rate-limited and add retries.

Pagination

Most Python client list endpoints return full lists (client methods return lists or iterators). If the underlying REST endpoints are paginated, the Python client transparently iterates or offers iterator variants (e.g., get_datasets_iter). Use iterator methods (get_*_iter) to handle large result sets.

Export endpoints returning file-like content or arrays

Export endpoints (filenames/readUrls, Labelbox/Label Studio exports) return lists of dictionaries; check that the response is a top-level JSON array (examples in docs show a list being returned). If you receive unexpected responses, verify dataset_id and tag_id are correct and that the dataset has processed samples.

Ensure that the API key is valid to avoid 401 Unauthorized errors. Also, verify endpoint paths and parameters to avoid 404 Not Found errors.


Next steps

Continue your data engineering journey with the other toolkits of the dltHub AI Workbench:

  • data-exploration — Build custom notebooks, charts, and dashboards for deeper analysis with marimo notebooks.
  • dlthub-runtime — Deploy, schedule, and monitor your pipeline in production.
dlt ai toolkit data-exploration install dlt ai toolkit dlthub-runtime install

Was this page helpful?

Community Hub

Need more dlt context for Lightly?

Request dlt skills, commands, AGENT.md files, and AI-native context.