> 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

client = Labric()

file_path = os.environ["LABRIC_FILE_PATH_0"]

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

client.tools.write(
    target_type="table",
    target_name="measurements",
    data=records,
    mode="create",
    job_execution_id=os.environ["LABRIC_JOB_EXECUTION_ID"],
)

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`. If the table doesn't exist, Labric creates it automatically.

## 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` parameter controls how data is written:

* **`create`** — Insert new rows. Use `batch_insert_ok=True` for better performance on large datasets.
* **`create-or-update`** — Upsert. Matches existing rows by the fields you specify in `params_to_match_for_update`, updates them if found, inserts if not.

```python
client.tools.write(
    target_type="table",
    target_name="measurements",
    data=records,
    mode="create-or-update",
    params_to_match_for_update=["sample_id"],
    job_execution_id=os.environ["LABRIC_JOB_EXECUTION_ID"],
)
```

## 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.