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

# Python

> Call LangSearch with the Python standard library.

No LangSearch-specific SDK is needed for a direct HTTP request. Select Python below, save the example as `search.py`, replace the key, and run `python3 search.py`.

<CodeGroup>
  ```bash cURL theme={null}
  curl https://api.langsearch.com/v1/web-search \
    -H "Authorization: Bearer YOUR_LANGSEARCH_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "query": "How does a search API help an AI agent?",
      "count": 5,
      "contents": {"text": true},
      "freshness": "noLimit"
    }'
  ```

  ```python Python theme={null}
  import json
  from urllib.request import Request, urlopen

  request = Request(
      "https://api.langsearch.com/v1/web-search",
      data=json.dumps({
          "query": "How does a search API help an AI agent?",
          "count": 5, "contents": {"text": True}, "freshness": "noLimit"
      }).encode(),
      headers={
          "Authorization": "Bearer YOUR_LANGSEARCH_API_KEY",
          "Content-Type": "application/json"
      }
  )
  with urlopen(request, timeout=30) as response:
      result = json.load(response)
  for source in result["data"]["webPages"]["value"]:
      print(source["name"], source["url"])
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch("https://api.langsearch.com/v1/web-search", {
    method: "POST",
    headers: {
      Authorization: "Bearer YOUR_LANGSEARCH_API_KEY",
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      query: "How does a search API help an AI agent?",
      count: 5, contents: { text: true }, freshness: "noLimit",
    }),
    signal: AbortSignal.timeout(30000),
  });
  if (!response.ok) throw new Error(`HTTP ${response.status}`);
  const result = await response.json();
  for (const source of result.data.webPages.value) {
    console.log(source.name, source.url);
  }
  ```
</CodeGroup>

## Handle request failures

`urlopen` raises `HTTPError` for non-success HTTP responses. Read the response body for the error message and `log_id`; do not log request headers.

```python theme={null}
from urllib.error import HTTPError, URLError

try:
    with urlopen(request, timeout=30) as response:
        result = json.load(response)
except HTTPError as error:
    print("HTTP status:", error.code)
    print(error.read().decode())
except URLError:
    print("Network request failed")
```

Use [bounded retries](/api/errors) for transient failures.
