# Crawl parameters

Every field of a crawl request, the job it returns, and the results you poll for.

```text
POST   https://api.millionshort.com/v1/crawl          start a crawl
GET    https://api.millionshort.com/v1/crawl          your team's crawls, newest first
GET    https://api.millionshort.com/v1/crawl/{id}     one crawl, with a page of its results
DELETE https://api.millionshort.com/v1/crawl/{id}     cancel
```

## Request

| Field | Type | Default | Notes |
|---|---|---|---|
| `url` | string | | Required. Where the crawl starts: `http` or `https`, default ports, a host name (not an address), no credentials in the URL. Always crawled |
| `max_pages` | integer, 1 to your plan's ceiling | `100`, or the ceiling if it is lower | The most pages charged: the pages that came back, plus any failed page that is charged (a page a browser mode found missing). Credits are held for this many, and a crawl never costs more than was held. See [Limits of a plan](#limits-of-a-plan) |
| `max_depth` | integer, 0 to 10 | `3` | How many links away from the start page to go. `0` is that page alone. Sitemap URLs are depth 1 |
| `include` | array of up to 20 patterns | | Crawl only URLs whose path matches one of these. The start page is crawled whatever they say |
| `exclude` | array of up to 20 patterns | | Never crawl URLs whose path matches one of these. Wins over `include` |
| `same_domain` | boolean | `true` | Stay on the host you named. `false` also follows links to other sites |
| `include_subdomains` | boolean | `false` | Also crawl other hosts of the same site |
| `sitemap` | `include`, `skip`, `only` | `"include"` | `include`: sitemap URLs and links. `skip`: links only. `only`: the start page and the sitemap's URLs, and links are not followed |
| `mode` | `cached`, `live`, `rendered`, `unblock` | `"live"` | How each page is obtained. See [Modes](#modes) |
| `formats` | array of `markdown`, `text`, `links`, `metadata` | `["markdown","metadata"]` | What to return for each page, as for [Extract](https://millionshort.com/docs/extract/parameters) |
| `max_chars` | integer, 1 to 100,000 | `50000` | The longest content per page. Longer content is cut and `truncated` is `true`. Lower than Extract's 500,000, because a crawl keeps every page for a week |
| `topics`, `page_types`, `source_hints` | arrays of slugs | | Keep only the pages with these [labels](https://millionshort.com/docs/extract/labels) |
| `json` | object with `schema` and an optional `prompt` | | Each page as JSON of your shape, on the plans that include it. Extract's limit of three URLs a call does not apply to a crawl; the daily limits on pages read as JSON do |
| `remove_top_sites` | integer, 0 to 1,000,000 | `0` | Only with `same_domain: false`: links to the N most popular sites are not followed |
| `webhook_id` | string | | An active [webhook endpoint](https://millionshort.com/docs/webhooks) of your team. It is told when the crawl ends |

Any other field is a `400 INVALID_PARAM`. A caller cannot send cookies, headers or a delay to a site, and there is no option to ignore robots.txt.

`include` and `exclude` are regular expressions on the path of a URL, not anchored, as for [Map](https://millionshort.com/docs/map/parameters#patterns): `/docs/` matches any path that contains it, `^/docs/` only paths that start with it.

An `Idempotency-Key` header is honoured for 24 hours: the same key with the same body returns the crawl it started, and the same key with another body is a `409 IDEMPOTENCY_CONFLICT`.

### Modes

| Page | How it is obtained | Credits per page |
|---|---|---|
| A crawled page, `live` | Fetched now, as our declared bot | 1 |
| A crawled page, `rendered` | Loaded in a browser, so content built by scripts appears. Paid plans | 2 |
| A crawled page, `unblock` | A rendered fetch for sites that refuse automated requests. Growth plan and above | 26 |
| A crawled page, `cached` | The copy we already hold. No page is requested from the site | 1 |

- robots.txt is checked for every page we fetch, in `live`, `rendered` and `unblock`. A page it closes to our bot is not fetched, and costs nothing.
- A crawl has no `auto` and no `stealth` mode, and no mode is ever chosen for you.
- A `cached` crawl asks the site for its robots.txt and its sitemaps and for no page. Its URLs are the sitemap's and the ones our index holds for the site. Links are not followed, because a stored copy carries none. A page we do not hold comes back as `NOT_IN_CACHE`, free. `cached` cannot be combined with `same_domain: false`, and it is switched off for now, as it is for Extract.
- A `cached` crawl reads the site's robots.txt first, with `sitemap: "skip"` too. When it closes the start URL to our bot, the crawl ends `failed` with `ROBOTS_DISALLOWED`; when it cannot be read, with `ROBOTS_UNAVAILABLE`. Nothing is served or charged. The stored copies themselves are served as Extract's `cached` serves them: robots.txt is not asked about each page.

### Scope

| You send | Hosts crawled |
|---|---|
| nothing | The host of `url`, with or without `www.` |
| `include_subdomains: true` | Every host of the same site, such as `docs.example.org` and `example.org` |
| `same_domain: false` | Any host a crawled page links to |

With `same_domain: false`, every other site is fetched under its own robots.txt and its own limits. `remove_top_sites: 1000` then leaves out links to the 1,000 most popular sites, so a crawl does not wander off to the large platforms every page links to. When popularity cannot be looked up for a part of the crawl, no off-site link of that part is followed.

## The crawl

`POST` answers `202` with the crawl. `GET /v1/crawl/{id}` and `DELETE` answer with the same object.

| Field | Type | Notes |
|---|---|---|
| `id` | string | Starts with `crawl_` |
| `status` | `queued`, `running`, `completed`, `cancelled`, `failed` | |
| `url`, `mode`, `max_pages`, `max_depth` | | As the crawl runs them. `max_pages` is your plan's ceiling when the default was above it |
| `total` | integer | URLs found so far |
| `completed` | integer | Pages that came back |
| `failed` | integer | URLs that were tried and gave no page. Free, except a page a browser mode found missing (see [Pricing](https://millionshort.com/docs/crawl/pricing)) |
| `skipped` | integer | URLs found and never tried, once the crawl has ended |
| `credits_held` | number | What was held when the crawl started |
| `credits_used` | number | What the crawl has been charged so far |
| `stop_reason` | string or null | Why the crawl ended. See [Errors](https://millionshort.com/docs/crawl/errors#why-a-crawl-stopped) |
| `unread_sources` | array of `sitemap`, `index` | Present when a list of URLs could not be read at the start. The crawl goes on with the links |
| `webhook_id` | string or null | |
| `created_at`, `finished_at` | date-times | |
| `expires_at` | date-time or null | When the results are deleted: 7 days after the crawl ended |
| `error` | object | `code` and `message`, on a crawl that failed |

## Poll

```bash
curl https://api.millionshort.com/v1/crawl/$CRAWL_ID \
  -H "Authorization: Bearer $MILLIONSHORT_API_KEY"
```

```python
import os

import requests

r = requests.get(
    f"https://api.millionshort.com/v1/crawl/{os.environ['CRAWL_ID']}",
    headers={"Authorization": "Bearer " + os.environ["MILLIONSHORT_API_KEY"]},
    timeout=30,
)
r.raise_for_status()
print(r.json()["data"])
```

```ts
const res = await fetch(`https://api.millionshort.com/v1/crawl/${process.env.CRAWL_ID}`, {
  headers: { Authorization: `Bearer ${process.env.MILLIONSHORT_API_KEY}` },
});
if (!res.ok) throw new Error(`GET /v1/crawl/… failed: ${res.status}`);
const { data } = (await res.json()) as { data: unknown };
console.log(data);
```

`GET /v1/crawl/{id}` adds two fields to the crawl:

| Field | Type | Notes |
|---|---|---|
| `results` | array | The pages finished so far, in the order they finished, nearest the start first. Up to 50 at a time, and at most 4 MiB |
| `next` | string or null | Pass as `?cursor=` to carry on from here. While the crawl runs it is set once any page has been read, even when no more pages are there yet; `null` before the first page has finished, and once the crawl has ended and its last page was read |

Each result is an [Extract result](https://millionshort.com/docs/extract/parameters#response) plus `depth`: its content in the formats you asked for, `mode_used`, the [labels](https://millionshort.com/docs/extract/labels), and the `site`, `popularity` and `risk` of the page's site. A URL that failed has `status: "error"` and one of the [per-URL codes](https://millionshort.com/docs/crawl/errors#per-page-errors).

Polling is free and does not count against the plan's requests a minute. It has its own limit of 120 a minute per team. `usage.credits` on a poll is what the crawl has been charged so far.

```bash
curl "https://api.millionshort.com/v1/crawl?limit=20" \
  -H "Authorization: Bearer $MILLIONSHORT_API_KEY"
```

```python
import os

import requests

r = requests.get(
    "https://api.millionshort.com/v1/crawl?limit=20",
    headers={"Authorization": "Bearer " + os.environ["MILLIONSHORT_API_KEY"]},
    timeout=30,
)
r.raise_for_status()
print(r.json()["data"])
```

```ts
const res = await fetch('https://api.millionshort.com/v1/crawl?limit=20', {
  headers: { Authorization: `Bearer ${process.env.MILLIONSHORT_API_KEY}` },
});
if (!res.ok) throw new Error(`GET /v1/crawl failed: ${res.status}`);
const { data } = (await res.json()) as { data: unknown };
console.log(data);
```

`GET /v1/crawl` lists your team's crawls, newest first, without results, in `data.crawls`. `limit` is 1 to 100 and defaults to 20. Free.

## Cancel

```bash
curl -X DELETE https://api.millionshort.com/v1/crawl/$CRAWL_ID \
  -H "Authorization: Bearer $MILLIONSHORT_API_KEY"
```

```python
import os

import requests

r = requests.delete(
    f"https://api.millionshort.com/v1/crawl/{os.environ['CRAWL_ID']}",
    headers={"Authorization": "Bearer " + os.environ["MILLIONSHORT_API_KEY"]},
    timeout=30,
)
r.raise_for_status()
print(r.json()["data"])
```

```ts
const res = await fetch(`https://api.millionshort.com/v1/crawl/${process.env.CRAWL_ID}`, {
  method: 'DELETE',
  headers: { Authorization: `Bearer ${process.env.MILLIONSHORT_API_KEY}` },
});
if (!res.ok) throw new Error(`DELETE /v1/crawl/… failed: ${res.status}`);
const { data } = (await res.json()) as { data: unknown };
console.log(data);
```

The crawl becomes `cancelled`. The pages that came back are kept and stay charged, and the rest of the hold is handed back. Cancelling a crawl that has ended changes nothing.

## Events

With `webhook_id`, one event is delivered when the crawl ends: `crawl.completed` for a crawl that is `completed` or `cancelled`, `crawl.failed` for one that failed. It carries `id`, `status`, `stop_reason`, `url`, `total`, `completed`, `failed` and `credits_used`. It does not carry the pages: fetch them with `GET /v1/crawl/{id}`. Events are signed and retried as described on [Webhooks](https://millionshort.com/docs/webhooks).

## How a crawl runs

- Pages are fetched nearest the start first, and each URL once: a fragment is dropped, and `http` and `https`, and a path with and without its last slash, are one page. The query string is kept, because it can name another page.
- Links that are not pages by their name, such as images, style sheets, archives and media files, are not followed.
- A page waiting for a busy site waits its turn, at no cost, as a page of an [Extract batch](https://millionshort.com/docs/extract/batch#how-a-batch-runs) does.
- A crawl tries at most 3 URLs for each page it may return, plus 20. Failures are free, and they are not endless.
- A crawl stops with `max_pages` once it has been charged for `max_pages` pages. In `rendered` and `unblock` a page the browser found missing is charged and counts towards that, so the hold is never passed.
- At about 30 pages of one site a minute, a large crawl takes hours.
- With `topics`, `page_types` or `source_hints`, a page that does not pass has `filtered: true` and no content. It was fetched, so it is charged, and it counts as `completed`.
- A crawl that ends with no page at all is `failed`, and its `error` is that of its first URL.
- A crawl that has not ended 24 hours after it was started becomes `failed`. The pages that finished are kept, and what is left of the hold is handed back.

## Limits of a plan

| | Free | Starter | Growth | Scale | Business |
|---|---|---|---|---|---|
| `max_pages`, at most | 50 | 500 | 2,000 | 10,000 | 25,000 |
| Crawls open at once | 1 | 2 | 3 | 5 | 10 |
| `rendered` | No | Yes | Yes | Yes | Yes |
| `unblock` | No | No | Yes | Yes | Yes |

A `max_pages` over the ceiling is a `400 INVALID_PARAM` whose `details.max` is the ceiling (over 25,000, the highest ceiling, it is a plain `400 INVALID_PARAM`). One crawl more than the plan may have open is a `429 RATE_LIMITED`.

On one site the daily limit ends a crawl before a large ceiling does: a team gets 2,000 fetches of a site a day, sitemap files included, so a crawl of one site stops with `stop_reason: "site_daily_limit"` at about 2,000 pages, whatever the plan.

## Credits

A crawled page costs 1 credit in `cached` and `live`, 2 in `rendered` and 26 in `unblock`, charged for the pages that came back. A URL that fails is free, with one exception in the browser modes. [Pricing and limits](https://millionshort.com/docs/crawl/pricing) has the table and examples.
