How-To: Upload Artifacts from a Mini-App
Goal: Save a file back to AuditSwarm.
Context: You have already retrieved the token (see Getting Started).
The 2-Step Upload Process
For security and performance, uploads happen in two steps:
- Ask for Permission: Request a "Signed Upload URL" from AuditSwarm.
- Upload Directly: Send the file to Google Cloud Storage using that URL.
Code Example
async function uploadFile(token, file) {
const API_BASE = 'https://demo.auditswarm.com/api/context';
// Step 1: Get Upload URL
const urlResponse = await fetch(`${API_BASE}/upload-url`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
fileName: file.name,
contentType: file.type
})
});
if (!urlResponse.ok) throw new Error('Failed to get upload URL');
const { uploadUrl } = await urlResponse.json();
// Step 2: Upload to Google Cloud (PUT)
const uploadResponse = await fetch(uploadUrl, {
method: 'PUT',
headers: {
'Content-Type': file.type
},
body: file
});
if (!uploadResponse.ok) throw new Error('Upload failed');
console.log('File uploaded successfully!');
}
Important Notes
- Draft Mode: Files are uploaded to a "Draft" area. They can be overwritten until the user "Approves" the node in AuditSwarm.
- Locked Artifacts: If the node is already approved/locked, Step 1 will return
403 Forbidden. You should handle this by disabling editing in your UI.