# Geocode

An address or place name to coordinates.

Turns a free-form address or place name (`q`), or a structured `address`, into coordinates with the parts of the address and its kind. `limit` is 5 by default and at most 20. Every response carries `attribution`, the licence note of the open data the places come from; show it with the results.

### What is charged

Charged when at least one result comes back. An empty list carries the warning `no_results` and is free.

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

## Request

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

### Body

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `q` | string |  | 2 to 512 characters. |
| `address` | object |  |  |
| `address.street` | string |  | At most 128 characters. |
| `address.house_number` | string |  | At most 128 characters. |
| `address.city` | string |  | At most 128 characters. |
| `address.district` | string |  | At most 128 characters. |
| `address.region` | string |  | At most 128 characters. |
| `address.postal_code` | string |  | At most 128 characters. |
| `address.country` | string |  | At most 128 characters. |
| `country` | string or array of string |  |  |
| `near` | object |  |  |
| `near.lat` | number | yes | -90 to 90. |
| `near.lon` | number | yes | -180 to 180. |
| `language` | string |  | BCP 47 language tag. At most 12 characters. |
| `limit` | integer |  | 1 to 20. Default `5`. |

### Example request

curl:

```bash
curl https://api.websearch.fast/v1/web/geocode \
  -H "Authorization: Bearer $WEBSEARCH_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"q": "Alexanderplatz 1, Berlin", "limit": 1}'
```

Python:

```python
import os
import httpx

response = httpx.post(
    "https://api.websearch.fast/v1/web/geocode",
    headers={"Authorization": f"Bearer {os.environ['WEBSEARCH_API_KEY']}"},
    json={
        "q": "Alexanderplatz 1, Berlin",
        "limit": 1,
    },
    timeout=60,
)
response.raise_for_status()
print(response.json())
```

TypeScript:

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

### Example response

```json
{
  "object": "web.geocode",
  "request_id": "20261128120004123456789a1b2c3d4Kx7Qm2Pe",
  "created": 1795867204,
  "input": {"q": "Alexanderplatz 1, Berlin", "language": "en", "limit": 1},
  "results": [
    {
      "formatted_address": "Alexanderplatz 1, 10178 Berlin, Germany",
      "location": {"lat": 52.5219, "lon": 13.4132},
      "components": {
        "house_number": "1",
        "street": "Alexanderplatz",
        "district": "Mitte",
        "city": "Berlin",
        "postal_code": "10178",
        "country": "Germany",
        "country_code": "de"
      },
      "kind": "address",
      "bbox": [13.4127, 52.5215, 13.4137, 52.5223]
    }
  ],
  "attribution": "© OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright",
  "usage": {"charged": true, "items": [{"unit": "geocode", "count": 1}]}
}
```

### Response fields

Matches.

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `object` | string | yes | Always `web.geocode`. |
| `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 |  |  |
| `results` | array of object | yes |  |
| `results.formatted_address` | string | yes |  |
| `results.name` | string |  | Name of the place when it has one. |
| `results.location` | object | yes |  |
| `results.location.lat` | number | yes | -90 to 90. |
| `results.location.lon` | number | yes | -180 to 180. |
| `results.components` | object |  |  |
| `results.components.house_number` | string |  |  |
| `results.components.street` | string |  |  |
| `results.components.district` | string |  |  |
| `results.components.city` | string |  |  |
| `results.components.region` | string |  |  |
| `results.components.postal_code` | string |  |  |
| `results.components.country` | string |  |  |
| `results.components.country_code` | string |  |  |
| `results.kind` | string | yes | A place of any other kind is not returned. One of `address`, `street`, `poi`, `postal_code`, `district`, `city`, `region`, `country`. |
| `results.bbox` | array of number |  | [min_lon, min_lat, max_lon, max_lat]. 4 to 4 items. |
| `results.place_id` | string |  | Stable id of the map object, when known. |
| `results.distance_m` | number |  | reverse-geocode only: metres from the point, rounded to 0.1. |
| `attribution` | string | yes | Licence notice of the map data. Always present; show it where the results are shown. |

### 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. |
| `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. |
