Code snippets examples
The following code-snippets can help you understand and implement better the upload files flow
Python
import requests
BASE_URL = "https://api.connecteam.com/attachments/v1/files"
# 1. Create upload URL
res = requests.post(f"{BASE_URL}/generate-upload-url", json={"fileName": "example.jpg", "fileTypeHint": "image/jpeg", "featureType": "chat"}).json()
file_url, file_id, headers = res["data"]["fileUrl"], res["data"]["fileId"], res["data"]["headers"]
# 2. Upload file
with open("example.jpg", "rb") as f:
requests.put(file_url, headers=headers, data=f)
# 3. Complete upload
requests.put(f"{BASE_URL}/complete-upload/{file_id}")
# 4. Get file status
print(requests.get(f"{BASE_URL}/{file_id}").json())
Javascript
const BASE_URL = "https://api.connecteam.com/attachments/v1/files";
async function uploadFile(file) {
const res = await fetch(`${BASE_URL}/generate-upload-url`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ fileName: file.name, fileTypeHint: file.type, featureType: "chat" })
}).then(res => res.json());
const { fileUrl, fileId, headers } = res.data;
await fetch(fileUrl, { method: "PUT", headers, body: file });
await fetch(`${BASE_URL}/complete-upload/${fileId}`, { method: "PUT" });
console.log(await fetch(`${BASE_URL}/${fileId}`).then(res => res.json()));
}
Ruby
require 'net/http'
require 'json'
BASE_URL = "https://api.connecteam.com/attachments/v1/files"
# 1. Create upload URL
uri = URI("#{BASE_URL}/create-upload-url")
res = Net::HTTP.post(uri, { fileName: "example.jpg", fileTypeHint: "image/jpeg", featureType: "chat" }.to_json, "Content-Type" => "application/json")
data = JSON.parse(res.body)["data"]
file_url, file_id, headers = data["fileUrl"], data["fileId"], data["headers"]
# 2. Upload file
file = File.read("example.jpg")
upload_uri = URI(file_url)
Net::HTTP.start(upload_uri.host, upload_uri.port, use_ssl: true) do |http|
req = Net::HTTP::Put.new(upload_uri)
headers.each { |key, value| req[key] = value }
req.body = file
http.request(req)
end
# 3. Complete upload
Net::HTTP.put(URI("#{BASE_URL}/complete-upload/#{file_id}"), "", "Content-Type" => "application/json")
# 4. Get file status
status_res = Net::HTTP.get(URI("#{BASE_URL}/#{file_id}"))
puts JSON.parse(status_res)
Bash
#!/bin/bash
# API Base URL
BASE_URL="https://api.connecteam.com/attachments/v1/files"
# File details
FILE_NAME="example.jpg"
FILE_TYPE="image/jpeg"
FEATURE_TYPE="chat"
# Step 1: Create upload URL
echo "Creating upload URL..."
CREATE_RESPONSE=$(curl -s -X POST "$BASE_URL/create-upload-url" \
-H "Content-Type: application/json" \
-d "{\"fileName\":\"$FILE_NAME\",\"fileTypeHint\":\"$FILE_TYPE\",\"featureType\":\"$FEATURE_TYPE\"}")
FILE_URL=$(echo "$CREATE_RESPONSE" | jq -r '.data.fileUrl')
FILE_ID=$(echo "$CREATE_RESPONSE" | jq -r '.data.fileId')
HEADERS=$(echo "$CREATE_RESPONSE" | jq -r '.data.headers | to_entries | map("-H \"" + .key + ": " + .value + "\"") | join(" ")')
echo "Upload URL: $FILE_URL"
echo "File ID: $FILE_ID"
# Step 2: Upload file
echo "Uploading file..."
curl -s -X PUT "$FILE_URL" $HEADERS --data-binary @"$FILE_NAME"
echo "File uploaded."
# Step 3: Complete upload
echo "Completing upload..."
COMPLETE_RESPONSE=$(curl -s -X PUT "$BASE_URL/complete-upload/$FILE_ID")
echo "Complete Upload Response: $COMPLETE_RESPONSE"
# Step 4: Get file status
echo "Retrieving file status..."
STATUS_RESPONSE=$(curl -s -X GET "$BASE_URL/$FILE_ID")
echo "File Status: $STATUS_RESPONSE"
Updated 1 day ago