---
title: How to upload files via RESTful API and link them to Attachments fields
slug: user-guide-10x/how-to-upload-files-via-restful-api-and-link-them-to-attachments-fields
docTags: 
createdAt: 2025-12-15T15:50:37.877Z
---

While it’s possible to upload attachments to records via the Swimlane Python driver, it’s also possible to do it directly by using the API. This guide will give a brief overview of how to use the RESTful API to upload files to a record and link them to an attachment field.

1. First, import the packages and connect to the RESTful API:

:::BlockQuote
from swimlane import Swimlane
import json

swimlane = Swimlane('HOST', 'USER', 'PASSWORD', verify\_ssl=False)
:::

2. Post the byte stream to the RESTful API:

:::BlockQuote
file\_name = 'api\_pod\_logs.txt'
file\_path = r'C:\user\Swimlane\api\_pod\_logs.txt'
resp = swimlane.request('post','/attachment', files=\{'file':(file\_name, open(file\_path, 'rb'))}, verify=False)
resp.raise\_for\_status()
rdata = resp.json()\[0]
:::

3. Finally, link the byte stream’s identifier to the Swimlane record:

:::BlockQuote
resp = swimlane.request('get', '/app/\{}/record/\{}'.format('aVGJPJ3E43TkwTQY5','aL\_KhxQqZkD1IwYNE'))
resp.raise\_for\_status()
rec\_DB12\_1 = resp.json()
rec\_DB12\_1\['values']\['afihc'].append(\{'$type':'Core.Models.Record.Attachment, Core', 'fileId'\:rdata\['fileId'], 'filename'\:file\_name, 'uploadDate'\:rdata\['uploadDate']})
resp = swimlane.request('put', '/app/\{}/record/\{}'.format('aVGJPJ3E43TkwTQY5','aL\_KhxQqZkD1IwYNE'), json=rec\_DB12\_1)
:::

Some additional notes:

- We’re only using the Swimlane driver to connect to the API. The rest is all done with API calls.
- When the file is uploaded, it’s stored in a segmented fashion by using MongoDB’s GridFS feature.
- The files are stored in two separate collections (fs\_files, and fs\_chunks)


The full code:

:::BlockQuote
from swimlane import Swimlane
import json

\# connect to RESTful API
swimlane = Swimlane('HOST', 'USER', 'PASSWORD', verify\_ssl=False)

\# post the byte stream to RESTful API
file\_name = 'api\_pod\_logs.txt'
file\_path = r'\<filename\_example>.txt'
resp = swimlane.request('post','/attachment', files=\{'file':(file\_name, open(file\_path, 'rb'))}, verify=False)
resp.raise\_for\_status()
rdata = resp.json()\[0]

\# link the byte stream's identifier to the Swimlane record
resp = swimlane.request('get', '/app/\{}/record/\{}'.format('aVGJPJ3E43TkwTQY5','aL\_KhxQqZkD1IwYNE'))
resp.raise\_for\_status()
rec\_DB12\_1 = resp.json()
rec\_DB12\_1\['values']\['afihc'].append(\{'$type':'Core.Models.Record.Attachment, Core', 'fileId'\:rdata\['fileId'], 'filename'\:file\_name, 'uploadDate'\:rdata\['uploadDate']})
resp = swimlane.request('put', '/app/\{}/record/\{}'.format('aVGJPJ3E43TkwTQY5','aL\_KhxQqZkD1IwYNE'), json=rec\_DB12\_1)
:::

