> This page is for Developer Documentation.

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

# Use APIs to process files with Benchling Connect

## Overview

This guide demonstrates how to leverage Benchling's API endpoints to:

1. Generate Input files
2. Insert Output files
3. Process (multiple) Benchling Connect runs within Notebook Entries

This is a summary of the configurations involved:

* A Notebook (NB) Entry that has been pre-populated with Run(s), eg: `RunA`, `RunB`, `RunC`
* Each of the Runs belongs to a different run schema (see the [docs](/docs/getting-started-1#copy-api-ids) on how to get the IDs):
  * RunA schemaId: `assaysch_pV6XCooH`
  * RunB schemaId: `assaysch_rF5GHolP`
  * RunC schemaId: `assaysch_PmvUoRMA`
* Each of the runs contains some `runfields` and `outputfileconfig(s)` which ingest files (eg: results from an instrument) and perform the following actions in Benchling:
  * Register entities
  * Add results against the entities
  * Transfer entities to a different location

> **Info**
>
> ### Why this page uses the V2 SDK
>
> [V3](/docs/v3-api-overview) is the recommended API for new development, but it has no endpoints for automation input generators, output processors, or file transforms; it exposes a run schema's automation file configs read-only. The V2 lab automation endpoints don't accept V3 IDs either: a V3 run ID is not a V2 `assay_run_id`, and a V3 attachment ID can't be used as an output processor's `fileId`. So every step on this page, including looking up runs and uploading the instrument file, uses the V2 Python SDK, `benchling-sdk`.

Install the SDK with `pip install benchling-sdk`. The examples below use this client, pointed at your tenant's root URL:

```python
from benchling_sdk.auth.api_key_auth import ApiKeyAuth
from benchling_sdk.benchling import Benchling

benchling = Benchling(
    url="https://yourcompany.benchling.com",
    auth_method=ApiKeyAuth("<your-api-key>"),
)
```

## Workflow Walkthrough

### A1. The user creates an Entry and starts working on the runs

> **Note**
>
> **Assumptions**
>
> * The NB entry is being created from a template (it can even be a request execution entry)
> * All the runs in the NB entry use different Run Schemas, which we see most frequently, so that's what we cover here

1. The user starts to work on the entry and adds information for the entities (samples, result table, reg tables etc.)
2. The user navigates to the run section(s) and clicks "**+**" → "create run". Let's call these RunA, RunB, RunC (in that order in the NB)
   1. If any fields are present for the run, the user fills them in and then creates the run

![A Benchling Connect run inside a notebook entry, with its run fields filled in](/_fern-files/_img/275e1da626153fa9a8c70d300d345dcd6f264e9e620a9e7d5711ecc4d239a6ea.webp)

3. The user continues their work; in the meanwhile we can look at how the API will now interact with this in the steps below

### A2. Get the Run ID and Entry ID the user is working on

> **Note**
>
> **Assumptions**
>
> Runs have already been created in the NB entry. This step must be done manually in the entry, or by
> creating a run with `benchling.assay_runs.create`
> and manually adding it from the [inbox](https://help.benchling.com/hc/en-us/articles/9714811662477-Unarchive-a-Lab-Automation-Run).

1. List the runs of a run schema, filtering on the schema ID (will be something like `assaysch_yzFj9plv`):

```python
for page in benchling.assay_runs.list(schema_id="assaysch_pV6XCooH"):
    for run in page:
        print(run.id, run.entry_id)
```

2. Each run's `entry_id` is the Notebook entry it sits in. It will look something like this: `etr_uMs6tSHK`
3. Each run's `id` will look something like this: `d9242e5a-9abc-4209-8e87-65eb2f765c00`, which we can now use for getting information about the run(s) and performing actions (below)
4. This way one can get the run ID for all the runs (RunA, RunB, RunC) across multiple notebook entries. To fetch one run you already know, use `benchling.assay_runs.get_by_id(run_id)`.

> **Note**
>
> Use the V2 run ID here. V3 run IDs use a different format and the V2 lab automation endpoints don't accept them. A V2 run's `v3_id` gives its V3 ID if you need to look the same run up through V3.

### B(X). Input File generation (optional)

This is an optional detour. Skip to [Step B1](#step-b1) to continue with `outputfileprocessing`.

Let's say the run you are interested in has an input file generation process. Follow the workflow below to create that file using the API.

> **Note**
>
> **Assumptions**
>
> The run has already been created.

1. List the run's automation input generators, using the run ID from step A2. Each generator's ID looks something like `aif_cvPqFaXD`.
2. Generate the input file. This starts a long-running task; `wait_for_response()` polls it and returns the updated input generator, or raises an error if the task failed. Failures here can arise from incorrect files, or errors in the automation runs.
3. The generated input file is a blob on the input generator's `file` field. Download it, here to a local path:

```python
from pathlib import Path

run_id = "d9242e5a-9abc-4209-8e87-65eb2f765c00"

input_generator = benchling.assay_runs.automation_input_generators(assay_run_id=run_id).first()

generated = benchling.lab_automation.generate_input(
    input_generator_id=input_generator.id
).wait_for_response()

print(generated.status)  # SUCCEEDED
benchling.blobs.download_file(generated.file.id, destination_path=Path("input-file.csv"))
```

If the run has more than one input file config, pick the generator whose `automation_file_config.name` matches the one you want instead of taking the first.

4. The file can be transferred to an instrument / external system for ingestion.
5. *(optional)* Click on the "refresh icon" ♻️ to refresh the run inside the NB and view the generated Input file in the UI.

### B1. Prepare the CSV file(s) for upload to Benchling \[#step-b1]

Now that we have the Entry (id) and Run (Id) we can start to send information for files coming from instruments / other systems in those run(s).

> **Note**
>
> **Assumptions**
>
> * The CSV files are already available
> * The matching of which CSV file to use for which Run/Entry combination is already done (eg: using the sample information in result tables, execution of a request etc.)

Upload each file as a blob. `create_from_file` switches to a multipart upload on its own for large files.

```python
from pathlib import Path

blob = benchling.blobs.create_from_file(
    Path("my_instrument_csv_file_runA.csv"),
    mime_type="text/csv",
)
print(blob.id)
```

The blob ID is the ID which we will need to process the Benchling Connect run in the next steps. Upload it with the V2 API: the output processor doesn't accept an attachment ID from the V3 [attachment upload](/docs/uploading-an-attachment-to-benchling).

> **Warning**
>
> **Don't lose your blob ID**
>
> There is no endpoint that lists blobs, so if you lose track of the blob ID, the file will need to be re-uploaded. So make sure to save it!

*This step (B1) can be repeated for all files for each of the run(s).*

### C. Process the run(s) with the uploaded CSV file(s)

Now that we know the run ID(s) and have the blob ID(s), we can start to process each of the runs using the respective file(s).

> **Note**
>
> **Assumptions**
>
> The CSV files have already been uploaded to Benchling and we have the blob IDs (from step B1 above).

### C1. Create Automation Output Processor

1. Create an output processor with `benchling.lab_automation.create_output_processor`, providing:
   * `assay_run_id`: ID of the run from step A2
   * `automation_file_config_name`: Name of the output file config (UI name)
   * `file_id`: the blob ID from step B1

```python
from benchling_sdk.models import AutomationOutputProcessorCreate

output_processor = benchling.lab_automation.create_output_processor(
    AutomationOutputProcessorCreate(
        assay_run_id="3da3a41b-3e92-4551-9c8b-644d5f465268",
        automation_file_config_name="1A. Process file",
        file_id="c33fe52d-fe6a-4c98-adcd-211bdf6778f7",
    )
)
print(output_processor.id)
```

2. The returned output processor's ID will look something like this: `aop_C3wGA9HF`

### C2. Process the generated Automation Output Processor

Now that the Automation Output Processor has been created, we can go ahead and "process" it.

1. Process the output processor created in step C1 (eg: `aop_C3wGA9HF`). This starts a long-running task; `wait_for_completion()` polls it until it finishes:

```python
completion = benchling.lab_automation.process_output(
    output_processor_id=output_processor.id
).wait_for_completion()

if completion.success:
    print("Processed:", completion.response.status)
else:
    print("Processing failed:", completion.message)
```

> **Info**
>
> * In case of errors, one can archive the output processor with `benchling.lab_automation.archive_automation_output_processors(automation_output_processor_ids=[...], reason=AutomationOutputProcessorsArchiveReason.MADE_IN_ERROR)`, importing the reason enum from `benchling_sdk.models`
> * Following that, one can point the output processor at a new file with `benchling.lab_automation.update_output_processor(output_processor_id, file_id)` (for example if you have uploaded a corrected file in step B1) and process it again

*The steps C1 and C2 can be repeated for all of the respective Run(s).*

### D. View the processed run(s) in the NB entry

> **Note**
>
> **Assumptions**
>
> The task from step C2 shows a `status` (for automation output processing) of `SUCCEEDED`.

1. After all the steps above (A–C) have been completed, one can navigate to the original NB entry and view / inspect the run
2. Click on the "refresh icon" ♻️ to refresh the run and view the generated results from output file processing.

> **Tip**
>
> Steps A–D can be repeated for all runs, across multiple NB entries.
>
> If you are looking to monitor the success of this process or keep track of which runs have been
> processed in which NB entries, you can log a [result](/docs/example-creating-results) with that
> information and track it in Benchling.