Filestack JavaScript SDK
Filestack-js is the official JavaScript SDK for integrating Filestack's comprehensive file management services into web and Node.js applications. It provides functionalities for secure content ingestion from various sources (local, URL, social media, cloud storage), powerful real-time transformations (image, video, document), and optimized delivery via a global CDN. Currently at version 3.48.4, the library receives regular updates to ensure compatibility with modern web frameworks and browsers, as well as to introduce new features like folder uploads and a mini uploader. Key differentiators include its highly customizable UI picker, built-in image editor, security features like virus scanning and content moderation, and simplified APIs for complex file workflows, aiming to streamline file handling from upload to delivery.
Common errors
-
{"error":"application is blocked"}cause This error typically indicates an issue with your Filestack account, most commonly an unpaid invoice.fixLog into your Filestack developer portal, check your billing status, and ensure all invoices are paid. Update any expired payment methods. -
TypeError: Picker is not a constructor (or similar 'Uncaught (in promise) Error: Filestack Picker Initialize Error')
cause This can occur if the Filestack picker attempts to mount into a DOM element that doesn't exist yet, especially with conditional rendering in frameworks like React, or if the library is not properly initialized.fixEnsure the DOM container for the picker is fully rendered before calling `client.picker().open()` or before the `PickerOverlay`/`PickerInline` component is mounted. If using a framework, ensure the component lifecycle correctly handles element availability. -
Content Security Policy (CSP) errors related to Filestack domains
cause Your application's Content Security Policy is blocking scripts, styles, images, or network requests from Filestack's domains (e.g., `filestackapi.com`, `static.filestackapi.com`).fixUpdate your CSP headers to explicitly allow `*.filestackapi.com` for `script-src`, `style-src`, `img-src`, `connect-src`, and `frame-src` directives, or other specific Filestack domains as needed by your integration. Consult Filestack documentation for required domains. -
Filestack SDK not working with ad blockers / trackers
cause Ad blockers or browser extensions designed to block trackers can interfere with the loading or functionality of Filestack's scripts, especially during initial signup or picker operations.fixAdvise users to temporarily disable ad blockers or try using a different browser or incognito/private mode to isolate the issue. Ensure your website doesn't have other blocking mechanisms.
Warnings
- breaking The Filestack Filepicker has evolved, with version 4.0 introducing significant enhancements like folder uploads and a mini uploader. While filestack-js is at v3.x, ensure your picker integration script links to the correct major version (e.g., `filestack-js/4.x.x/filestack.min.js` if using CDN, or update the npm package if using framework-specific SDKs like `filestack-react`). This may involve updating configuration options and UI integration patterns to leverage new features and avoid deprecated behavior.
- gotcha The `client.upload` method behaves differently in Node.js versus browser environments. In Node.js, it expects a file path, whereas in browsers, it anticipates a Blob object (e.g., from an `<input type='file'>` or drag-and-drop).
- gotcha When using `filestack-js` via a CDN in browser environments, it's crucial to implement Subresource Integrity (SRI) to protect against unexpected manipulation of the loaded library. Without SRI, a compromised CDN could serve malicious code.
- gotcha File uploads can fail due to various client-side issues, including incorrect filenames (e.g., special characters, spaces), incompatible file extensions not allowed by server configurations, or timeout issues during large file transfers over unstable networks.
Install
-
npm install filestack-js -
yarn add filestack-js -
pnpm add filestack-js
Imports
- filestack
const filestack = require('filestack-js');import * as filestack from 'filestack-js';
- Client
const client = new filestack.Client('YOUR_API_KEY');const client = filestack.init('YOUR_API_KEY'); - PickerOptions
import type { PickerOptions } from 'filestack-js';
Quickstart
import * as filestack from 'filestack-js';
const API_KEY = process.env.FILESTACK_API_KEY ?? 'YOUR_FILESTACK_API_KEY';
// Ensure a valid API key is set
if (API_KEY === 'YOUR_FILESTACK_API_KEY' || !API_KEY) {
console.error('Please replace YOUR_FILESTACK_API_KEY with your actual Filestack API key.');
console.error('You can get one from https://www.filestack.com/signup-start/');
// Exit or throw error in a real application
}
const client = filestack.init(API_KEY);
const pickerOptions: filestack.PickerOptions = {
maxFiles: 5,
accept: ['image/*', 'application/pdf'],
onUploadDone: (res) => {
console.log('Upload successful:', res.filesUploaded);
if (res.filesUploaded.length > 0) {
console.log('First file URL:', res.filesUploaded[0].url);
console.log('First file handle:', res.filesUploaded[0].handle);
}
},
onOpen: () => console.log('Picker opened.'),
onClose: () => console.log('Picker closed.'),
onError: (error) => console.error('Picker error:', error),
uploadInBackground: false,
transformations: {
crop: true,
rotate: true
}
};
async function openFilePicker() {
try {
const picker = client.picker(pickerOptions);
await picker.open();
console.log('File picker process initiated.');
} catch (error) {
console.error('Failed to open file picker:', error);
}
}
// In a browser environment, you might attach this to a button click:
// document.getElementById('uploadButton')?.addEventListener('click', openFilePicker);
// For demonstration, immediately open the picker if in a browser-like environment (e.g., CodeSandbox)
if (typeof window !== 'undefined') {
openFilePicker();
} else {
console.log('Running in Node.js environment. Picker UI cannot be displayed directly.');
}