Skip to content

Latest commit

 

History

History
269 lines (213 loc) · 5.72 KB

File metadata and controls

269 lines (213 loc) · 5.72 KB

Examples

This directory contains example usage of the Open Browser Rendering API.

JavaScript/Node.js Examples

Basic Screenshot

const fetch = require('node-fetch');
const fs = require('fs');

async function takeScreenshot() {
  const response = await fetch('http://localhost:3000/screenshot', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer supersecret123',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      url: 'https://example.com',
      viewport: { width: 1920, height: 1080 },
      screenshotOptions: { fullPage: true, type: 'png' }
    })
  });

  const buffer = await response.buffer();
  fs.writeFileSync('screenshot.png', buffer);
  console.log('Screenshot saved!');
}

takeScreenshot();

Generate PDF

const fetch = require('node-fetch');
const fs = require('fs');

async function generatePDF() {
  const response = await fetch('http://localhost:3000/pdf', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer supersecret123',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      url: 'https://example.com',
      format: 'A4',
      printBackground: true
    })
  });

  const buffer = await response.buffer();
  fs.writeFileSync('page.pdf', buffer);
  console.log('PDF saved!');
}

generatePDF();

Evaluate JavaScript

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

async function evaluateScript() {
  const response = await fetch('http://localhost:3000/evaluate', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer supersecret123',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      url: 'https://example.com',
      script: 'document.title'
    })
  });

  const result = await response.json();
  console.log('Page title:', result.result);
}

evaluateScript();

Python Examples

Basic Screenshot

import requests

def take_screenshot():
    response = requests.post(
        'http://localhost:3000/screenshot',
        headers={
            'Authorization': 'Bearer supersecret123',
            'Content-Type': 'application/json'
        },
        json={
            'url': 'https://example.com',
            'viewport': {'width': 1920, 'height': 1080},
            'screenshotOptions': {'fullPage': True, 'type': 'png'}
        }
    )
    
    with open('screenshot.png', 'wb') as f:
        f.write(response.content)
    
    print('Screenshot saved!')

take_screenshot()

Generate PDF

import requests

def generate_pdf():
    response = requests.post(
        'http://localhost:3000/pdf',
        headers={
            'Authorization': 'Bearer supersecret123',
            'Content-Type': 'application/json'
        },
        json={
            'url': 'https://example.com',
            'format': 'A4',
            'printBackground': True
        }
    )
    
    with open('page.pdf', 'wb') as f:
        f.write(response.content)
    
    print('PDF saved!')

generate_pdf()

PHP Examples

Basic Screenshot

<?php

function takeScreenshot() {
    $ch = curl_init('http://localhost:3000/screenshot');
    
    $data = json_encode([
        'url' => 'https://example.com',
        'viewport' => ['width' => 1920, 'height' => 1080],
        'screenshotOptions' => ['fullPage' => true, 'type' => 'png']
    ]);
    
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Authorization: Bearer supersecret123',
        'Content-Type: application/json'
    ]);
    
    $result = curl_exec($ch);
    curl_close($ch);
    
    file_put_contents('screenshot.png', $result);
    echo 'Screenshot saved!';
}

takeScreenshot();
?>

cURL Examples

Screenshot

curl -X POST http://localhost:3000/screenshot \
  -H "Authorization: Bearer supersecret123" \
  -H "Content-Type: application/json" \
  -o screenshot.png \
  -d '{
    "url": "https://example.com",
    "viewport": {"width": 1920, "height": 1080},
    "screenshotOptions": {"fullPage": true, "type": "png"}
  }'

PDF

curl -X POST http://localhost:3000/pdf \
  -H "Authorization: Bearer supersecret123" \
  -H "Content-Type: application/json" \
  -o page.pdf \
  -d '{
    "url": "https://example.com",
    "format": "A4",
    "printBackground": true
  }'

Evaluate

curl -X POST http://localhost:3000/evaluate \
  -H "Authorization: Bearer supersecret123" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://example.com",
    "script": "document.title"
  }'

Health Check

curl http://localhost:3000/health

Advanced Examples

Screenshot with Custom Options

curl -X POST http://localhost:3000/screenshot \
  -H "Authorization: Bearer supersecret123" \
  -H "Content-Type: application/json" \
  -o screenshot.jpg \
  -d '{
    "url": "https://example.com",
    "viewport": {"width": 1920, "height": 1080},
    "screenshotOptions": {
      "fullPage": false,
      "type": "jpeg",
      "quality": 90
    },
    "gotoOptions": {
      "waitUntil": "networkidle",
      "timeout": 45000
    }
  }'

Extract Page Data

curl -X POST http://localhost:3000/evaluate \
  -H "Authorization: Bearer supersecret123" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://example.com",
    "script": "({ title: document.title, url: window.location.href, links: Array.from(document.querySelectorAll(\"a\")).length })"
  }'

Integration Examples

See the examples/ directory for complete integration examples:

  • examples/node-express/ - Express.js integration
  • examples/python-flask/ - Flask integration
  • examples/php-laravel/ - Laravel integration