Puppeteer API

Puppeteer is a Node.js library for controlling headless Chrome or Chromium browsers. Integrating Puppeteer with our API enables you to automate browser tasks, capture screenshots, generate PDFs, or extract data from complex web pages. This guide covers setup and basic usage.

Prerequisites

  • Node.js: Version 14.x or higher recommended.
  • Puppeteer: Install via npm or yarn.
  • API Key: Retrieve your API key from your account dashboard.

Setup

  1. Install Puppeteer. Run the following command in your Node.js project. This also downloads a compatible Chromium binary.

    npm install puppeteer
    
  2. API Configuration. Store your API key securely in your script or environment variables.

Basic Example (JavaScript)

Here's an example of using Puppeteer with our API to scrape a page and send data:

const puppeteer = require('puppeteer')
const fetch = require('node-fetch')

const API_ENDPOINT = 'https://api.example.com/data'

async function main() {
  // Launch Puppeteer browser
  const browser = await puppeteer.launch({ headless: true })
  const page = await browser.newPage()

  // Navigate to a webpage
  await page.goto('https://example.com', { waitUntil: 'networkidle2' })

  // Extract data
  const text = await page.$eval('#sample-id', (el) => el.textContent)
  console.log(`Extracted text: ${text}`)

  // Send data to API (without any API key)
  const response = await fetch(API_ENDPOINT, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({ data: text }),
  })

  // Handle API response
  const result = await response.json()
  if (response.ok) {
    console.log('API Response:', result)
  } else {
    console.log('API Error:', response.status)
  }

  // Clean up
  await browser.close()
}

main().catch((err) => {
  console.error('Error occurred:', err)
})

Key Features

  • Headless Mode: Run Chromium in the background for efficient automation.
  • Rich APIs: Take screenshots, emulate devices, or intercept network requests.
  • Fast Integration: Combine Puppeteer's capabilities with our API endpoints.

Troubleshooting

  • Ensure sufficient memory for Chromium (increase if running in constrained environments).
  • Verify API key permissions for your endpoint.
  • Use try/catch for robust error handling in async code.

Was this page helpful?