> ## Documentation Index
> Fetch the complete documentation index at: https://docs.styne.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Drop-in for Tavily and Google Custom Search

> Two endpoints speak the request and response formats of other search APIs. Change the URL and the key; the code that reads the answer keeps working. Both are billed like POST /v1/search: per started block of 10 results, by mode, with errors and empty answers free.

[//]: # "smoke: not executed, the search endpoints are served by the main API host and bill the account that calls them"

A new key can call both. A key limited to chosen endpoints needs `compat` (shown as Drop-ins in the dashboard); without it the call answers `403 endpoint_not_allowed`.

## Tavily

`POST /compat/tavily/search` takes Tavily's request body and answers in Tavily's shape, plus a `styne` object with the cost, the balance and per-result dates and rights.

Before:

```python theme={"theme":"css-variables"}
import os
import requests

r = requests.post(
    "https://api.tavily.com/search",
    headers={"Authorization": f"Bearer {os.environ['TAVILY_API_KEY']}"},
    json={"query": "rate limiting RFC", "max_results": 5, "include_domains": ["rfc-editor.org"]},
    timeout=30,
)
for hit in r.json()["results"]:
    print(hit["title"], hit["url"], hit["content"])
```

After:

```python theme={"theme":"css-variables"}
import os
import requests

r = requests.post(
    "https://api.styne.ai/compat/tavily/search",
    headers={"Authorization": f"Bearer {os.environ['STYNE_API_KEY']}"},
    json={"query": "rate limiting RFC", "max_results": 5, "include_domains": ["rfc-editor.org"]},
    timeout=30,
)
for hit in r.json()["results"]:
    print(hit["title"], hit["url"], hit["content"])
```

```typescript theme={"theme":"css-variables"}
// before: https://api.tavily.com/search
const res = await fetch("https://api.styne.ai/compat/tavily/search", {
  method: "POST",
  headers: { Authorization: `Bearer ${process.env.STYNE_API_KEY}`, "Content-Type": "application/json" },
  body: JSON.stringify({ query: "rate limiting RFC", max_results: 5 }),
});
```

```bash theme={"theme":"css-variables"}
curl -sS https://api.styne.ai/compat/tavily/search \
  -H "Authorization: Bearer $STYNE_API_KEY" \
  -H "content-type: application/json" \
  -d '{"query": "rate limiting RFC", "max_results": 5}'
```

What it takes:

* `query`, `max_results` (up to 20, billed per started block of 10), `search_depth`, `chunks_per_source`, `time_range`, `days`, `start_date`, `end_date`, `include_domains` and `exclude_domains`, `language`.
* `search_depth` picks the mode: `basic`, `fast`, `ultra-fast` or none run `fast`; `advanced` runs `auto`. The mode served is in `styne.mode`.
* Fields Styne does not act on (`topic`, `include_answer`, `include_raw_content`, `include_images`, `country`, `exact_match`, `auto_parameters`) are accepted and listed in `styne.warnings`. Unknown fields are ignored, so newer client options never break a call.
* In the answer, `score` runs from 0 to 1 against the best hit, `published_date` is an HTTP date, `usage.credits` is the number of blocks billed, and `styne` holds `cost_usd`, `balance_usd` and per-result `published_at`, `crawled_at`, `tokens` and `rights`.

## Google Custom Search

`GET /compat/customsearch/v1` takes the Custom Search JSON API query string and answers in its shape, with a top-level `styne` object.

Before:

```bash theme={"theme":"css-variables"}
curl "https://www.googleapis.com/customsearch/v1?key=$GOOGLE_API_KEY&cx=$GOOGLE_CX&q=rate+limiting+rfc&num=5"
```

After:

```bash theme={"theme":"css-variables"}
curl "https://api.styne.ai/compat/customsearch/v1?key=$STYNE_API_KEY&cx=web&q=rate+limiting+rfc&num=5"
```

```python theme={"theme":"css-variables"}
import os
import requests

r = requests.get(
    "https://api.styne.ai/compat/customsearch/v1",
    params={"key": os.environ["STYNE_API_KEY"], "cx": "web", "q": "rate limiting rfc", "num": 5},
    timeout=30,
)
for item in r.json().get("items", []):
    print(item["title"], item["link"])
```

* `key` may be a query parameter on this path only (also as `access_token` or `oauth_token`), as Custom Search clients send it, or a Bearer header.
* `cx` is `web` (or empty) for the whole index, or the id of one of your saved site lists, which plays the part of the Custom Search engine. Any other `cx` answers `400 invalid_cx`.
* Every `cse.list` parameter is served; a parameter `cse.list` does not define answers `400`, as Google does.
* `q`, `num` (1 to 20), `start` (`start + num` at most 101), `dateRestrict`, `siteSearch` with `siteSearchFilter`, `lr`: as on Google.
* `exactTerms` and `excludeTerms` are index filters; `orTerms` runs one search per term (each requiring its term) and fuses them, billed per search; `hq` and `lowRange..highRange` are appended to the query; `fileType` matches the result URL's extension; `linkSite` searches the sites that link to that host (crawl link graph, per site).
* `gl` and `googlehost` prefer a country's sites; `cr=countryXX` keeps only them (`|` lists fuse one search per country); `safe=active` is moderate safe search; `rights` keeps openly licensed pages by `cc_*` licence trait.
* `sort=date` (hard sort, dated pages only), `date:a`, `date:d:s`, `date:r:YYYYMMDD:YYYYMMDD`; the order applies to the returned page.
* `searchType=image` answers Google's image items from the stored content images of the ranked pages; `imgSize` (by pixels), `imgType`, `imgColorType` and `imgDominantColor` (by format and the image's alt text, caption and file name) narrow them. `byteSize` is 0: Styne stores image addresses and sizes, not image bytes.
* `callback` wraps the answer in JSONP; `fields` gives Google's partial response (the `styne` object stays); `hl`, `filter`, `c2coff`, `prettyPrint`, `quotaUser`, `userIp`, `relatedSite` are checked and accepted.
* Like Google, `items` is left out when nothing matched.

## Errors

Errors keep the shape of the API being replaced and add Styne's `code` and `request_id`.

* Tavily: `{"detail": {"error": "<message>"}, "code": "<code>", "request_id": "<id>"}`.
* Custom Search: `{"error": {"code": <http status>, "message": "...", "status": "INVALID_ARGUMENT", "errors": [{"message": "...", "domain": "global", "reason": "<code>"}], "request_id": "<id>"}}`. The Styne code is the `reason`.

The codes are the ones on [Errors](https://docs.styne.ai/errors.md). Rate-limit headers and `Retry-After` are the same as everywhere.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.