# screenshot()

Canonical URL: https://developers.iagenify.com/pt/sdk/ref/tools/screenshot/
Language: pt

`api.tools.screenshot({ url, viewport?, deviceScaleFactor?, wait?, timeout? }) → Promise<Screenshot>`

Capture a public page as an image only. Choose a viewport and wait interval; without a viewport the capture keeps the full page.

Status: POST /api/fetch · 1 credit · viewport or full page, JPEG

It is `fetch()` with `type: "screenshot"`, kept as its own name because that is how it reads at the call site and in a tool list.

## Parameters {#parameters}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `url` | string | required | The page to capture. Checked by the same guard as `fetch()`. |
| `viewport` | { width, height } | 1280 × 720 | CSS viewport, width 320–2560 and height 480–1600. Explicit viewport returns viewport-only pixels. |
| `deviceScaleFactor` | 1 | 2 | 3 | 1 | Use 1 for exact output dimensions; 2× or 3× increases raster dimensions. |
| `wait` | integer | 1000 | Additional time for rendering, 0–10000 ms. |
| `timeout` | integer | 20000 | Milliseconds. Clamped between 5000 and 60000. |

## Returns {#returns}

```javascript
{
  ok: true,
  type: "screenshot",
  title: "Example Domain",
  url: "https://example.com/",
  mimeType: "image/jpeg",                  // the image's, on this path
  timedOut: false,
  screenshot: { mimeType: "image/jpeg", base64: "/9j/4AAQSkZJRgABAQ…" },
  captureMs: 1_480,
  cached: false,
  creditsUsed: 1,
  timing: { durationMs: 1_700 }
}
```

With `viewport: { width: 1920, height: 1080 }` and `deviceScaleFactor: 1`, the returned raster is 1920 × 1080 pixels. That is a viewport capture, so content below the fold is not included. Omit `viewport` for the whole page; a long page then produces a tall image. JPEG at quality 86.

## Saving it {#writing}

```javascript
const shot = await api.tools.screenshot({ url: "https://example.com", viewport: { width: 1920, height: 1080 }, deviceScaleFactor: 1, wait: 1200 });
const bytes = Buffer.from(shot.screenshot.base64, "base64");

// To this machine
await pc.files.write("Captures/example.jpg", bytes);

// Or to the account
await api.files.create({ name: "example.jpg", content: bytes, mimeType: "image/jpeg" }).save();
```

## Which of the two to call {#which}

| You want | Call |
| --- | --- |
| The image only at an exact viewport | `screenshot({ url, viewport: { width: 1920, height: 1080 } })` |
| The text only | `fetch({ url })` |
| Both | `fetch({ url, screenshot: true })` — one credit |

> **Two calls for one page is two credits**
>
> screenshot() then fetch() on the same address is charged twice, since the two answers are cached apart. When you want the picture and the words, ask for them together.

## Here mimeType is the image; on a page capture it is the page {#mimetype}

On this path the top-level `mimeType` is `image/jpeg`, the same as `screenshot.mimeType`. Asking [fetch()](/pt/sdk/ref/tools/fetch) for an image with `screenshot: true` is the other case: the top-level field then describes the **page** that was loaded, and only `screenshot.mimeType` describes the image. Read the nested one in code that handles both.

## A capture that fails here fails the call {#failure}

> **The two paths part company on a failed capture**
>
> When the image is the whole point, a capture that cannot be taken — a page too tall, memory — raises fetch_failed and the credit is given back. On fetch({ screenshot: true }) the page is worth keeping, so the answer arrives with its text and the screenshot field carries an error instead of an image.

## Errors {#errors}

The same set as [fetch()](/pt/sdk/ref/tools/fetch): `url_required`, `url_refusee`, `invalid_url`, `fetch_failed`, and `NotEnoughCredits`. The credit is refunded before the failure is raised.

- [fetch()](/pt/sdk/ref/tools/fetch): The page as text, with its console and its TLS.
- [api.tools](/pt/sdk/ref/tools): The fifteen, and the two ways to use them.

```python
{
    "ok": True,
    "type": "screenshot",
    "title": "Example Domain",
    "url": "https://example.com/",
    "mimeType": "image/jpeg",                 # the image's, on this path
    "timedOut": False,
    "screenshot": {"mimeType": "image/jpeg", "base64": "/9j/4AAQSkZJRgABAQ…"},
    "captureMs": 1_480,
    "cached": False,
    "creditsUsed": 1,
    "timing": {"durationMs": 1_700},
}
```

```python
import base64

shot = api.tools.screenshot({"url": "https://example.com"})
data = base64.b64decode(shot["screenshot"]["base64"])

# To this machine
pc.files.write("Captures/example.jpg", data)

# Or to the account
api.files.create(name="example.jpg", content=data, mime_type="image/jpeg").save()
```
