# `extract_by_schema` — Extract Records by Declarative CSS Schema

> Part of [DOM Harvest & Table Grid Edge API](https://dom-harvest.apikeg.com/llms.txt) (`com.apikeg/dom-harvest`)

Deterministic CSS-selector record extractor supporting descendant/child/attribute/group selectors, relative URL resolution against baseUrl, numeric currency/comma coercion, boolean flags, inner HTML serialization, and multi-value arrays.

**Why call instead of DIY:** Eliminates brittle ad-hoc cheerio/regex glue code and runs identically at the Cloudflare edge in <10ms for $0.001.

- **REST Endpoint:** `POST https://dom-harvest.apikeg.com/v1/extract-by-schema`
- **MCP Server:** `https://dom-harvest.apikeg.com/mcp` (tool: `extract_by_schema`)
- **Pricing:** $0.001 USD per declarative CSS-schema extraction (x402 instant micropayment)
- **Free Dry-Run:** `POST https://dom-harvest.apikeg.com/v1/extract-by-schema?dry_run=true`

## Input JSON Schema
```json
{
  "type": "object",
  "properties": {
    "html": {
      "type": "string",
      "minLength": 1,
      "maxLength": 4000000,
      "description": "Raw HTML page or fragment."
    },
    "baseUrl": {
      "type": "string",
      "maxLength": 2048,
      "description": "Optional page URL used to resolve relative URLs when field type is 'url'."
    },
    "itemSelector": {
      "type": "string",
      "minLength": 1,
      "maxLength": 512,
      "description": "CSS selector matching each repeating record element (or 'body' / 'html' for single-object page extraction)."
    },
    "fields": {
      "type": "object",
      "additionalProperties": {
        "type": "object",
        "properties": {
          "selector": {
            "type": "string",
            "minLength": 1,
            "maxLength": 512,
            "description": "CSS selector evaluated within each matched item element (use ':scope' to target the item element itself)."
          },
          "attribute": {
            "type": "string",
            "minLength": 1,
            "maxLength": 128,
            "description": "Optional HTML attribute name to read (e.g. 'href', 'src', 'data-price'). Omit to extract text or inner HTML."
          },
          "type": {
            "type": "string",
            "enum": [
              "string",
              "number",
              "url",
              "boolean",
              "html"
            ],
            "default": "string",
            "description": "Coercion type: 'string', 'number' (strips currency/commas), 'url' (resolves against baseUrl), 'boolean', or 'html'."
          },
          "multiple": {
            "type": "boolean",
            "default": false,
            "description": "When true, extracts an array across all matching elements inside each item instead of the first match."
          }
        },
        "required": [
          "selector"
        ],
        "additionalProperties": false,
        "description": "Declarative extraction rule for a single field."
      },
      "description": "Map of output property names to declarative CSS selector + type coercion rules."
    }
  },
  "required": [
    "html",
    "itemSelector",
    "fields"
  ],
  "additionalProperties": false
}
```

## Examples
### Extract typed e-commerce product records
```bash
curl -X POST "https://dom-harvest.apikeg.com/v1/extract-by-schema" \
  -H "Content-Type: application/json" \
  -d '{"baseUrl":"https://store.example.com/deals/","itemSelector":"article.product-card","fields":{"sku":{"selector":":scope","attribute":"data-sku","type":"string"},"title":{"selector":"h3.title","type":"string"},"url":{"selector":"a.card-link","attribute":"href","type":"url"},"price":{"selector":".price-current","type":"number"},"inStock":{"selector":".badge-in-stock","type":"boolean"},"tags":{"selector":"ul.tags > li","type":"string","multiple":true}},"html":"<div class='grid'><article class='product-card' data-sku='SKU-9'><a class='card-link' href='../p/sku-9'><h3 class='title'>Pro Dock</h3></a><span class='price-current'>$189.99</span><span class='badge-in-stock'>In Stock</span><ul class='tags'><li>USB4</li><li>100W PD</li></ul></article></div>"}'
```
Expected output: Returns 1 typed item with url='https://store.example.com/p/sku-9', price=189.99, inStock=true, tags=['USB4', '100W PD'].
