# fetch()

Canonical URL: https://developers.iagenify.com/sdk/ref/tools/fetch/
Language: en

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

Opens a page in a real browser and hands back its text, its HTML, its console, its requests and its TLS — everything a page says about itself in one credit.

Status: POST /api/fetch · 1 credit · cached for an hour

This is not an HTTP request: the page is loaded and rendered, so what comes back is what a visitor would see — including whatever JavaScript built. That is also why there is no client-side timeout on the call.

## Parameters {#parameters}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `url` | string | required | The page. It must be public — see below. |
| `timeout` | integer | 20000 | Milliseconds. Clamped between 5000 and 60000. |
| `screenshot` | boolean | false | Also capture the image, in the **same** credit. |
| `type` | string | page | Use `screenshot` to return only an image; use `page` with `screenshot: true` to return text and image together. |
| `viewport` | { width, height } | 1280 × 720 | Responsive CSS viewport. For `type: "screenshot"`, explicitly setting it returns viewport-only pixels at that size when `deviceScaleFactor: 1`. |
| `deviceScaleFactor` | 1 | 2 | 3 | 1 | Raster density multiplier; 2× and 3× multiply output pixel dimensions. |
| `wait` | integer | 1000 | Extra visual settling time, 0–10000 ms. |
| `include` | string[] |  | Attach `console`, `network`, `links`, `forms`, `snapshot`, or `screenshot`. |

## Returns {#returns}

```javascript
{
  ok: true,
  type: "page",
  title: "Example Domain",
  url: "https://example.com/",
  mimeType: "text/html; charset=utf-8",
  timedOut: false,
  markdown: "# Example Domain\n\nThis domain is for use in…",

  elements: { head: "<meta …>", body: "<main>…</main>", css: "body{…}" },
  console:  [{ level: "error", message: "Uncaught TypeError…", source: "…/app.js", location: "14:9" }],
  sources:  [{ url: "https://example.com/app.js", method: "GET", status: 200,
               type: "script", mimeType: "text/javascript", duration: 84 }],

  performance: {
    navigation: { /* timings */ },
    paints: [ /* first paint, first contentful paint */ ],
    resources: { count: 24, totalBytes: 418_204, totalDuration: 1_902,
                 slowest: [{ url: "…", duration: 840, transferSize: 120_400 }] }
  },

  security: {
    https: true, protocol: "https",
    tls: { version: "TLS 1.3", cipher: "…", certificate: { subject, issuer, validFrom, validTo } },
    headers: { contentSecurityPolicy, strictTransportSecurity, xFrameOptions,
               xContentTypeOptions, referrerPolicy },
    mixedContent: [], cookies: { total: 3, secure: 3, httpOnly: 2, list: [ … ] },
    warnings: []
  },

  captureMs: 1_640,
  cached: false,
  creditsUsed: 1,
  timing: { durationMs: 1_902 }
}
```

## markdown is the field you usually want {#markdown}

The readable content, with the navigation, the scripts and the hidden elements taken out. It is what you pass to a model: `elements.body` is the HTML, which is several times larger for the same meaning.

## The rest is for diagnosing a page {#diagnosis}

| Field | Answers |
| --- | --- |
| `console` | What the page's own code complained about. Up to 500 entries. |
| `sources` | Every request it made, with its method, status, type and duration. Up to 500. A request that failed has `status: 0` and a `failure` — successful ones carry no such field. |
| `performance.resources.slowest` | The five requests that cost the most time. |
| `security.tls` | The certificate and the protocol version. |
| `security.headers` | Five headers that decide what a browser allows. |
| `security.mixedContent` | Insecure sub-resources on a secure page. |
| `security.warnings` | Mixed content and certificate failures, already gathered. |

```javascript
const page = await api.tools.fetch({ url: "https://example.com" });

const broken = page.console.filter((l) => l.level === "error");
const failed = page.sources.filter((s) => s.failure);
const slow = page.performance.resources.slowest;

if (page.security.warnings.length) console.warn(page.security.warnings);
```

## The page and its image, one credit {#screenshot}

`screenshot: true` adds the image to a page capture without a second call and without a second credit. The flag is part of the cache key, so asking for it on a page captured without it does a fresh capture rather than handing back one with no image.

```javascript
const page = await api.tools.fetch({ url, screenshot: true });
// page.screenshot → { mimeType: "image/jpeg", base64: "…" }   full page, JPEG
// A capture that could not be taken arrives as { error: "…" } — the page is still there
```

## timedOut does not mean failed {#timedout}

> **A page that never settles is still returned**
>
> When the ceiling is reached, what had loaded is captured and timedOut is true. A long-polling page or an endless animation would otherwise cost a credit for nothing — here you get the content and the warning.

## Private addresses are refused {#guard}

The address is checked before anything is opened: the scheme must be http or https, and the host must resolve to a public address. A redirect does not escape the check.

That matters most when the URL comes from a model or from a page's own text: it is the difference between reading the web and being asked to read something that is not meant to be read.

## What is charged {#billing}

| Situation | creditsUsed |
| --- | --- |
| A capture | 1 |
| The same page within the hour, same flags | 0 — `cached: true` |
| The capture failed | 0 — refunded, then `fetch_failed` |

## Errors {#errors}

| Code | Status | When |
| --- | --- | --- |
| url_required | 400 | `url` is missing or is not a string. |
| url_refusee | 400 | The address is refused by the guard. |
| invalid_url | 400 | Not a usable address. |
| fetch_failed | 502 | The page could not be opened. The credit is given back first. |
| NotEnoughCredits | 402 | The balance will not cover one credit. |

- [screenshot()](/sdk/ref/tools/screenshot): The image alone, when the text is not the point.
- [The agent's fetch](/sdk/agent/tools/fetch): The same capture, with the console and the slowest requests attached.
- [api.tools](/sdk/ref/tools): The fifteen, and the two ways to use them.

```python
{
    "ok": True,
    "type": "page",
    "title": "Example Domain",
    "url": "https://example.com/",
    "mimeType": "text/html; charset=utf-8",
    "timedOut": False,
    "markdown": "# Example Domain\n\nThis domain is for use in…",

    "elements": {"head": "<meta …>", "body": "<main>…</main>", "css": "body{…}"},
    "console": [{"level": "error", "message": "Uncaught TypeError…", "source": "…/app.js", "location": "14:9"}],
    "sources": [{"url": "https://example.com/app.js", "method": "GET", "status": 200,
                 "type": "script", "mimeType": "text/javascript", "duration": 84}],

    "performance": {
        "navigation": {},                       # timings
        "paints": [],                           # first paint, first contentful paint
        "resources": {"count": 24, "totalBytes": 418_204, "totalDuration": 1_902,
                      "slowest": [{"url": "…", "duration": 840, "transferSize": 120_400}]},
    },

    "security": {
        "https": True, "protocol": "https",
        "tls": {"version": "TLS 1.3", "cipher": "…", "certificate": {"subject": ..., "issuer": ..., "validFrom": ..., "validTo": ...}},
        "headers": {"contentSecurityPolicy": ..., "strictTransportSecurity": ..., "xFrameOptions": ...,
                    "xContentTypeOptions": ..., "referrerPolicy": ...},
        "mixedContent": [], "cookies": {"total": 3, "secure": 3, "httpOnly": 2, "list": []},
        "warnings": [],
    },

    "captureMs": 1_640,
    "cached": False,
    "creditsUsed": 1,
    "timing": {"durationMs": 1_902},
}
```

```python
page = api.tools.fetch({"url": "https://example.com"})

broken = [line for line in page["console"] if line["level"] == "error"]
failed = [s for s in page["sources"] if s.get("failure")]
slow = page["performance"]["resources"]["slowest"]

if page["security"]["warnings"]:
    print(page["security"]["warnings"])
```

```python
page = api.tools.fetch({"url": url, "screenshot": True})
# page["screenshot"] → {"mimeType": "image/jpeg", "base64": "…"}   full page, JPEG
# A capture that could not be taken arrives as {"error": "…"} — the page is still there
```
