Skip to main content
All CollectionsWorkflow editor - Automation in Labguru
Uploading files to experiments via API
Uploading files to experiments via API

Learn how to upload files and associate them with an attachment element

Malka Hoffmann avatar
Written by Malka Hoffmann
Updated over a week ago


When using the POST attachment API endpoint to upload a file to an experiment, the uploaded file will appear, by default, in the attachment box at the bottom of the experiment’s page:

If you want the attachment to appear in a specific section, the file must be associated with an attachment element.
If there's no attachment element in the target section, a new one can be created using the ‘POST element’ API endpoint.
Similarly, if there's no relevant section in the experiment, a new section can be created with the ‘POST section’ API request, followed by the creation of an attachment element.

Once the attachment element is established, the uploaded file can be linked to the newly created attachment element.


Below is a step-by-step guide for uploading a file to an experiment page, creating a section and an attachment element, and associating the uploaded file with the attachment element.

Step 1: Get the target experiment UUID

experiment_uuid = requests.get(f'{base_url}/api/v1/experiments/{experiment_id}', json={ "token" : token}).json()["uuid"]

Step 2: Upload the attachment

​file_name = 'file.csv'
​with open(file_name, 'rb') as file:
​ file_content = file.read()

​url = f"{base_url}/api/v1/attachments"
​data = {
'item[title]': file_name,
​ 'item[attachable_type]': 'Projects::Experiment',
​ 'item[attach_to_uuid]': experiment_uuid,
​ 'token': token}
​files = {'item[attachment]': (file_name, file_content)}
​attachment = requests.post(url=url, data=data, files=files)

Step 3: Get the uploaded attachment ID

attachment_id = attachment.json()["id"]

Step 4: Create a new section

response = requests.post(f'{base_url}/api/v1/sections',json={
'token': token,
'item': {
"name":"Section Name",
"container_id": experiment_id,
"container_type": "Projects::Experiment",
"position": 0,
"section_type": "custom"
}})
section = response.json()
​section_id = section["id"]

Step 5: Create an attachment element in the target section (indicating the target section ID in the container_id field)

response = requests.post(f'{base_url}/api/v1/elements', json={
​ 'token': token,
​ 'item': {
'element_type': 'attachments',
​ 'container_type': 'ExperimentProcedure',
​ 'container_id': section_id,
​ 'attachment_data': "[]", },
​ })
​attachment_element_id = response.json()['id']

Step 6: Associate the attachment with the attachment element

response = requests.put( f'https://{base_url}/api/v1/attachments/{attachment_id}/?token={token}', json={'item': {'element_id': attachment_element_id}}, )

Guru Tips

  • The above example is relevant for experiments but can be applied to different types of ELN objects such as Protocols, Reports, and Documents, by changing the 'attachable_type' in the 'POST attachment' (step 2) and 'POST section' (step 4) endpoints, as follows:
    - For Protocols:
    item[attachable_type]':'Knowledgebase::Protocol'
    - For Reports and Documents:
    item[attachable_type]': 'Knowledgebase::AbstractDocument'

  • If the attachment element already exists in the target experiment, you can skip steps 3-5 and associate the attachment with the existing attachment element by specifying the element's ID.

Did this answer your question?