# API documentation — webfetch

Canonical HTML: https://webfetch.run/docs

Fetch a URL as formatted content, or open a persistent browser and control it with JavaScript through the API or MCP.

## Scrape

```
curl https://api.webfetch.run/v1/scrape \
  -H "X-API-Key: $SCRAPE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://www.allbirds.com/products/mens-wool-runners",
    "location": {"country": "US"}, "formats": ["markdown"],
    "egress": "residential",
    "location": {"country": "ES", "languages": ["es-ES", "es"]},
    "persist": true
  }'
```

### Request fields

| Field | Type | What it does |
| --- | --- | --- |
| `url` | string | Required. The page to fetch. Must be public http(s). |
| `formats` | string[] | Defaults to `["markdown"]`. Names are case-insensitive; an unknown name is a 400 that lists the valid ones. |
| `egress` | `"direct"` \| `"datacenter"` \| `"residential"` | Where the request leaves from. Defaults to `residential`. The price per fetch is the same either way. |
| `location` | `{country, languages?}` | Required. Visit from an ISO 3166-1 alpha-2 country such as `GB`. Optional languages set browser preferences. Explicit location uses the rendered fetch lane. |
| `onlyMainContent` | boolean | Defaults to true: narrows `html` and `markdown` to the main content. Set false for the whole DOM. |
| `waitForSelector` | string | Wait for this CSS selector before capturing. |
| `persist` | boolean | Store the capture and return a link that renders it from the webfetch origin. Off by default. |
| `rotations` | number | How many exits to try on a wall. Default 3, clamped 0–5. |
| `parallelProbes` | number | How many exits to try concurrently. Default 3, clamped 1–5. |
| `timeout` | number | Overall budget for the request, in milliseconds. Default 120000, clamped 5000–300000. |
| `session` | object | Replay a cleared session: `session.cookies` (and `egressPort`) from a previous response. |

GET `/v1/locations` lists all country codes and their configured egress routes. Explicit `egress` overrides must be configured for the country. An unavailable route returns `location_unavailable`. Successful targeted results include `egressCountry`, the country attributed to the selected route. This field reflects routing configuration.

### MCP tools

- `fetch` — Fetch a URL as formatted content (same work as `POST /v1/scrape`).
- `check_coverage` — Preflight only: whether a URL is public and http(s). Makes no request, costs nothing.
- `list_locations` — List country codes and configured egress routes. Costs nothing.
- `session_open` — Open an interactive browser. One session per account; five-minute maximum, 60-second idle close.
- `session_list` — See open session count and the account limit.
- `session_execute` — Run Playwright-style JavaScript against persistent page/context state. Each run has a 30-second limit.
- `session_close` — Close a session and refund the unused portion of its reserve.

MCP endpoint: `https://mcp.webfetch.run/mcp`

## Interactive browser sessions

Use this when a single scrape is not enough: open a real browser, keep its page and context between calls, and run Playwright-style JavaScript as the page changes.

- The account can hold one session at a time.
- It expires after five minutes or 60 seconds without activity.
- Each code execution is capped at 30 seconds.
- Session time is billed by the minute, with a documented one-minute minimum and a reserve that is settled when the session closes. See the live docs and pricing pages for current amounts; this file does not treat those amounts as a live checkout.

```
# Open a browser (optional country-targeted egress)
curl https://api.webfetch.run/v1/sessions \
  -H "X-API-Key: $SCRAPE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"url":"https://example.com","location":{"country":"ES","languages":["es-ES","es"]}}'

# Run JavaScript in the same page/context
curl https://api.webfetch.run/v1/sessions/$SESSION_ID/execute \
  -H "X-API-Key: $SCRAPE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"code":"await page.title()"}'

# Close early and settle the charge
curl -X DELETE https://api.webfetch.run/v1/sessions/$SESSION_ID \
  -H "X-API-Key: $SCRAPE_API_KEY"
```

REPL code has access to `page` and `context`, including page-side `evaluate`. Callback functions are limited to page-context evaluation so code cannot execute in the shared fleet process.

## Formats

Ask for any combination. You get back only what you asked for.

| Format | Returns | What it is |
| --- | --- | --- |
| `markdown` | string | The page as markdown. The default when no format is given. |
| `html` | string | Cleaned HTML. Narrowed by `onlyMainContent`, which defaults to true. |
| `rawHtml` | string | The document exactly as received, never narrowed. |
| `rawBase64` | string | The document bytes as base64. UTF-8 safe. |
| `text` | string | Visible text only, no markup. |
| `screenshot` | string | Base64 PNG of the rendered page. |
| `links` | string[] | Absolute URLs found on the page. |
| `images` | `{src,alt,...}[]` | Absolute image URLs with alt text and dimensions when present. |
| `metadata` | object | The page's head tags: title, description, og:*, canonical, language. |
| `branding` | object | Colours, fonts and logo, extracted deterministically. |
| `attributes` | object | Named attributes per CSS selector. Send `attributes: [{selector, attribute}]`. |
| `summary` | string | A short abstract, generated by a model. |
| `json` | object | Structured extraction. Send `jsonOptions: { schema }`. |
| `ai_overview` | object | The generated-answer card, expanded: markdown plus a PNG of that card. `present` is false when the page has none, including every non-Google URL. |

## The response

A successful 200 body includes `success: true` and `data` (requested formats plus metadata such as `url`, `finalUrl`, `title`, `status`, `verdict`, `attempts`, `bytes`, and `session`). It may include a `balance` object.

Keep `metadata.session` and send it back as `session` on the next request. A cleared session is bound to the exit that earned it. The docs report reusing it turned repeat queries from about 38s into about 6–8s.

## Errors

A failure is always `success: false` with a machine-readable code, and is not charged.

| Code | HTTP | Meaning |
| --- | --- | --- |
| `invalid_request` | 400 | The body did not validate — a missing `url`, an unknown format, a bad selector shape. |
| `target_not_allowed` | 400 | The URL is private, internal, or not a public http(s) address. |
| `egress_unavailable` | 400 | You asked for an egress tier this deployment has not configured. Refunded in full. |
| `insufficient_balance` | 402 | The balance cannot cover one fetch. Nothing ran, nothing was charged. |
| `blocked` | 200 | The site returned a wall instead of content. `success: false`, nothing charged. |
| `timeout` | 504 | The request exceeded its `timeout` budget. Nothing charged. |
| `fleet_unavailable` | 503 | The renderer could not be reached. Refunded in full. |

## Keys

Create a key in the console. The plaintext is shown once; only a SHA-256 hash is stored. Send it as `X-API-Key: sk_live_...`.

Related: [OpenAPI](https://webfetch.run/openapi.json), [agent.json](https://webfetch.run/agent.json), [llms.txt](https://webfetch.run/llms.txt), [pricing](https://webfetch.run/pricing).
