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

# JavaScript

> Use native fetch from a server-side JavaScript application.

Use a current Node.js runtime with native `fetch` and `AbortSignal.timeout`. Select JavaScript below, save as `search.mjs`, replace the key, and run `node search.mjs`.

<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>

## Keep requests on the server

Call LangSearch from a server route or backend service. Do not embed your real key in a public browser bundle.

## Select source context

```javascript theme={null}
const sources = result.data.webPages.value.map((item) => ({
  title: item.name,
  url: item.url,
  context: item.text || item.snippet || "",
}));
```

Keep the URLs with the context so your model can cite its sources. For failures, inspect HTTP status and the error body before deciding whether to retry.
