---
agentTools:
  projectIndex: https://hub.phantombuster.com/llms.txt
---

# download()

download(url [, saveAs, headers, callback])

**Downloads a distant file to your agent's disk (not to your persistent storage).**

If you do not save the file to your persistent storage (see [buster.save()](https://hub.phantombuster.com/reference/buster-save) or [buster.saveFolder()](https://hub.phantombuster.com/reference/buster-savefolder)), **it will be lost when your agent exits**.

## — url `(String)`

URL of the file to be downloaded.

* `https://www.google.com/images/srpr/logo11w.png`
* `http://soundcloud.com/` (you'll get the HTML content of their homepage)

## — saveAs `(String)`

Where to put the file on your agent's disk (*optional*).
By default, the name will be taken from url and the file will be saved in the current working directory on your agent's disk.
If a file with the same name already exists, it is overwritten.

* `foo/ (saves http://example.com/baz/bar.png as foo/bar.png)`
* `null (saves http://example.com/foo/bar.png as bar.png)`
* `foo/ (fails on http://example.com/ with could not determine filename)`
* `foo/a (saves http://example.com/bar.png as foo/a)`

**Intermediate directories are not created automatically on your agent's disk.** When using NodeJS, create directories beforehand with [require('fs').mkdirSync()](https://nodejs.org/api/fs.html#fs_fs_mkdirsync_path_mode).

## — headers `(Plain Object)`

HTTP headers to use when requesting the file (*optional*).
Cookies are automatically set when using CasperJS or PhantomJS.

## — callback `(Function(err, path))`

Function called when finished (*optional*).

* **`err (String)`**: `null` or a description of what went wrong if something went wrong (typically if there was a network error or timeout).
* **`path (String)`**: the path to the file on your agent's disk.

[block:callout]
{
  "type": "info",
  "title": "Using NodeJS?",
  "body": "When running NodeJS within Phantombuster (the recommended binary), you also have access to common HTTP request modules such as `request`, `needle` and `fetch`. They can come in handy! See [packages & modules](https://hub.phantombuster.com/docs/packages).",
  "sidebar": true
}
[/block]

[block:code]
{
  "codes": [
    {
      "code": "const url = \"https://phantombuster.com\"\nconst saveAs = \"phantombuster.html\"\nconst headers = {\n  \"Accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\",\n  \"User-Agent\": \"Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/21.0\"\n}\n\ntry {\n  const path = await buster.download(url, saveAs, headers)\n  console.log(\"File successfully download here:\", path)\n  //The file is saved on the disk at this path, you can only access it inside the script\n} catch (err) {\n\tconsole.log(\"Could not download the file:\", err)\n}",
      "language": "javascript",
      "name": "Async/Await"
    },
    {
      "code": "const url = \"https://phantombuster.com\"\nconst saveAs = \"phantombuster.html\"\nconst headers = {\n  \"Accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\",\n  \"User-Agent\": \"Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/21.0\"\n}\n\nawait buster.download(url, saveAs, headers)\n.then((path) => {\n  console.log(\"File successfully download here:\", path)\n  //The file is saved on the disk at this path, you can only access it inside the script\n})\n.catch((err) => {\n\tconsole.log(\"Could not download the file:\", err)\n})",
      "language": "javascript",
      "name": "Promises"
    },
    {
      "code": "var url = \"https://phantombuster.com\"\nvar saveAs = \"phantombuster.html\"\nvar headers = {\n  \"Accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\",\n  \"User-Agent\": \"Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/21.0\"\n}\n\nawait buster.download(url, saveAs, headers, function(err, path) {\n\tif (err) {\n    console.log(\"Could not download the file:\", err)\n  } else {\n    console.log(\"File successfully download here:\", path)\n    //The file is saved on the disk at this path, you can only access it inside the script\n  }\n})",
      "language": "javascript",
      "name": "Callbacks (ES5)"
    }
  ],
  "sidebar": true
}
[/block]

[block:callout]
{
  "type": "warning",
  "title": "Note",
  "body": "This file will be only accessible from your script when it is running. If you want to keep the file in your persistent storage, use [buster.save()](#buster-save) or [buster.saveFolder()](https://hub.phantombuster.com/reference/buster-savefolder) or similar.",
  "sidebar": true
}
[/block]