Browserless API, How to use it

Dec 14, 2024

Browserless offers a suite of production-ready APIs accessible to both shared and enterprise users. Here are five key Browser APIs

Browserless API, How to use it

Top 5 Browserless APIs and How to Use Them

Browserless offers a suite of production-ready APIs accessible to both shared and enterprise users. Here are five key Browser APIs, along with basic usage examples:

1. /browserql API

This API allows you to run custom queries against a live browser using BrowserQL, a GraphQL-based API. It simplifies browser interaction without requiring Puppeteer or Playwright code.

Example (GraphQL Query):

mutation NewTab {
  goto(
    url: "https://browserless.io", 
    waitUntil: networkIdle, 
    timeout: 5000
  ) {
    status
    text
    url
    time
  }
}

This example uses the goto mutation to navigate to https://browserless.io, waiting for network idle and setting a 5-second timeout. The response includes the status code, status text, final URL, and load time. Other BrowserQL actions like setCheckbox are also available.

2. /content API

This API returns the HTML of dynamic content. It's useful for fetching content from websites that rely on JavaScript for rendering.

Example (Conceptual):

The exact usage depends on your chosen programming language (e.g., cURL, JavaScript, Python). You would send a POST request to /content with a JSON payload containing the target URL and any other relevant options (cookies, headers, etc.). The response will contain the HTML content of the page.

3. /unblock API

This API helps retrieve HTML, screenshots, or cookies from websites that employ bot detection mechanisms.

Example (Conceptual):

Similar to /content, you'd send a POST request to /unblock with a JSON payload including the URL and options like browserWSEndpoint (to keep the browser alive for reconnection), cookies, content, and screenshot. The response will include the requested data.

4. /download API

This API returns files downloaded by Chrome during the execution of your code. It's designed for scenarios where you need to programmatically download files from a website.

Example (JavaScript):

// ... (fetch request setup) ...
const data = `
export default async function () {
  await page.goto("https://example.com/download.pdf");
  await page.pdf({ path: 'download.pdf' });
}
`;
// ... (send POST request with 'data' to /download) ...

This example uses a JavaScript function to navigate to a PDF download link and save the file. The response will contain the downloaded PDF.

5. /function API

This API provides a quick way to run custom Puppeteer code without installing additional libraries. It supports various programming languages via HTTP requests.

Example (JavaScript):

// ... (fetch request setup) ...
const data = `
export default async function ({ page }) {
  await page.goto("https://example.com/");
  const title = await page.title();
  return {  { title }, type: "application/json" };
}
`;
// ... (send POST request with 'data' to /function) ...

This example uses a JavaScript function to navigate to a URL and return the page title. The response will be a JSON object containing the title. The API also supports importing external libraries using ESM modules (e.g., import { faker } from "https://esm.sh/@faker-js/faker";).

Note: All these APIs require an API token for authentication. Refer to the Browserless documentation for detailed parameter schemas and usage instructions for each API in your preferred programming language. Remember to replace "YOUR_API_TOKEN_HERE" with your actual API token. Some APIs have specific requirements regarding pricing plans and features. Check the documentation for details.

browserless logo browserless io websocket secure
Recent Posts