# Autocomplete

Query suggestions for a prefix.

Returns query suggestions for the start of a query, for a search box or for an agent that refines its own query. Suggestions come from open encyclopedic data: names of people, places, organizations and things, and article titles.

`q` is the prefix, at most 256 bytes and 200 characters. Leading spaces are removed; a trailing space is kept and asks for the next word. `num` is 1 to 20, default 10. `language` is detected from `q` when it is not sent. `timeout_ms` is 500 to 5000, default 5000. `location` is not accepted.

The response lists `suggestions` in rank order without repeats, the `language` they were looked up in, and `attribution` with the open data sets they come from. Show the attribution as you show other licence notices; the [licences](https://websearch.fast/licenses) page lists the data sets.

### What is charged

One `autocomplete` unit per answer with at least one suggestion, whatever their number. An empty list carries the warning `no_results` and is free, as is every error.

Units: `autocomplete`. `usage.items` in the response lists them.

## Request

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

### Body

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `q` | string | yes | The prefix, at most 200 characters. Leading spaces are removed; a trailing space asks for the next word. 1 to 256 characters. |
| `country` | string |  | ISO 3166-1 alpha-2 country code, case-insensitive. |
| `language` | string |  | BCP 47 language tag. Detected from q when absent. At most 12 characters. |
| `num` | integer |  | 1 to 20. Default `10`. |
| `timeout_ms` | integer |  | Upper bound for the call. 500 to 5000. Default `5000`. |

### Example request

curl:

```bash
curl https://api.websearch.fast/v1/web/autocomplete \
  -H "Authorization: Bearer $WEBSEARCH_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"q": "marie cu", "num": 3}'
```

Python:

```python
import os
import httpx

response = httpx.post(
    "https://api.websearch.fast/v1/web/autocomplete",
    headers={"Authorization": f"Bearer {os.environ['WEBSEARCH_API_KEY']}"},
    json={
        "q": "marie cu",
        "num": 3,
    },
    timeout=60,
)
response.raise_for_status()
print(response.json())
```

TypeScript:

```typescript
const response = await fetch("https://api.websearch.fast/v1/web/autocomplete", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.WEBSEARCH_API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    q: "marie cu",
    num: 3,
  }),
});
if (!response.ok) throw new Error(`HTTP ${response.status}`);
console.log(await response.json());
```

### Example response

```json
{
  "object": "web.autocomplete",
  "request_id": "20261128120000223456789a1b2c3d4Kx7Qm2Pa",
  "created": 1795867200,
  "input": {"q": "marie cu", "num": 3, "timeout_ms": 5000},
  "language": "en",
  "suggestions": [
    {"value": "Marie Curie"},
    {"value": "Marie Curie Hospice"},
    {"value": "Marie Curie Medal"}
  ],
  "attribution": [
    {
      "source": "Open encyclopedic data",
      "url": "https://data.example.org/",
      "license": "CC BY-SA 4.0"
    }
  ],
  "usage": {
    "charged": true,
    "items": [{"unit": "autocomplete", "count": 1}]
  }
}
```

### Response fields

Suggestions.

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `object` | string | yes | Always `web.autocomplete`. |
| `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 |  |
| `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 |  |  |
| `suggestions` | array of object | yes |  |
| `suggestions.value` | string | yes |  |
| `language` | string |  | Language of the suggestions. |
| `attribution` | array of object |  | The open data projects the data comes from, with their licences. Show it with the data. |
| `attribution.source` | string | yes | Name of the open data set the fact comes from. |
| `attribution.url` | string | yes |  |
| `attribution.license` | string | yes | For example CC BY-SA 4.0 or CC0 1.0. |

### 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. |
| `429` | `rate_limited`, `concurrency_limited`, `api_key_temporarily_frozen`, or the failure guard. Not charged. |
| `500` | `internal_error`. 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. |
