A JavaScript SDK for seamlessly interacting with Oxylabs AI Studio API services, including AI-Scraper, AI-Crawler, AI-Browser-Agent and other data extraction tools.
npm install oxylabs-ai-studioEither add OXYLABS_AI_STUDIO_API_URL and OXYLABS_AI_STUDIO_API_KEY values to the .env file, or as your environment variables:
export OXYLABS_AI_STUDIO_API_KEY=your_api_key_hereimport { 
  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();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(string): The target URL to process.user_prompt(string): Instructions for what data to extract. This is used to automatically generate theopenapi_schemawhen using thescrapeWithAutoSchemamethod.output_format(string): The desired format for the output. Can be eithermarkdownorjson. Defaults tomarkdown.render_html(boolean): Specifies whether to render JavaScript on the page before extraction. Defaults tofalse.openapi_schema(Record<string, any>): A JSON Schema object that defines the structure of the output data. This is required whenoutput_formatis set tojson.geo_location(string): Specifies the geographic location (ISO2 format) from which the request should be simulated.
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(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 theopenapi_schemawhen using thecrawlWithAutoSchemamethod.output_format(string): The desired format for the output. Can be eithermarkdownorjson. Defaults tomarkdown.max_pages(integer): The maximum number of pages or sources to return. Defaults to25.render_html(boolean): Specifies whether to render JavaScript on the pages before extraction. Defaults tofalse.openapi_schema(Record<string, any>): A JSON Schema object that defines the structure of the output data. This is required whenoutput_formatis set tojson.geo_location(string): Specifies the geographic location (ISO2 format) from which the request should be simulated.
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(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 theopenapi_schemawhen using thebrowseWithAutoSchemamethod.output_format(string): The desired format for the output. Can bemarkdown,html,json, orscreenshot. Defaults tomarkdown.render_html(boolean): Specifies whether to render JavaScript on the page. Although this is a browser agent, this flag might influence certain behaviors. Defaults tofalse.openapi_schema(Record<string, any>): A JSON Schema object that defines the structure of the output data. This is required whenoutput_formatis set tojson.geo_location(string): Specifies the geographic location (ISO2 format) from which the request should be simulated.
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(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 tofalse.return_content(boolean): Whether to return the markdown content of each of the search result. Defaults totrue.geo_location(string): Specifies the geographic location (ISO2 format) from which the request should be simulated.
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(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 tofalse.
You can find more examples of each application here:
