search.py, replace the key, and run python3 search.py.
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.
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Call LangSearch with the Python standard library.
search.py, replace the key, and run python3 search.py.
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"
}'
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"])
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);
}
urlopen raises HTTPError for non-success HTTP responses. Read the response body for the error message and log_id; do not log request headers.
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")
