> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.labric.co/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.labric.co/_mcp/server.

# Write your first Labric job

Jobs are Python scripts that run in a sandboxed environment on Labric. They can be triggered automatically when files are uploaded, or run on demand. This guide walks through creating a simple job that reads a CSV and writes the data to a table.

## Create a job

1. Go to [Jobs](https://platform.labric.co/jobs) and click **New**
2. Give your job a name (e.g., "CSV Importer")
3. Replace the default script with the code below

## The script

```python
import csv
import os
from labric import Labric, TableWrite

client = Labric()

file_path = os.environ["LABRIC_FILE_PATH_0"]

with open(file_path, "r") as f:
    records = list(csv.DictReader(f))

client.data.write(tables=[TableWrite(table="measurements", rows=records)])

print(f"Wrote {len(records)} records to measurements")
```

This reads the first input file as a CSV, then writes every row to a table called `measurements`. The table must already exist in your organization, and each CSV header must name one of its columns. Values are converted to each column's type, so numbers read from the CSV as text are stored as numbers.

Labric records every write the script makes under the execution that ran it, so you can inspect what a run produced and [revert it](/api-reference/labric-api/jobs/revert) as a unit. The job's API key identifies the execution, so the script doesn't pass `job_execution_id`.

## Add dependencies

The `labric` SDK is always available. To use other packages, declare them in a [PEP 723](https://peps.python.org/pep-0723/) inline metadata block at the top of your script — Labric installs them before the script runs:

```python
# /// script
# dependencies = ["pandas", "scipy>=1.13"]
# ///
import pandas as pd
```

Any standard version specifier works, so pin versions (e.g. `pandas==2.2.2`) when reproducibility matters. The block is validated when you save the job, so a malformed declaration surfaces immediately rather than at run time.

## Set up a trigger (optional)

To run this job automatically whenever a CSV is uploaded:

1. Toggle **Enable trigger** in the job form
2. Set the file extension filter to `.csv`

The job will fire each time a matching file arrives, with the file path available as `LABRIC_FILE_PATH_0`.

## Write modes

The `mode` of a table write controls how its rows are written:

* **`insert`** (the default) creates every row.
* **`upsert`** updates rows that match an existing row on the `key` columns and creates the rest. Running the job again on the same file then updates rows instead of duplicating them.
* **`update`** and **`delete`** require every row to match an existing row on `key`.

```python
client.data.write(
    tables=[
        TableWrite(
            table="measurements",
            rows=records,
            mode="upsert",
            key=["sample_id"],
        )
    ],
)
```

A single `client.data.write` call can write several tables in one transaction, with rows that reference rows written earlier in the same call, and can write raw data series column by column. If any row is invalid, nothing is written and the error lists every problem by its place in the request. See the [write data reference](/api-reference/labric-api/data/write) for the full request.

## Run it

Click **Execute** on your job page, select a CSV file, and watch the execution log. Once it completes, your data will be available in the [Data](https://platform.labric.co/data) section.