Browserless API, How to use it
Dec 14, 2024Browserless offers a suite of production-ready APIs accessible to both shared and enterprise users. Here are five key Browser APIs
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.
React OpenGraph Image Generation: Techniques and Best Practices
Published Jan 15, 2025
Learn how to generate dynamic Open Graph (OG) images using React for improved social media engagement. Explore techniques like browser automation, server-side rendering, and serverless functions....
Setting Up a Robust Supabase Local Development Environment
Published Jan 13, 2025
Learn how to set up a robust Supabase local development environment for efficient software development. This guide covers Docker, CLI, email templates, database migrations, and testing....
Understanding and Implementing Javascript Heap Memory Allocation in Next.js
Published Jan 12, 2025
Learn how to increase Javascript heap memory in Next.js applications to avoid out-of-memory errors. Explore methods, best practices, and configurations for optimal performance....