Skip to content
AnswerWell geo Documentation

Research · 8 min read · Answerwell

Extension Downloads API: DownloadItem, Lifecycle and Search

How the Chrome extension downloads API starts downloads, tracks DownloadItem state, searches history, and controls files, pause, resume, and danger prompts.

This page summarises Google Chrome’s extension documentation for the downloads API (browser.downloads in the unified reference and the matching chrome.downloads surface in Manifest V2). Extension authors use it to start downloads from a URL, observe progress, pause or resume transfers, query history, and manage saved files—without reimplementing the browser’s download pipeline.

downloads API: what it is and when extensions need it

Section titled “downloads API: what it is and when extensions need it”

The downloads API lets an extension delegate file retrieval to Chrome: the browser handles HTTP(S) (including cookies for the target host), writes to the user’s Downloads folder, records history, and applies safe-browsing checks. You need it when your extension must save remote content, show progress in your own UI, or automate cleanup of download history while leaving files on disk.

The central object is DownloadItem: one row in download history with stable id, URLs, paths, byte counts, state, and safety metadata. Methods take a numeric downloadId that maps to DownloadItem.id.

download(): how to start a download from a URL

Section titled “download(): how to start a download from a URL”

browser.downloads.download(options) returns a Promise that resolves to the new item’s downloadId, or fails with runtime.lastError (and no id) when the download cannot start.

Download a URL. If the URL uses the HTTP[S] protocol, then the request will include all cookies currently set for its hostname. If both filename and saveAs are specified, then the Save As dialog will be displayed, pre-populated with the specified filename. If the download started successfully, callback will be called with the new DownloadItem’s downloadId. If there was an error starting the download, then callback will be called with downloadId=undefined and runtime.lastError will contain a descriptive string. The error strings are not guaranteed to remain backwards compatible between releases. Extensions must not parse it.

— Google Chrome, browser.downloads

Important DownloadOptions fields from the reference:

Option Role
url (required) Absolute URL to fetch
filename Path relative to the Downloads directory; absolute paths, empty paths, or .. cause an error
saveAs Opens a file chooser even when filename is set
method, body, headers For HTTP(S) only; headers follow XMLHttpRequest allowlists
conflictAction What to do if the target filename already exists

Manifest V2 documents the same behaviour on chrome.downloads.

DownloadItem: fields people query in code and in support threads

Section titled “DownloadItem: fields people query in code and in support threads”

DownloadItem is what search() returns and what change listeners describe (fully in deltas). Fields extension code relies on most often:

Field Meaning
state Whether the download is in progress, interrupted, or complete
paused, canResume Pause state and whether an interrupted download can resume
bytesReceived, totalBytes, fileSize Progress; -1 when size is unknown
url URL before redirects
finalUrl URL after redirects (Chrome 54+)
filename Absolute local path once known
mime Detected MIME type
danger Safe-browsing assessment (DangerType)
error InterruptReason when interrupted (prefixed SERVER_, NETWORK_, FILE_, USER_, …)
exists Whether the file is still on disk (may be stale; see below)
incognito Whether the item is omitted from normal history
byExtensionId, byExtensionName Set when the extension initiated the download

Times such as startTime, endTime, and estimatedEndTime are ISO 8601 strings suitable for new Date(...).

Whether the downloaded file still exists. This information may be out of date because Chrome does not automatically watch for file removal. Call search() in order to trigger the check for file existence. When the existence check completes, if the file has been deleted, then an onChanged event will fire. Note that search() does not wait for the existence check to finish before returning, so results from search() may not accurately reflect the file system. Also, search() may be called as often as necessary, but will not check for file existence any more frequently than once every 10 seconds.

— Google Chrome, browser.downloads

search(): how to find downloads and page through history

Section titled “search(): how to find downloads and page through history”

browser.downloads.search(query) returns matching DownloadItem objects. An empty query {} returns all items (subject to limit).

Find DownloadItem. Set query to the empty object to get all DownloadItem. To get a specific DownloadItem, set only the id field. To page through a large number of items, set orderBy: [‘-startTime’], set limit to the number of items per page, and set startedAfter to the startTime of the last item from the last page.

— Google Chrome, browser.downloads

DownloadQuery supports filters such as id, url, finalUrl, filename, state, mime, time ranges (startedAfter, startedBefore, endedAfter, endedBefore), size bounds, and regex filters (urlRegex, finalUrlRegex, filenameRegex). The query string array requires all non-dash terms to appear in filename, url, or finalUrl, and excludes items matching dash-prefixed terms.

Default limit is 1000; set limit to 0 to return all matches. Sort with orderBy (property names; prefix - for descending).

onChanged and DownloadDelta: how to track progress without polling

Section titled “onChanged and DownloadDelta: how to track progress without polling”

When any DownloadItem field changes, the downloads API can notify listeners with a DownloadDelta: the item’s id plus optional deltas (state, filename, totalBytes, exists, and others). This is the usual pattern for progress bars instead of hammering search().

Pair onChanged with occasional search({ id }) when you need a full snapshot after completion.

pause(), resume(), and cancel(): controlling an active transfer

Section titled “pause(), resume(), and cancel(): controlling an active transfer”
Method Effect
pause(downloadId) Stops reading from the host while keeping the connection open
resume(downloadId) Continues a paused download; fails if not active
cancel(downloadId) Cancels; the item may end cancelled, complete, interrupted, or missing

canResume is true when a download is paused in progress, or interrupted in a way Chrome can continue from the partial file.

acceptDanger(): how dangerous downloads complete

Section titled “acceptDanger(): how dangerous downloads complete”

Chrome may flag a file as suspicious. Data still lands in a temporary file until the user accepts the risk or the file is deemed safe.

Prompt the user to accept a dangerous download. Can only be called from a visible context (tab, window, or page/browser action popup). Does not automatically accept dangerous downloads. If the download is accepted, then an onChanged event will fire, otherwise nothing will happen. When all the data is fetched into a temporary file and either the download is not dangerous or the danger has been accepted, then the temporary file is renamed to the target filename, the state changes to ‘complete’, and onChanged fires.

— Google Chrome, browser.downloads

Call acceptDanger only from UI the user can see; it does not bypass the prompt by itself.

open(), removeFile(), show(), and erase(): files versus history

Section titled “open(), removeFile(), show(), and erase(): files versus history”
Method What it does
open(downloadId) Opens the file if state is complete; needs downloads.open permission and a user gesture
removeFile(downloadId) Deletes the on-disk file for a complete item
show(downloadId) Reveals the file in the system file manager
showDefaultFolder() Opens the default Downloads folder
erase(query) Removes matching items from history only; files stay on disk; fires onErased

Opens the downloaded file now if the DownloadItem is complete; otherwise returns an error through runtime.lastError. This method requires the “downloads.open” permission in addition to the “downloads” permission. An onChanged event fires when the item is opened for the first time. This method can only be called in response to a user gesture.

— Google Chrome, browser.downloads

onDeterminingFilename: how extensions suggest a save path

Section titled “onDeterminingFilename: how extensions suggest a save path”

If any extension registers onDeterminingFilename, a download() call’s filename option is ignored; listeners receive the tentative name and MIME type and call suggest() with an optional FilenameSuggestion (relative path under Downloads, optional conflictAction). Absolute paths, empty paths, and .. in suggestions are ignored.

Coordinate with other extensions: only one effective suggestion wins per download.

getFileIcon(downloadId, options?) returns a platform-dependent icon URL. Icons appear only after the target filename is known; calling too early sets runtime.lastError. In-progress icons may differ from the final icon.

browser.downloads versus chrome.downloads (MV2)

Section titled “browser.downloads versus chrome.downloads (MV2)”

For downloads specifically, the unified browser.downloads reference matches the MV2 chrome.downloads API shape: same DownloadItem, search paging recipe, and lifecycle methods. Prefer the namespace your manifest targets; behaviour described here applies to both unless Chrome’s platform notes say otherwise (for example finalUrl availability).

Other APIs in unrelated cluster snippets (desktop capture picker cancel, notifications clear, process terminate, context menu remove, media gallery watches) are not part of the downloads lifecycle; see their own reference pages if you need those features—for example browser.desktopCapture for cancelChooseDesktopMedia.

  • Parsing runtime.lastError strings after failed download() calls instead of handling failure generically.
  • Trusting exists immediately after search() without waiting for the asynchronous filesystem check (at most once per 10 seconds).
  • Calling open() without a user gesture or without the extra downloads.open permission.
  • Setting absolute filename paths in download() options (invalid); use paths relative to Downloads.
  • Expecting erase() to delete files—it only clears history entries.
  1. Load your unpacked extension with the "downloads" permission (and "downloads.open" only if you call open()), using the workflow described in browser.downloads.

  2. From a visible extension page, call browser.downloads.download({ url: 'https://example.com' }) and confirm the returned downloadId is a number.

  3. Run browser.downloads.search({ id: downloadId }) and verify state moves to complete (or interrupted with an error you can log).

  4. Open chrome://downloads in the browser and confirm the same URL, filename, and status match your DownloadItem fields.

  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page does not match the official Chrome references or you need a clarification, email yimasynthetic@gmail.com with the page URL and the exact sentence you are asking about.