# Extract

Structured data from pages by a JSON Schema.

Reads pages, or takes text you send, and returns data that matches your JSON Schema. The output is validated against the schema before it is returned. A value the pages do not contain is `null` when the schema allows `null`, and is never invented.

The schema supports `type`, `properties`, `required`, `items`, `enum`, `const`, `description`, `format` (`date`, `date-time`, `uri`, `email`), `minimum`, `maximum`, `minItems`, `maxItems` and `anyOf`. The root must be an object; the schema is at most 16 KB, depth 5 and 200 properties.

### What is charged

Each item that comes back with `status: ok` is charged: the page read and the extraction. Output that does not validate after our own retries is reported as `422 extraction_failed` and is free.

Units: `fetch_page`, `extract_page`. `usage.items` in the response lists them.

## Request

```
POST https://api.websearch.fast/v1/web/extract
```

Planned. Until it ships, calls return `501 api_not_implemented`.

### Body

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `urls` | array of string |  | 1 to 10 items. |
| `text` | string |  | 1 to 200,000 characters. |
| `schema` | object | yes | JSON Schema 2020-12 subset; root must be an object; at most 16 KB, depth 5, 200 properties. |
| `instructions` | string |  | At most 4000 characters. |
| `merge` | boolean |  | Default `false`. |
| `max_chars_per_page` | integer |  | 1000 to 200,000. Default `50000`. |
| `include_sources` | boolean |  | Default `true`. |
| `max_age` | integer |  | Accept a stored copy up to this age in seconds. 0 to 2,592,000. |
| `no_cache` | boolean |  | Never answer from a stored copy. Default `false`. |
| `timeout_ms` | integer |  | 5000 to 60,000. |

### Example request

curl:

```bash
curl https://api.websearch.fast/v1/web/extract \
  -H "Authorization: Bearer $WEBSEARCH_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "urls": ["https://shop.example.com/p/kettle-k2"],
    "schema": {
      "type": "object",
      "properties": {
        "name": {"type": "string"},
        "price": {"type": "number"},
        "currency": {"type": "string"},
        "in_stock": {"type": "boolean"}
      },
      "required": ["name"]
    }
  }'
```

Python:

```python
import os
import httpx

response = httpx.post(
    "https://api.websearch.fast/v1/web/extract",
    headers={"Authorization": f"Bearer {os.environ['WEBSEARCH_API_KEY']}"},
    json={
        "urls": ["https://shop.example.com/p/kettle-k2"],
        "schema": {
            "type": "object",
            "properties": {
                "name": {"type": "string"},
                "price": {"type": "number"},
                "currency": {"type": "string"},
                "in_stock": {"type": "boolean"},
            },
            "required": ["name"],
        },
    },
    timeout=60,
)
response.raise_for_status()
print(response.json())
```

TypeScript:

```typescript
const response = await fetch("https://api.websearch.fast/v1/web/extract", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.WEBSEARCH_API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    urls: ["https://shop.example.com/p/kettle-k2"],
    schema: {
      type: "object",
      properties: {
        name: { type: "string" },
        price: { type: "number" },
        currency: { type: "string" },
        in_stock: { type: "boolean" },
      },
      required: ["name"],
    },
  }),
});
if (!response.ok) throw new Error(`HTTP ${response.status}`);
console.log(await response.json());
```

### Example response

```json
{
  "object": "web.extract",
  "request_id": "20261128120002123456789a1b2c3d4Kx7Qm2Pc",
  "created": 1795867202,
  "input": {
    "urls": ["https://shop.example.com/p/kettle-k2"],
    "merge": false
  },
  "items": [
    {
      "url": "https://shop.example.com/p/kettle-k2",
      "status": "ok",
      "data": {
        "name": "Kettle K2",
        "price": 49.9,
        "currency": "EUR",
        "in_stock": true
      },
      "sources": {"price": ["https://shop.example.com/p/kettle-k2"]}
    }
  ],
  "schema_valid": true,
  "usage": {
    "charged": true,
    "items": [
      {"unit": "fetch_page", "count": 1},
      {"unit": "extract_page", "count": 1}
    ]
  }
}
```

### Response fields

Validated data.

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `object` | string | yes | Always `web.extract`. |
| `request_id` | string | yes | Same as the X-Request-Id header. |
| `created` | integer | yes | Unix seconds (UTC). |
| `input` | object | yes | The parameters as applied, defaults filled in. |
| `usage` | object | yes |  |
| `usage.charged` | boolean | yes |  |
| `usage.items` | array of object | yes |  |
| `usage.items.unit` | string | yes | Billable unit, for example search_web, content_page, fetch_page, geocode, autocomplete, knowledge. |
| `usage.items.count` | integer | yes |  |
| `cache` | object |  |  |
| `cache.hit` | boolean | yes | The response, or every page in it, came from a stored copy. |
| `cache.data_age_seconds` | integer | yes | Age of the oldest part of the response. |
| `cache.fetched_at` | integer | yes | Unix seconds when the oldest part was read. |
| `warnings` | array of object |  |  |
| `warnings.code` | string | yes | For example no_results, content_partial, answer_unavailable, partial, all_failed, reach_reduced. |
| `warnings.message` | string |  |  |
| `warnings.param` | string |  |  |
| `data` | object |  | With merge: true. |
| `items` | array of object |  |  |
| `items.url` | string |  |  |
| `items.status` | string | yes | One of `ok`, `failed`. |
| `items.data` | object |  |  |
| `items.error` | object |  |  |
| `items.error.code` | string | yes |  |
| `items.error.message` | string |  |  |
| `items.sources` | object |  | Top-level property to the URLs its value came from. |
| `sources` | object |  |  |
| `schema_valid` | boolean | yes | Always `true`. |

### Errors

| Status | Codes |
| --- | --- |
| `400` | `invalid_request`, `invalid_url`, `invalid_schema` or `invalid_image`. Not charged. |
| `401` | `invalid_token`. Not charged. |
| `403` | `pre_consume_token_quota_failed` (the key's own spending limit), `access_denied`, `web_not_allowed` or `model_not_in_plan` (customer keys while plans cannot include the web tools). Not charged. |
| `404` | `not_found` or `invalid_endpoint`. Not charged. |
| `413` | `read_request_body_failed`: the request body is over the limit. Not charged. |
| `415` | `unsupported_content`. Not charged. |
| `422` | `fetch_failed`, `site_refused`, `page_too_large` or `extraction_failed`. Not charged. |
| `429` | `rate_limited`, `concurrency_limited`, `api_key_temporarily_frozen`, or the failure guard. Not charged. |
| `500` | `internal_error`. Not charged. |
| `501` | `api_not_implemented`: the endpoint, type or option has not shipped. Not charged. |
| `503` | `source_unavailable`, `web_api_disabled`, `token_cache_busy` or `billing_busy` (the charge could not be prepared in time; retry after Retry-After). Not charged. |
| `504` | timeout. Not charged. |
