Uploading Files
How to use the Bipsync API to upload files.
There are multiple different ways of storing files in Bipsync. This page provides step-by-step guidance for each of them.
Uploading Files as Note Attachments
One approach to storing files in Bipsync is attaching them to notes. This involves either 2 or 3 API requests depending on whether you need to create the note, or whether you'd like to attach files to an existing note.
To upload multiple attachments to a note, steps 2 and 3 can be repeated for each file.
1. Create the note
If you would like to upload attachments to an existing note, skip to step 2. Otherwise, to create a note you need to make an HTTP POST request to /v2/research/note with a JSON body containing user as a minimum:
curl https://client-api.bipsync.com/v2/research/note \
--header "Token: {api_token}" \
--json \
--request POST \
--data '{"user": "{user_id}"}'const response = await fetch("https://client-api.bipsync.com/v2/research/note", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Token": "{api_token}"
},
body: JSON.stringify({
user: "{user_id}"
}),
});
// get the note ID
const {id: noteId} = await response.json();import requests, json
result = requests.post(
url='https://client-api.bipsync.com/v2/research/note',
json={
'user': '{user_id}'
},
headers={
'token': '{api_token}'
}
);
note = result.json()
# get the note ID from the result
noteId = note.get('id')The request can also include other properties listed in the API endpoint documentation in the JSON body, like fields .
If the request succeeded, you will receive a JSON response containing the note ID in the id property.
2. Create attachment metadata
The next step is to generate the attachment. This involves uploading some metadata about the attachment by making a POST request to the /v2/research/note/{id}/attachment endpoint, including:
fileName- the full name of the file e.g.,my-report.pdflength- the size of the file in bytesuser- the user to attribute the file touploadDate- optional upload date
curl -v https://client-api.bipsync.com/v2/research/note/{note_id}/attachment \
--header "Token: {api_token}" \
--json \
--request POST \
--data '{"user": "{user_id}", "fileName": "test.txt", "length": 14}'const attachmentResponse = await fetch(
`https://client-api.bipsync.com/v2/research/note/${noteId}/attachment`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
"Token": "{api_token}"
},
body: JSON.stringify({
user: "{user_id}",
fileName: "test.txt",
length: 14,
uploadDate: (new Date()).toISOString()
}),
}
);
// get the path where the file data should be uploaded
const uploadPath = attachmentResponse.headers.get('location');import requests
url = 'https://client-api.bipsync.com/v2/research/note/{noteId}/attachment'.format(
noteId=noteId
)
result = requests.post(
url=url,
json={
'user': '{user_id}',
'fileName': 'test.txt',
'length': 14
},
headers={
'token': '{api_token}'
}
);
# get the upload path from the location response header
uploadPath = result.headers.get('location')If the request succeeded, you should be provided with a path to upload the binary file data to in the location response header.
3. Upload the binary file data
Taking the value from the location header in the previous step, upload the binary file data using a multipart HTTP request:
curl https://client-api.bipsync.com{location_header_value} \
--header "Token: {api_token}" \
--request POST \
--form file='@filename'
import { openAsBlob } from "fs";
const formData = new FormData();
const fileBlob = await openAsBlob("test.txt");
formData.append("file", fileBlob, "test.txt");
const response = await fetch( `https://client-api.bipsync.com${uploadPath}`, {
method: "POST",
headers: {
"Token": "{api_token}"
},
body: formData
});
// check for a 200 status
const success = response.status === 200;import requests
url = 'https://client-api.bipsync.com{uploadPath}'.format(
uploadPath=uploadPath
)
result = requests.post(
url=url,
files={
'file': open('{file_name}', 'rb')
},
headers={
'token': '{api_token}'
}
);
# check for a 200 success result
success = result.status_code == 200Check for a 200 response status code to verify that the request was accepted.
Uploading Files as Research
Another approach to file storage in Bipsync is storing them as research themselves, rather than attachments to notes. In this case, you can make a single request to create the note and upload the file:
1. Upload the file and create the research
Make an HTTP POST request to /v2/research/file including the file binary data and user as form data. The multipart/form-data content type should be used:
curl https://client-api.bipsync.com/v2/research/file \
--header "Token: {api_token}" \
--request POST \
--form user={user_id} \
--form file='@filename'import { openAsBlob } from "fs";
const formData = new FormData();
const fileBlob = await openAsBlob("{file_path}");
formData.append("file", fileBlob, "{file_name}");
formData.append("user", "{user_id}");
const response = await fetch( "https://client-api.bipsync.com/v2/research/file", {
method: "POST",
headers: {
"Token": "{api_token}"
},
body: formData
});
// get the ID from the created note
const {id} = await response.json();import requests
result = requests.post(
url='https://client-api.bipsync.com/v2/research/file',
files={
'file': open('{file_name}', 'rb')
},
data={
'user': '{user_id}'
},
headers={
'token': '{api_token}'
}
);
# check for a 200 success result
success = result.status_code == 200Updated 18 days ago
