======================== CODE SNIPPETS ======================== TITLE: Crawlbase Crawling API GET Request Examples DESCRIPTION: Examples demonstrating how to make GET requests using the Crawling API, including basic usage, custom options, fetching Javascript-rendered pages, and accessing response status codes (original and Crawlbase specific). SOURCE: https://github.com/crawlbase/crawlbase-node/blob/main/README.md#_snippet_4 LANGUAGE: javascript CODE: ``` api.get('https://www.facebook.com/britneyspears').then(response => { if (response.statusCode === 200) { console.log(response.body); } }).catch(error => console.error); ``` LANGUAGE: javascript CODE: ``` api.get('https://www.reddit.com/r/pics/comments/5bx4bx/thanks_obama/', { userAgent: 'Mozilla/5.0 (Windows NT 6.2; rv:20.0) Gecko/20121202 Firefox/30.0', format: 'json' }).then(response => { if (response.statusCode === 200) { console.log(response.body); } }).catch(error => console.error); ``` LANGUAGE: javascript CODE: ``` api.get('https://www.nfl.com').then(response => { if (response.statusCode === 200) { console.log(response.body); } }).catch(error => console.error); ``` LANGUAGE: javascript CODE: ``` api.get('https://www.freelancer.com', { page_wait: 5000 }).then(response => { if (response.statusCode === 200) { console.log(response.body); } }).catch(error => console.error); ``` LANGUAGE: javascript CODE: ``` api.get('https://craiglist.com').then(response => { console.log(response.originalStatus, response.cbStatus); }).catch(error => console.error); ``` ---------------------------------------- TITLE: Install Crawlbase Node.js Module DESCRIPTION: Instructions to install the Crawlbase Node.js module using npm, making it available for use in your project. SOURCE: https://github.com/crawlbase/crawlbase-node/blob/main/README.md#_snippet_0 LANGUAGE: javascript CODE: ``` npm i crawlbase ``` ---------------------------------------- TITLE: Initialize and Use Crawlbase Screenshots API in Node.js DESCRIPTION: This snippet demonstrates how to initialize the Crawlbase Screenshots API client in Node.js using your API token. It shows two examples of using the `get` method: one for a basic screenshot capture, and another for capturing a screenshot with additional parameters, such as specifying a 'mobile' device. The responses are saved as binary files. SOURCE: https://github.com/crawlbase/crawlbase-node/blob/main/README.md#_snippet_9 LANGUAGE: javascript CODE: ``` const api = new ScreenshotsAPI({ token: 'YOUR_TOKEN' }); api.get('https://www.amazon.com').then(response => { fs.writeFileSync('amazon.jpg', response.body, { encoding: 'binary' }); }); // Example with parameters api.get('https://www.amazon.com', { device: 'mobile' }).then(response => { fs.writeFileSync('amazon-mobile.jpg', response.body, { encoding: 'binary' }); }); ``` ---------------------------------------- TITLE: Crawlbase Leads API Usage Example DESCRIPTION: Demonstrates how to use the Leads API to retrieve leads associated with a given domain by calling the `getFromDomain` method. SOURCE: https://github.com/crawlbase/crawlbase-node/blob/main/README.md#_snippet_8 LANGUAGE: javascript CODE: ``` const api = new LeadsAPI({ token: 'YOUR_TOKEN' }); api.getFromDomain('somesite.com').then(response => { console.log(response.leads); }); ``` ---------------------------------------- TITLE: Crawlbase Crawling API POST Request Examples DESCRIPTION: Examples demonstrating how to make POST requests using the Crawling API, including sending form data and specifying the postType option to send JSON payloads. SOURCE: https://github.com/crawlbase/crawlbase-node/blob/main/README.md#_snippet_5 LANGUAGE: javascript CODE: ``` api.post('https://producthunt.com/search', { text: 'example search' }).then(response => { if (response.statusCode === 200) { console.log(response.body); } }).catch(error => console.error); ``` LANGUAGE: javascript CODE: ``` api.post('https://httpbin.org/post', { some_json: 'with some value' }, { postType: 'json' }).then(response => { if (response.statusCode === 200) { console.log(response.body); } }).catch(error => console.error); ``` ---------------------------------------- TITLE: Crawlbase Crawling API PUT Request Example DESCRIPTION: Example demonstrating how to make PUT requests using the Crawling API, similar to POST requests for sending data. SOURCE: https://github.com/crawlbase/crawlbase-node/blob/main/README.md#_snippet_6 LANGUAGE: javascript CODE: ``` api.put('https://producthunt.com/search', { text: 'example search' }).then(response => { if (response.statusCode === 200) { console.log(response.body); } }).catch(error => console.error); ``` ---------------------------------------- TITLE: Initialize Crawlbase API Clients DESCRIPTION: Demonstrates how to initialize instances of CrawlingAPI, ScraperAPI, and LeadsAPI using your respective Crawlbase tokens. This setup is crucial for making requests to the Crawlbase services, including specific initialization for Javascript-enabled crawling. SOURCE: https://github.com/crawlbase/crawlbase-node/blob/main/README.md#_snippet_2 LANGUAGE: javascript CODE: ``` const api = new CrawlingAPI({ token: 'YOUR_TOKEN' }); ``` LANGUAGE: javascript CODE: ``` const api = new ScraperAPI({ token: 'YOUR_TOKEN' }); ``` LANGUAGE: javascript CODE: ``` const api = new LeadsAPI({ token: 'YOUR_TOKEN' }); ``` LANGUAGE: javascript CODE: ``` const api = new CrawlingAPI({ token: 'YOUR_JAVASCRIPT_TOKEN' }); ``` ---------------------------------------- TITLE: Crawlbase Crawling API Methods Reference DESCRIPTION: Reference for the Crawling API methods, including their signatures and parameters for making GET, POST, and PUT requests to scrape web content. Note that only GET is available when using a Javascript token. SOURCE: https://github.com/crawlbase/crawlbase-node/blob/main/README.md#_snippet_3 LANGUAGE: APIDOC CODE: ``` CrawlingAPI.get(url: string, options?: object): Promise - Makes a GET request to scrape a URL. - Parameters: - url: The URL to scrape. - options: Optional parameters for the Crawlbase API (e.g., userAgent, format, page_wait). - Returns: A Promise that resolves to a response object containing statusCode, body, originalStatus, cbStatus, etc. CrawlingAPI.post(url: string, data: string | object, options?: object): Promise - Makes a POST request to scrape a URL, sending data. - Parameters: - url: The URL to scrape. - data: The data to send, can be a JSON object or a string. - options: Optional parameters for the Crawlbase API (e.g., postType). - Returns: A Promise that resolves to a response object. CrawlingAPI.put(url: string, data: string | object, options?: object): Promise - Makes a PUT request to scrape a URL, sending data. - Parameters: - url: The URL to scrape. - data: The data to send, can be a JSON object or a string. - options: Optional parameters for the Crawlbase API. - Returns: A Promise that resolves to a response object. ``` ---------------------------------------- TITLE: Crawlbase Scraper API Usage Example DESCRIPTION: Demonstrates how to use the Scraper API to extract structured data from a product page, accessing the parsed JSON response directly. SOURCE: https://github.com/crawlbase/crawlbase-node/blob/main/README.md#_snippet_7 LANGUAGE: javascript CODE: ``` api.get('https://www.amazon.com/Halo-SleepSack-Swaddle-Triangle-Neutral/dp/B01LAG1TOS').then(response => { if (response.statusCode === 200) { console.log(response.json); } }).catch(error => console.error); ``` ---------------------------------------- TITLE: Import Crawlbase API Classes in Node.js DESCRIPTION: Imports the necessary Crawlbase API classes (CrawlingAPI, ScraperAPI, LeadsAPI, ScreenshotsAPI) into your Node.js project, allowing you to instantiate and use them. SOURCE: https://github.com/crawlbase/crawlbase-node/blob/main/README.md#_snippet_1 LANGUAGE: javascript CODE: ``` const { CrawlingAPI, ScraperAPI, LeadsAPI, ScreenshotsAPI } = require('crawlbase'); ```