BackgroundURLSession provides APIs in the Scripting app for creating, resuming, and managing background download and upload tasks that continue running even when your script or app is not active.
Availability: Only available when your script is running in the main app (
Script.env === "index").
BackgroundURLSessionstartDownload(options): URLSessionDownloadTaskDescription: Starts a new background download task.
Signature
Parameters
url (string): The URL of the file to download.destination (string): The local file path where the downloaded file will be saved.headers (Record<string, string>, optional): Custom HTTP headers.notifyOnFinished ({ success: string, failure: string }, optional): Whether to show a local notification when the download finishes.Returns
URLSessionDownloadTask: A download task object (the task starts automatically).Example
resumeDownload(options): URLSessionDownloadTaskDescription: Resumes a previously paused or interrupted download task using resume data.
Signature
Parameters
resumeData (Data): The resume data returned by cancelByProducingResumeData().destination (string): The path to save the resumed download.notifyOnFinished ({ success: string, failure: string }, optional): Whether to show a local notification when the download finishes.Returns
URLSessionDownloadTask: A resumed download task (starts automatically).Example
getDownloadTasks(): Promise<URLSessionDownloadTask[]>Description: Retrieves all active or pending background download tasks currently managed by the system. Useful when your script is restarted or relaunched — you can restore callbacks and monitor ongoing tasks.
Signature
Returns
Promise<URLSessionDownloadTask[]>: An array of active URLSessionDownloadTask objects.Example
startUpload(options): URLSessionUploadTaskDescription: Starts a new background upload task.
Signature
Parameters
filePath (string): The local file path to upload.toURL (string): The destination server URL.method (string, optional, default "POST"): The HTTP method to use.headers (Record<string, string>, optional): Custom HTTP headers.notifyOnFinished ({ success: string, failure: string }, optional): Whether to show a local notification when the upload finishes.Returns
URLSessionUploadTask: An upload task object (the task starts automatically).Example
resumeUpload(options): URLSessionUploadTaskDescription: Resumes a paused or failed upload task using previously saved resume data (if supported by the server).
Signature
Parameters
resumeData (Data): Resume data returned from a previous incomplete upload.notifyOnFinished ({ success: string, failure: string }, optional): Whether to show a local notification when the upload finishes.Returns
URLSessionUploadTask: A new resumed upload task (starts automatically).Example
getUploadTasks(): Promise<URLSessionUploadTask[]>Description: Retrieves all active or pending background upload tasks currently managed by the system. Use this method after restarting your script to reattach callbacks to ongoing uploads.
Signature
Returns
Promise<URLSessionUploadTask[]>: An array of active URLSessionUploadTask objects.Example
resume() manually.task.suspend() and later call task.resume() to continue.cancelByProducingResumeData(). Upload resumability depends on server capabilities.getDownloadTasks() or getUploadTasks() to retrieve them and reattach event handlers.notifyOnFinished option only controls whether a notification is shown when a task completes; it does not affect task lifecycle or callbacks.