BackgroundURLSession
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").
Namespace: BackgroundURLSession
1) startDownload(options): URLSessionDownloadTask
Description: 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
2) resumeDownload(options): URLSessionDownloadTask
Description: Resumes a previously paused or interrupted download task using resume data.
Signature
Parameters
resumeData(Data): The resume data returned bycancelByProducingResumeData().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
3) 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 activeURLSessionDownloadTaskobjects.
Example
4) startUpload(options): URLSessionUploadTask
Description: 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
5) resumeUpload(options): URLSessionUploadTask
Description: 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
6) 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 activeURLSessionUploadTaskobjects.
Example
Usage Notes and Best Practices
- Start tasks: Tasks begin in a suspended state, so you need to call
resume()manually. - Pause and resume: You can temporarily pause a task using
task.suspend()and later calltask.resume()to continue. - Resume support: Download tasks can generate resume data via
cancelByProducingResumeData(). Upload resumability depends on server capabilities. - Task restoration: Even if your script is terminated, ongoing background tasks continue running. When restarted, use
getDownloadTasks()orgetUploadTasks()to retrieve them and reattach event handlers. - Local notifications: The
notifyOnFinishedoption only controls whether a notification is shown when a task completes; it does not affect task lifecycle or callbacks.
