> For the complete documentation index, see [llms.txt](https://ai.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ai.ortusbooks.com/advanced/reference/built-in-functions/aiwebsearch.md).

# aiWebSearch

Search the web using the configured or selected provider and return normalized results.

## Syntax

```javascript
aiWebSearch( query, options = {} )
```

## Parameters

| Parameter | Type   | Required | Description                                                     |
| --------- | ------ | -------- | --------------------------------------------------------------- |
| `query`   | string | Yes      | Search query text                                               |
| `options` | struct | No       | Search options including provider, limits, filters, and logging |

## Common Options

| Option        | Type    | Default                  | Description                                          |
| ------------- | ------- | ------------------------ | ---------------------------------------------------- |
| `provider`    | string  | module default or `http` | Provider: `http`, `brave`, `google`, `tavily`, `exa` |
| `maxResults`  | numeric | `5`                      | Maximum number of results                            |
| `timeout`     | numeric | `30`                     | HTTP timeout in seconds                              |
| `logRequest`  | boolean | `false`                  | Log provider request details                         |
| `logResponse` | boolean | `false`                  | Log provider response details                        |

## Provider-Specific Options

### Brave

| Option       | Description      |
| ------------ | ---------------- |
| `country`    | Country filter   |
| `language`   | Language filter  |
| `safeSearch` | Safe search mode |

### Google

| Option | Description        |
| ------ | ------------------ |
| `gl`   | Country target     |
| `hl`   | Interface language |
| `safe` | Safe search mode   |

### Tavily

| Option              | Description              |
| ------------------- | ------------------------ |
| `topic`             | Topic mode               |
| `includeAnswer`     | Include summary answer   |
| `includeRawContent` | Include raw page content |
| `days`              | Recency in days          |

### Exa

| Option     | Description                     |
| ---------- | ------------------------------- |
| `type`     | `keyword`, `neural`, or `magic` |
| `country`  | Country filter                  |
| `language` | Language filter                 |

## Returns

Array of structs with normalized fields:

```javascript
[
    {
        title: "...",
        url: "...",
        snippet: "...",
        publishedDate: "...",
        domain: "...",
        score: 0.95,
        thumbnail: "...",
        language: "en"
    }
]
```

API keys follow the standard resolution order used in BoxLang AI integrations: constructor/config options, then module settings, then environment variables.

## Events Fired

| Event                   | When                               |
| ----------------------- | ---------------------------------- |
| `beforeAIWebSearch`     | Before provider search execution   |
| `onAIWebSearchRequest`  | Right before HTTP request dispatch |
| `onAIWebSearchResponse` | After provider HTTP response       |
| `afterAIWebSearch`      | After successful search completion |
| `onAIWebSearchError`    | On provider/search error           |

## Examples

### Default provider

```javascript
results = aiWebSearch( "BoxLang AI" )
```

### Brave search with filters

```javascript
results = aiWebSearch(
    "latest BoxLang news",
    {
        provider: "brave",
        maxResults: 10,
        country: "us",
        language: "en",
        safeSearch: "moderate"
    }
)
```

### Exa neural search

```javascript
results = aiWebSearch(
    "agent memory retrieval patterns",
    {
        provider: "exa",
        type: "neural",
        maxResults: 8
    }
)
```

### Agent usage

```javascript
agent = aiAgent(
    name: "ResearchAgent",
    tools: [ "webSearch@bxai" ]
)

response = agent.run( "Find the latest BoxLang MCP updates" )
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ai.ortusbooks.com/advanced/reference/built-in-functions/aiwebsearch.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
