import requests
import hashlib
import os
def upload_file_for_template(file_path, api_key):
"""Upload a file and get file_id for template"""
# Read file and calculate hash
with open(file_path, 'rb') as f:
content = f.read()
file_hash = hashlib.sha256(content).hexdigest()
file_size = len(content)
# Get upload URL
response = requests.post(
"https://api.hopx.dev/v1/templates/files/upload-link",
headers={
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
},
json={
"files": [{
"path": os.path.basename(file_path),
"size": file_size,
"hash": f"sha256:{file_hash}"
}]
}
)
upload_info = response.json()["upload_urls"][0]
# Upload file to presigned URL
upload_response = requests.put(
upload_info["upload_url"],
data=content,
headers={"Content-Type": "application/octet-stream"}
)
upload_response.raise_for_status()
return upload_info["file_id"]
# Usage
file_id = upload_file_for_template("config.json", api_key)
print(f"File ID: {file_id}")