Compute functions are great for processing data and calling APIs. But what happens when those APIs return binary files like PDFs, images, or documents?
Previously, you'd need to store file references and fetch them separately, or pass base64 data through your client. Neither option is ideal.
Now you can store files directly from compute functions with api.storeFile().
Basic Usage
const result = await api.storeFile(
pdfBase64Content, // File content
"shipping-label.pdf", // Filename
{
mimeType: "application/pdf", // Required
encoding: "base64" // "base64" or "utf8"
}
);
if (result.success) {
api.log(`Stored: ${result.fileUrl}`);
// Save URL to your record
await api.updateRecord(orderId, {
shippingLabelUrl: result.fileUrl
});
}Real Example: DHL Shipping Labels
One of the most common use cases is storing shipping labels from carrier APIs:
async function run() {
const order = await api.fetchRecord(executionParams.orderId);
// Call DHL to generate label
const dhlResponse = await api.httpPost('https://api.dhl.com/parcel/labels', {
headers: {
'Authorization': `Bearer ${triggerParams.dhlApiKey}`
},
data: {
shipperAddress: triggerParams.warehouseAddress,
recipientAddress: order.data.shippingAddress,
weight: order.data.totalWeight,
service: 'EXPRESS'
}
});
// DHL returns base64-encoded PDF
const labelBase64 = dhlResponse.labelImage;
// Store it in Centrali
const result = await api.storeFile(
labelBase64,
`dhl-label-${order.data.orderNumber}.pdf`,
{
mimeType: "application/pdf",
encoding: "base64",
folder: "/shipping-labels"
}
);
// Update order with label URL
await api.updateRecord(executionParams.orderId, {
shippingLabelUrl: result.fileUrl,
trackingNumber: dhlResponse.trackingNumber,
status: 'shipped'
});
return {
success: true,
data: { trackingNumber: dhlResponse.trackingNumber }
};
}Parameters
| Parameter | Required | Description |
|---|---|---|
content | Yes | File content (string) |
filename | Yes | Name for the file |
options.mimeType | Yes | MIME type (e.g., "application/pdf") |
options.encoding | No | "base64" or "utf8" (default: "utf8") |
options.folder | No | Target folder (default: "/root/shared") |
options.isPublic | No | Public access (default: false) |
Return Value
// Success
{
success: true,
fileUrl: "https://storage.centrali.io/workspace/...",
renderId: "abc123..."
}
// Error
{
success: false,
error: "File size exceeds 100MB limit"
}More Use Cases
Generated Reports
// Generate HTML report
const html = api.renderTemplate(reportTemplate, {
orders: executionParams.orders,
date: new Date().toISOString()
});
// Store as HTML file
const result = await api.storeFile(
html,
`sales-report-${api.dayjs().format('YYYY-MM-DD')}.html`,
{
mimeType: "text/html",
encoding: "utf8", // UTF-8 for text
folder: "/reports/sales"
}
);Downloaded Images
// Download product image from supplier
const imageResponse = await api.httpGet(
supplierImageUrl,
{ responseType: 'base64' }
);
// Store in Centrali
const result = await api.storeFile(
imageResponse.data,
`product-${sku}.jpg`,
{
mimeType: "image/jpeg",
encoding: "base64",
folder: "/products/images",
isPublic: true // For public product pages
}
);Invoice PDFs
// Generate invoice with PDF service
const pdfResponse = await api.httpPost('https://api.pdfshift.io/v3/convert', {
headers: {
'Authorization': `Basic ${api.base64.encode(`api:${triggerParams.pdfshiftKey}`)}`
},
data: {
source: invoiceHtml,
landscape: false,
format: 'A4'
}
});
// Store the PDF
const result = await api.storeFile(
pdfResponse.data,
`invoice-${invoiceNumber}.pdf`,
{
mimeType: "application/pdf",
encoding: "base64",
folder: `/invoices/${year}`
}
);Public vs Private Files
// Private (default) - requires auth to access
await api.storeFile(content, "invoice.pdf", {
mimeType: "application/pdf",
isPublic: false
});
// Public - anyone with URL can access
await api.storeFile(content, "product-photo.jpg", {
mimeType: "image/jpeg",
isPublic: true
});Error Handling
Always check the result:
const result = await api.storeFile(content, filename, options);
if (!result.success) {
api.logError({
message: 'Failed to store file',
error: result.error,
filename
});
return { success: false, error: result.error };
}
// Continue with success flowLimits
- Maximum file size: 100 MB
- Supported encodings: base64, utf8
Summary
api.storeFile() makes it easy to save files from compute functions:
- Call external API that returns a file
- Store it with
api.storeFile() - Save the URL to your records
No more workarounds, no more client-side file handling for server operations.
Check the compute functions guide for more examples.