> For the complete documentation index, see [llms.txt](https://developers.oxylabs.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers.oxylabs.io/integrations/cn/ai-studio-integrations/javascript-sdk.md).

# JavaScript SDK

我们提供一个 JavaScript SDK，用于无缝交互 [Oxylabs AI Studio API](https://aistudio.oxylabs.io/) 服务交互，包括 AI-Scraper、AI-Crawler、AI-Browser-Agent 以及其他数据提取工具。

## 安装

安装 SDK：

```sh
npm install oxylabs-ai-studio
```

请添加 `OXYLABS_AI_STUDIO_API_URL` 并 `OXYLABS_AI_STUDIO_API_KEY` 的值到 `.env` 文件中，或者作为环境变量：

```sh
export OXYLABS_AI_STUDIO_API_KEY=your_api_key_here
```

## <sup>用法</sup>

### AI-Scraper

```javascript
import { 
  OxylabsAIStudioSDK
} from 'oxylabs-ai-studio';

const sdk = new OxylabsAIStudioSDK({
  apiKey: 'your_api_key_here',
  timeout: 120000,
  retryAttempts: 3,
});

async function testGenerateSchema() {
  try {
    console.log('Testing schema generation...');
    const schema = await sdk.aiScraper.generateSchema({
      user_prompt: 'Extract the title of the page'
    });
    console.log('Schema:', schema);
  } catch (error) {
    console.error('Schema generation error:', error.message);
  }
}

testGenerateSchema();
```

#### 基本用法

```javascript
import { 
  OxylabsAIStudioSDK, 
  OutputFormat
} from 'oxylabs-ai-studio';

const sdk = new OxylabsAIStudioSDK({
  apiKey: 'your_api_key_here',
  timeout: 120000,
  retryAttempts: 3,
});

async function testScrapeOutputJson() {
  try {
    console.log('Testing synchronous scraping with JSON output...');
    
    const options = {
      url: 'https://www.freelancer.com',
      user_prompt: 'Extract all links',
      output_format: OutputFormat.JSON,
      geo_location: "US",
      schema: {
        type: 'object',
        properties: {
          links: { type: 'array', items: { type: 'string' } }
        }
      }
    };
    
    const results = await sdk.aiScraper.scrape(options);
    console.log('Sync scraping results:', results);
  } catch (error) {
    console.error('Sync scraping error:', error.message);
  }
}

testScrapeOutputJson();
```

#### 输入参数

* `url` (*字符串*）：要处理的目标 URL。
* `user_prompt` (*字符串*）：要提取的数据说明。这用于自动生成 `openapi_schema` ，当使用 `scrapeWithAutoSchema` 方法时。
* `output_format` (*字符串*）：所需的输出格式。可以是 `markdown` 或 `json`。默认为 `markdown`.
* `render_html` (*布尔值*）：指定在提取前是否在页面上渲染 JavaScript。默认为 `false`.
* `openapi_schema` (*Record\<string, any>*）：定义输出数据结构的 JSON Schema 对象。当 `output_format` 设置为 `json`.
* `geo_location` (*字符串*）：指定请求应模拟的地理位置（ISO2 格式）。

### AI-Crawler

#### 基本用法

```javascript
import { 
  OxylabsAIStudioSDK, 
  OutputFormat
} from 'oxylabs-ai-studio';

const sdk = new OxylabsAIStudioSDK({
  apiKey: 'your_api_key_here',
  timeout: 120000,
  retryAttempts: 3,
});

async function testCrawlOutputJson() {
  try {
    console.log('Testing crawling with JSON output...');
    
    const options = {
      url: 'https://www.freelancer.com',
      output_format: OutputFormat.JSON,
      user_prompt: 'Get job ad pages',
      return_sources_limit: 3,
      geo_location: "DE",
      schema: {
        type: "object",
        properties: {
          jobAd: {
            type: "object",
            properties: {
              position_title: {
                type: "string"
              },
              salary: {
                type: "string"
              }
            }
          }
        }
      }
    };
    
    const results = await sdk.aiCrawler.crawl(options);
    console.log('Crawling results:', JSON.stringify(results, null, 2));      
  } catch (error) {
    console.error('Crawling error:', error.message);
  }
}

testCrawlOutputJson();
```

#### 输入参数

* `url` (*字符串*）：抓取的起始 URL。
* `crawl_prompt` (*字符串*）：定义要查找和抓取的页面类型的说明。
* `parse_prompt` (*字符串*）：定义从抓取页面中提取哪些数据的说明。这用于自动生成 `openapi_schema` ，当使用 `crawlWithAutoSchema` 方法时。
* `output_format` (*字符串*）：所需的输出格式。可以是 `markdown` 或 `json`。默认为 `markdown`.
* `max_pages` (*整数*）：要返回的最大页面或来源数量。默认为 `25`.
* `render_html` (*布尔值*）：指定在提取前是否在页面上渲染 JavaScript。默认为 `false`.
* `openapi_schema` (*Record\<string, any>*）：定义输出数据结构的 JSON Schema 对象。当 `output_format` 设置为 `json`.
* `geo_location` (*字符串*）：指定请求应模拟的地理位置（ISO2 格式）。

### Browser-Agent

#### 基本用法

```javascript
import { 
  OxylabsAIStudioSDK, 
  OutputFormat
} from 'oxylabs-ai-studio';

const sdk = new OxylabsAIStudioSDK({
  apiKey: 'your_api_key_here',
  timeout: 120000,
  retryAttempts: 3,
});

async function testBrowseOutputJson() {
  try {
    console.log('Testing synchronous browsing with JSON output...');
    
    const options = {
      url: 'https://www.freelancer.com',
      output_format: OutputFormat.JSON,
      user_prompt: 'Navigate to the first job ad you can find.',
      geo_location: "US",
      schema: {
        type: 'object',
        properties: {
          job_title: { type: 'string' }
        }
      }
    };
    
    const results = await sdk.browserAgent.browse(options);
    console.log('Sync browsing results:', JSON.stringify(results, null, 2));
  } catch (error) {
    console.error('Sync browsing error:', error.message);
  }
}

testBrowseOutputJson();
```

#### 输入参数

* `url` (*字符串*）：Browser agent 的起始目标 URL。
* `browse_prompt` (*字符串*）：定义 Browser agent 应执行的操作的说明。
* `parse_prompt` (*字符串*）：在执行浏览器操作后定义要提取哪些数据的说明。这用于自动生成 `openapi_schema` ，当使用 `browseWithAutoSchema` 方法时。
* `output_format` (*字符串*）：所需的输出格式。可以是 `markdown`, `html`, `json`，或 `screenshot`。默认为 `markdown`.
* `render_html` (*布尔值*）：指定是否在页面上渲染 JavaScript。虽然这是一个 browser agent，但此标志可能会影响某些行为。默认为 `false`.
* `openapi_schema` (*Record\<string, any>*）：定义输出数据结构的 JSON Schema 对象。当 `output_format` 设置为 `json`.
* `geo_location` (*字符串*）：指定请求应模拟的地理位置（ISO2 格式）。

### AI-Search

#### 基本用法

```javascript
import {
  OxylabsAIStudioSDK,
} from 'oxylabs-ai-studio';

const sdk = new OxylabsAIStudioSDK({
  apiKey: 'your_api_key_here',
  timeout: 120000,
  retryAttempts: 3,
});

async function testSearch() {
  try {
    console.log('Testing search...');

    const options = {
      query: 'weather in London',
      limit: 3,
      return_content: true,
      render_javascript: false,
      geo_location: "IT",
    };

    const results = await sdk.aiSearch.search(options);
    console.log('Search results:', JSON.stringify(results, null, 2));
  } catch (error) {
    console.error('Search error:', error.message);
  }
}

testSearch();
```

#### 输入参数

* `query` (*字符串*）：搜索查询。
* `limit` (*整数*）：要返回的最大搜索结果数。最大值：50。
* `render_javascript` (*布尔值*）：是否渲染页面上的 JavaScript。默认为 `false`.
* `return_content` (*布尔值*）：是否返回每个搜索结果的 Markdown 内容。默认为 `true`.
* `geo_location` (*字符串*）：指定请求应模拟的地理位置（ISO2 格式）。

### AI-Map

#### 基本用法

```javascript
import { 
  OxylabsAIStudioSDK
} from 'oxylabs-ai-studio';

const sdk = new OxylabsAIStudioSDK({
  apiKey: 'your_api_key_here',
  timeout: 120000,
  retryAttempts: 3,
});

async function testMap() {
  try {
    console.log('Testing map...');
    
    const options = {
      url: 'https://www.freelancer.com/jobs',
      user_prompt: 'Extract tech job ads',
      return_sources_limit: 10,
      geo_location: 'US',
      render_javascript: false
    };
    
    const results = await sdk.aiMap.map(options);
    console.log('Map results:', JSON.stringify(results, null, 2));
  } catch (error) {
    console.error('Map error:', error.message);
  }
}

testMap();
```

#### 输入参数

* `url` (*字符串*）：要映射并从中提取数据的目标 URL。
* `user_prompt` (*字符串*）：要从映射页面中提取哪些数据的说明。
* `return_sources_limit` (*整数*）：映射过程中要返回的最大来源/页面数量。
* `geo_location` (*字符串*）：映射请求使用的地理位置（例如 'US'、'UK'）。
* `render_javascript` (*布尔值*）：指定在映射前是否在页面上渲染 JavaScript。默认为 `false`.

### 用法示例

你可以在这里找到每个应用的更多示例：

* [Browser-agent 示例](https://github.com/oxylabs/oxylabs-ai-studio-js/blob/main/examples/browser-agent.js)
* [AI-Crawler 示例](https://github.com/oxylabs/oxylabs-ai-studio-js/blob/main/examples/ai-crawler.js)
* [AI-Scraper 示例](https://github.com/oxylabs/oxylabs-ai-studio-js/blob/main/examples/ai-scraper.js)
* [AI-Search 示例](https://github.com/oxylabs/oxylabs-ai-studio-js/blob/main/examples/ai-search.js)
* [AI-Map 示例](https://github.com/oxylabs/oxylabs-ai-studio-js/blob/main/examples/ai-map.js)


---

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

```
GET https://developers.oxylabs.io/integrations/cn/ai-studio-integrations/javascript-sdk.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
