# Website capture

Save a web page or your own HTML as a PDF or as one image with a single request. Each capture costs 1 credit.

A capture loads a web page and saves it as a PDF or as an image (`png`, `jpg` or `webp`). The page is loaded like a normal browser visit from our servers, with JavaScript on. Instead of an address you can send your own HTML, for example an invoice or a report, and get it back as a PDF.

Captures work with both APIs: `POST /v1/captures` in the simple API, and the task `capture-website` in jobs. The fields of the task are the same as in the CloudConvert API, so an existing job definition keeps working.

## Simple API

Send the address and the format. `?wait=30` waits up to 30 seconds for the result; without it, the answer is `202` and you poll `links.self`.

```bash
curl "https://api.switchfile.io/v1/captures?wait=30" \
  -H "Authorization: Bearer $SWITCHFILE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"url": "https://example.com/pricing", "output_format": "pdf", "options": {"page_format": "a4"}}'
```

The finished capture has `result.file.url`, a signed download link that is valid until `expires_at` (24 hours). An image has its size in pixels in `result.width` and `result.height`; a PDF has its page count in `result.pages`. See [POST /v1/captures](https://switchfile.io/docs/reference/post-v1-captures) for every field.

To turn your own HTML into a PDF, send `html` instead of `url`:

```bash
curl "https://api.switchfile.io/v1/captures?wait=30" \
  -H "Authorization: Bearer $SWITCHFILE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"html": "<h1>Invoice 1042</h1><p>Total: 120.00 EUR</p>", "output_format": "pdf", "options": {"page_format": "a4", "margin_top": 15, "margin_bottom": 15}}'
```

## Jobs (v2)

In a job, `capture-website` produces one file. Add an `export/url` task to get a download link:

```json
{
  "tasks": {
    "capture": {
      "operation": "capture-website",
      "url": "https://example.com/pricing",
      "output_format": "png",
      "device": "mobile"
    },
    "export": { "operation": "export/url", "input": "capture" }
  }
}
```

### HTML instead of a web page

Give exactly one of these three in a `capture-website` task:

- `url`: a public web page.
- `html`: an HTML document as text, at most 1 MB.
- `input`: the name of a task whose result is an HTML file (`.html` or `.htm`, at most 5 MB), for example from `import/upload`. The character set is detected from the file.

```json
{
  "tasks": {
    "upload": { "operation": "import/upload" },
    "capture": { "operation": "capture-website", "input": "upload", "output_format": "pdf", "page_format": "a4" },
    "export": { "operation": "export/url", "input": "capture" }
  }
}
```

HTML has no address of its own, so relative links such as `logo.png` do not load. Use absolute `https://` addresses for images, styles and fonts, or embed them as `data:` URLs. Everything the HTML loads follows the same rules as a web page: only public addresses on ports 80 and 443, and no local files. With `html` or `input`, the result has no `url`, and `cookies` cannot be used.

## Options

Every option is optional. Options that do not fit the format (for example `quality` with `png`) are rejected with a clear message, never ignored.

| Option | Formats | Default | Meaning |
| --- | --- | --- | --- |
| `filename` | all | from the address | File name of the result; the extension is added if missing |
| `device` | all | `desktop` | `mobile` uses a phone screen width with touch and the user agent of a phone browser |
| `wait_until` | all | `load` | When the page counts as loaded: `load`, `domcontentloaded`, `networkidle0` or `networkidle2` |
| `wait_for_element` | all | none | CSS selector that must appear before the capture |
| `wait_time` | all | `0` | Extra wait after loading in milliseconds, at most 10000 |
| `timeout` | all | none | Your own time limit in seconds; ours is lower if it is shorter |
| `headers` | all | none | Extra request headers as an object, see [Logins](https://switchfile.io/docs/capture-website#logins) |
| `cookies` | all | none | Cookies for the site of `url`, see [Logins](https://switchfile.io/docs/capture-website#logins) |
| `screen_width` | png, jpg, webp | `1440` | Width of the browser window in pixels (390 for `mobile`) |
| `screen_height` | png, jpg, webp | whole page | Height of the window. Without it, the whole page is captured |
| `device_scale_factor` | png, jpg, webp | `2` | Pixel density, 1 to 3 |
| `width`, `height` | png, jpg, webp | none | Size of the output image; `fit` sets how it is resized (`max`, `crop` or `scale`) |
| `quality` | jpg, webp | `80` | Image quality, 1 to 100 |
| `transparent_background` | png, webp | `false` | Keep a transparent page background |
| `page_format` | pdf | see note | Paper size, for example `a4` or `letter`. Without it (and without `page_width`), the size the page sets for printing, otherwise `letter` |
| `page_width`, `page_height` | pdf | none | Paper size in cm instead of `page_format` |
| `page_orientation` | pdf | `portrait` | `portrait` or `landscape` |
| `margin_top`, `margin_bottom`, `margin_left`, `margin_right` | pdf | `0` | Margins in mm |
| `zoom` | pdf | `1` | Scale of the page, 0.1 to 2 |
| `pages` | pdf | all | Page ranges, for example `1-3,5` |
| `print_background` | pdf | `true` | Print background colors and images |
| `display_header_footer` | pdf | `false` | Add a header with the date and title and a footer with page numbers |
| `header_template`, `footer_template` | pdf | none | Your own header or footer as HTML, see below. Turns on `display_header_footer` |
| `css_media_type` | pdf | `print` | `screen` renders the page as it looks in a browser |

### Header and footer

`header_template` and `footer_template` are HTML snippets of at most 20 KB that are printed on every page. Elements with the class `pageNumber`, `totalPages`, `date`, `title` or `url` are filled with that value. Scripts in a template do not run, and images load only as `data:` URLs. If you set only one of the two, the other stays empty. Leave room with `margin_top` and `margin_bottom`; the default font size of a template is very small, so set one.

```json
{
  "operation": "capture-website",
  "html": "<h1>Quarterly report</h1>",
  "output_format": "pdf",
  "margin_top": 20,
  "margin_bottom": 20,
  "footer_template": "<div style=\"font-size:9px;width:100%;text-align:center\">Page <span class=\"pageNumber\"></span> of <span class=\"totalPages\"></span></div>"
}
```

### Logins: headers and cookies

For pages behind a login, send the session as `cookies` or a token in `headers`:

```json
{
  "operation": "capture-website",
  "url": "https://app.example.com/reports/42",
  "output_format": "pdf",
  "cookies": [{ "name": "session", "value": "…" }],
  "headers": { "Authorization": "Bearer …" }
}
```

- `cookies` is a list of objects with `name` and `value`, and optionally `domain` (the host of `url` or one of its parent domains) and `path`. Without `domain`, a cookie goes to the host of `url` only, like a cookie the site set itself; with `domain`, it also goes to subdomains of that domain. Cookies are never sent to other sites the page loads. A cookie the browser rejects, such as a `__Host-` cookie for an `http` URL, fails the task with `INVALID_OPTIONS`.
- `headers` is an object of header names and values. Headers are sent with every request of the page, also to other sites it loads, such as image hosts. Put session data in `cookies` instead. A `Cookie` header is turned into cookies for the host of `url`. Headers that control the connection, such as `Host` or `Content-Length`, are rejected. Your own `User-Agent` replaces ours for every request; the browser then sends no `Sec-CH-UA` client hints, so it does not report a different browser than your `User-Agent` names.
- Both are stored encrypted and appear as `"[redacted]"` in every response and webhook. They are deleted when the job finishes successfully, otherwise after 24 hours. They never appear in logs.

### Not supported

| Field | Why |
| --- | --- |
| `close_cookie_banners` | Only `false` is accepted |
| `engine_version` | There is one version |

## Limits

| Limit | Value |
| --- | --- |
| Time to load the page | 30 seconds |
| Data the page may load | 50 MB, at most 1000 requests |
| Height of a full-page image | 20000 pixels (`webp`: 16383); longer pages are cut and `notes` says so |
| Size of the result | 50 MB |
| PDF pages | 200 |
| HTML in `html` | 1 MB (in `POST /v1/captures` the whole request may have 4 MB); as a file through `input`, 5 MB |
| Captures per minute | 30 per API key and 60 per IP address; without credits or with a sandbox key 5 and 10 |

Above the rate limit the API answers `429` with `RATE_LIMIT_EXCEEDED`; the `RateLimit` headers say when the minute resets. Retrying a capture task counts as a new capture.

Only addresses with `http` or `https` on port 80 or 443 work. Addresses that point to private or reserved networks are blocked. This also applies to every redirect and to every request the page makes, including frames.

Some websites show a check for automated visitors before the page. If the check finishes on its own and the website then opens the page, we wait up to 12 seconds from the check's first answer, as a normal browser would. We never solve captchas or click anything on such a page. A website that keeps the check or refuses the visit ends with `WEBSITE_BLOCKED`.

The wait for a check counts toward the 30 seconds for loading the page. If an image capture comes out blank, we keep capturing for up to 8 more seconds until content appears. An image that still shows no visible content fails with `EMPTY_CAPTURE` instead of returning an empty file. PDF captures are not checked for blank pages.

## Errors

A failed capture has `"status": "failed"` and an `error` with `code` and a full sentence in `message`.

| Code | Meaning |
| --- | --- |
| `FORBIDDEN_URL`  | The address or a port is not allowed, for example a private network |
| `INVALID_URL` | The host name does not exist, or the address leads to a file download |
| `INVALID_INPUT` | The `input` file is not an HTML file |
| `WEBSITE_BLOCKED`  | The website refused the visit (for example HTTP 403 or 429) or showed a check for automated visitors |
| `EMPTY_CAPTURE`  | The image showed no visible content, for example a blank check page or an app that never finished drawing. `wait_time` or `wait_until: networkidle0` can help |
| `DOWNLOAD_FAILED` | The website could not be reached or answered with an HTTP error |
| `TIMEOUT` | The page did not finish loading in time |
| `INPUT_TOO_LARGE` | The page loaded more data or made more requests than allowed |
| `FILE_TOO_LARGE` | The result is larger than 50 MB |

## Price

Each finished capture costs 1 credit, whatever the format and size. Failed captures cost nothing. With a [sandbox key](https://switchfile.io/docs/sandbox), captures are free.

## Next

- [Jobs and tasks](https://switchfile.io/docs/jobs-and-tasks): The CloudConvert-compatible switchfile API (v2). Jobs made of tasks, task status, results, retries and moving over from CloudConvert.
- [Limits and quotas](https://switchfile.io/docs/limits): Rate limits, file sizes, time limits, the free daily quota, quotas per visitor for service keys and how long files are kept.
