> For the complete documentation index, see [llms.txt](https://docs.biodock.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.biodock.ai/public-api/examples.md). Agents can also search and read this documentation through its [MCP server](https://docs.biodock.ai/~gitbook/mcp).

# Examples

*What you'll get: three complete Python scripts, from uploading images to downloading the results.*

All three use the [`requests`](https://requests.readthedocs.io) library. Replace the empty values at the top with your own. To find the IDs, see [Finding IDs](https://docs.biodock.ai/public-api/authentication.md#find-ids).

{% hint style="info" %}
**In a workspace:** add `"X-Biodock-Workspace": "<workspace-url>"` to every `headers` dictionary below, or use a key that moved into the workspace with your team. See [Act in a workspace](https://docs.biodock.ai/public-api/authentication.md#act-in-a-workspace).
{% endhint %}

### Upload and analyse files from your computer

Upload each file, then submit one analysis job with all of the new file IDs.

```python
import os
import time

import requests

LOCAL_FILES = [""]  # Replace with your file paths
API_KEY = ""  # Replace with your API key
DESIRED_FOLDER = ""  # A folder path such as "experiment_1/day_2", or leave empty for the top level
PIPELINE_ID = ""  # Replace with your pipeline ID

UPLOAD_URL = "https://app.biodock.ai/api/external/filesystem-items/upload-file"
ANALYSIS_URL = "https://app.biodock.ai/api/external/analysis-jobs"


def post_with_retry(url, **kwargs):
    """Waits and retries when the rate limit (100 requests a minute) is reached."""
    while True:
        response = requests.post(url, **kwargs)
        if response.status_code != 429:
            return response
        time.sleep(60)


# Upload files, one request per file
biodock_file_ids = []
for my_file in LOCAL_FILES:
    with open(my_file, "rb") as file_to_upload:
        data = {"fileName": os.path.basename(my_file)}
        if DESIRED_FOLDER:
            data["destinationFolder"] = DESIRED_FOLDER
        headers = {"X-API-KEY": API_KEY}
        files = {"upload": file_to_upload}
        response = post_with_retry(UPLOAD_URL, data=data, headers=headers, files=files)
        response.raise_for_status()
        biodock_file_ids.append(response.json()["id"])

# Submit the analysis job
submit_headers = {"X-API-KEY": API_KEY, "Content-Type": "application/json"}
data = {"filesystemIds": biodock_file_ids, "pipelineId": PIPELINE_ID}
response = post_with_retry(ANALYSIS_URL, json=data, headers=submit_headers)
response.raise_for_status()
print("Analysis job ID:", response.json()["id"])
```

### Add files to an existing folder and analyse the whole folder

You already have a folder in Biodock, and some new files on your computer. Upload the new files into the folder, then analyse everything in it. Submitting a folder's ID analyses every file inside it, including in subfolders.

```python
import os

import requests

LOCAL_FILES = [""]  # Replace with your file paths
API_KEY = ""  # Replace with your API key
BIODOCK_FOLDER_PATH = ""  # The folder's path from the top level, e.g. "experiment_1/day_2"
BIODOCK_FOLDER_ID = ""  # The same folder's ID
PIPELINE_ID = ""  # Replace with your pipeline ID

UPLOAD_URL = "https://app.biodock.ai/api/external/filesystem-items/upload-file"
ANALYSIS_URL = "https://app.biodock.ai/api/external/analysis-jobs"

# Upload files into the folder
for my_file in LOCAL_FILES:
    with open(my_file, "rb") as file_to_upload:
        data = {
            "fileName": os.path.basename(my_file),
            "destinationFolder": BIODOCK_FOLDER_PATH,
        }
        headers = {"X-API-KEY": API_KEY}
        files = {"upload": file_to_upload}
        response = requests.post(UPLOAD_URL, data=data, headers=headers, files=files)
        response.raise_for_status()

# Analyse the whole folder
submit_headers = {"X-API-KEY": API_KEY, "Content-Type": "application/json"}
data = {"filesystemIds": [BIODOCK_FOLDER_ID], "pipelineId": PIPELINE_ID}
response = requests.post(ANALYSIS_URL, json=data, headers=submit_headers)
response.raise_for_status()
print("Analysis job ID:", response.json()["id"])
```

{% hint style="warning" %}
**Team moved into a workspace?** In a workspace, an upload's `destinationFolder` starts in your own folder from the move (*"\<your name>'s files"*), but listing files starts at the workspace's top level. Use the folder path as you see it from your own folder.
{% endhint %}

### Wait for a job to finish and download its results

Check the job every minute until it finishes, then download the per-object measurements as a CSV file. The download link works for 24 hours.

```python
import time

import requests

API_KEY = ""  # Replace with your API key
ANALYSIS_JOB_ID = ""  # The id returned when you submitted the job
OUTPUT_CSV = "objects.csv"

URL = f"https://app.biodock.ai/api/external/analysis-jobs/{ANALYSIS_JOB_ID}"
headers = {"X-API-KEY": API_KEY}

FINISHED = {"SUCCEEDED", "PARTIALLY_SUCCEEDED", "FAILED", "CANCELLED"}

while True:
    response = requests.get(URL, headers=headers)
    response.raise_for_status()
    job = response.json()["analysisJob"]
    print(job["status"], f'{job["percentageCompleted"]}%')
    if job["status"] in FINISHED:
        break
    time.sleep(60)

if job["status"] in {"SUCCEEDED", "PARTIALLY_SUCCEEDED"}:
    csv = requests.get(job["objectsDataCSVUrl"])
    csv.raise_for_status()
    with open(OUTPUT_CSV, "wb") as f:
        f.write(csv.content)
    print("Saved", OUTPUT_CSV)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.biodock.ai/public-api/examples.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
