# JavaScript SDK

<figure><img src="https://raw.githubusercontent.com/oxylabs/oxylabs-ai-studio-js/refs/heads/main/images/Github-AI-Studio-1262x525px%20new.png" alt=""><figcaption></figcaption></figure>

[![](https://camo.githubusercontent.com/ec456302fbdba5490012a0bc8d2739619d0e57dd8840197d06b33b9ea3b35bda/68747470733a2f2f646362616467652e6c696d65732e70696e6b2f6170692f7365727665722f5064733367426d4b4d483f7374796c653d666f722d7468652d6261646765267468656d653d646973636f7264)](https://discord.gg/Pds3gBmKMH) [![YouTube](https://camo.githubusercontent.com/1b748ffea39adfb777030373ce78d8bc07d3625a9039b324bcbc708a91f376c1/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f596f75547562652d4f78796c6162732d7265643f7374796c653d666f722d7468652d6261646765266c6f676f3d796f7574756265266c6f676f436f6c6f723d7768697465)](https://www.youtube.com/@oxylabs)

We offer a JavaScript SDK for seamlessly interacting with [Oxylabs AI Studio API](https://aistudio.oxylabs.io/) services, including AI-Scraper, AI-Crawler, AI-Browser-Agent, and other data extraction tools.

## Installation

Install the SDK:

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

Either add `OXYLABS_AI_STUDIO_API_URL` and `OXYLABS_AI_STUDIO_API_KEY` values to the `.env` file, or as your environment variables:

```sh
export OXYLABS_AI_STUDIO_API_KEY=your_api_key_here
```

## <sup>Usage</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();
```

#### Basic usage

```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();
```

#### Input parameters

* `url` (*string*): The target URL to process.
* `user_prompt` (*string*): Instructions for what data to extract. This is used to automatically generate the `openapi_schema` when using the `scrapeWithAutoSchema` method.
* `output_format` (*string*): The desired format for the output. Can be either `markdown` or `json`. Defaults to `markdown`.
* `render_html` (*boolean*): Specifies whether to render JavaScript on the page before extraction. Defaults to `false`.
* `openapi_schema` (*Record\<string, any>*): A JSON Schema object that defines the structure of the output data. This is required when `output_format` is set to `json`.
* `geo_location` (*string*): Specifies the geographic location (ISO2 format) from which the request should be simulated.

### AI-Crawler

#### Basic usage

```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();
```

#### Input parameters

* `url` (*string*): The starting URL for the crawl.
* `crawl_prompt` (*string*): Instructions defining the types of pages to find and crawl.
* `parse_prompt` (*string*): Instructions for what data to extract from the crawled pages. This is used to automatically generate the `openapi_schema` when using the `crawlWithAutoSchema` method.
* `output_format` (*string*): The desired format for the output. Can be either `markdown` or `json`. Defaults to `markdown`.
* `max_pages` (*integer*): The maximum number of pages or sources to return. Defaults to `25`.
* `render_html` (*boolean*): Specifies whether to render JavaScript on the pages before extraction. Defaults to `false`.
* `openapi_schema` (*Record\<string, any>*): A JSON Schema object that defines the structure of the output data. This is required when `output_format` is set to `json`.
* `geo_location` (*string*): Specifies the geographic location (ISO2 format) from which the request should be simulated.

### Browser-Agent

#### Basic usage

```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();
```

#### Input parameters

* `url` (*string*): The target URL for the browser agent to start at.
* `browse_prompt` (*string*): Instructions defining the actions the browser agent should perform.
* `parse_prompt` (*string*): Instructions for what data to extract after performing the browser actions. This is used to automatically generate the `openapi_schema` when using the `browseWithAutoSchema` method.
* `output_format` (*string*): The desired format for the output. Can be `markdown`, `html`, `json`, or `screenshot`. Defaults to `markdown`.
* `render_html` (*boolean*): Specifies whether to render JavaScript on the page. Although this is a browser agent, this flag might influence certain behaviors. Defaults to `false`.
* `openapi_schema` (*Record\<string, any>*): A JSON Schema object that defines the structure of the output data. This is required when `output_format` is set to `json`.
* `geo_location` (*string*): Specifies the geographic location (ISO2 format) from which the request should be simulated.

### AI-Search

#### Basic usage

```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();
```

#### Input parameters

* `query` (*string*): The search query.
* `limit` (*integer*): The maximum number of search results to return. Maximum: 50.
* `render_javascript` (*boolean*): Whether to render JavaScript on the page. Defaults to `false`.
* `return_content` (*boolean*): Whether to return the markdown content of each of the search result. Defaults to `true`.
* `geo_location` (*string*): Specifies the geographic location (ISO2 format) from which the request should be simulated.

### AI-Map

#### Basic usage

```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();
```

#### Input parameters

* `url` (*string*): The target URL to map and extract data from.
* `user_prompt` (*string*): Instructions for what data to extract from the mapped pages.
* `return_sources_limit` (*integer*): The maximum number of sources/pages to return from the mapping process.
* `geo_location` (*string*): The geographical location to use for the mapping request (e.g., 'US', 'UK').
* `render_javascript` (*boolean*): Specifies whether to render JavaScript on the pages before mapping. Defaults to `false`.

### Usage examples

You can find more examples of each application here:

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