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 functions with api.storeFile().
Basic Usage
const result = await api.storeFile(pdfBase64Content, // File content"shipping-label.pdf", // Filename{mimeType: "application/pdf", // Requiredencoding: "base64" // "base64" or "utf8"});if (result.success) {api.log(`Stored: ${result.fileUrl}`);// Save URL to your recordawait 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 labelconst 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 PDFconst labelBase64 = dhlResponse.labelImage;// Store it in Centraliconst result = await api.storeFile(labelBase64,`dhl-label-${order.data.orderNumber}.pdf`,{mimeType: "application/pdf",encoding: "base64",folder: "/shipping-labels"});// Update order with label URLawait 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 reportconst html = api.renderTemplate(reportTemplate, {orders: executionParams.orders,date: new Date().toISOString()});// Store as HTML fileconst result = await api.storeFile(html,`sales-report-${api.dayjs().format('YYYY-MM-DD')}.html`,{mimeType: "text/html",encoding: "utf8", // UTF-8 for textfolder: "/reports/sales"});
Downloaded Images
// Download product image from supplierconst imageResponse = await api.httpGet(supplierImageUrl,{ responseType: 'base64' });// Store in Centraliconst 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 serviceconst 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 PDFconst 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 accessawait api.storeFile(content, "invoice.pdf", {mimeType: "application/pdf",isPublic: false});// Public - anyone with URL can accessawait 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 flow
Limits
- Maximum file size: 100 MB
- Supported encodings: base64, utf8
Summary
api.storeFile() makes it easy to save files from 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 functions guide for more examples.