Example

Call the Places API from Python.

This example uses the Python standard library, keeps the API key server-side, and parses either a result body or a structured error code.

Python example.

import json
import os
import urllib.parse
import urllib.request

api_key = os.environ["OPEN_PLACES_API_KEY"]

params = urllib.parse.urlencode({
    "q": "coffee",
    "lat": "40.7128",
    "lon": "-74.0060",
    "radius_mi": "25",
    "limit": "10",
})

request = urllib.request.Request(
    f"https://api.openplacesapi.com/v1/places?{params}",
    headers={"Authorization": f"Bearer {api_key}"},
)

try:
    with urllib.request.urlopen(request, timeout=10) as response:
        body = json.loads(response.read().decode("utf-8"))
except urllib.error.HTTPError as error:
    body = json.loads(error.read().decode("utf-8"))
    raise RuntimeError(body.get("error", {}).get("code", "places_api_error"))

print(body["results"])