import { v4 as uuidv4 } from "uuid";
const API_KEY = "YOUR_API_KEY";
const BASE_URL = "https://api.agentoffice.dev";
async function processDocument(file) {
// 1. Upload document
const formData = new FormData();
formData.append("file", file);
const uploadResponse = await fetch(`${BASE_URL}/v1/documents/`, {
method: "POST",
headers: { Authorization: `Bearer ${API_KEY}` },
body: formData,
});
const { docId } = await uploadResponse.json();
console.log("Uploaded document:", docId);
// 2. Apply edit
const editResponse = await fetch(`${BASE_URL}/v1/documents/${docId}/edits`, {
method: "POST",
headers: {
Authorization: `Bearer ${API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
editUid: uuidv4(),
editInstructions: "Update all dates to 2024",
}),
});
const editResult = await editResponse.json();
console.log("Edit applied:", editResult.editApplied);
// 3. Download edited document
const downloadResponse = await fetch(`${BASE_URL}/v1/documents/${docId}`, {
headers: { Authorization: `Bearer ${API_KEY}` },
});
const { downloadUrl, filename } = await downloadResponse.json();
// Download the file
const fileResponse = await fetch(downloadUrl);
const blob = await fileResponse.blob();
// Trigger browser download
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = filename;
a.click();
console.log("Downloaded:", filename);
}