Code Examples

Take Screenshots with R

Ready-to-copy R code for capturing website screenshots, generating PDFs, and processing pages at scale. Uses httr2.

Quick Start

Screenshot in R — minimal example

The simplest way to capture a screenshot. Just a URL and your API key.

Rquickstart
library(httr2)

resp <- request("https://api.screenshotdev.com/v1/screenshot") |>
  req_url_query(url = "https://example.com", access_key = "YOUR_ACCESS_KEY") |>
  req_perform()
writeBin(resp_body_raw(resp), "screenshot.png")
Full Example

Production-ready R script

Complete working script with options for viewport, format, full page capture, and error handling.

Rfull example
library(httr2)

api_key <- "YOUR_ACCESS_KEY"
base_url <- "https://api.screenshotdev.com/v1/screenshot"

take_screenshot <- function(
  url,
  format = "webp",
  width = 1280,
  full_page = FALSE,
  dark_mode = FALSE
) {
  resp <- request(base_url) |>
    req_url_query(
      url = url,
      access_key = api_key,
      format = format,
      width = width,
      full_page = tolower(as.character(full_page)),
      dark_mode = tolower(as.character(dark_mode))
    ) |>
    req_error(is_error = function(resp) FALSE) |>
    req_perform()

  if (resp_status(resp) != 200) {
    stop(paste0("Screenshot failed (", resp_status(resp), "): ", resp_body_string(resp)))
  }
  resp_body_raw(resp)
}

img <- take_screenshot("https://example.com", format = "webp", width = 1440, full_page = TRUE)
writeBin(img, "screenshot.webp")
message(sprintf("Saved screenshot.webp (%d bytes)", length(img)))
Advanced

More R examples

PDF generation, batch processing, webhooks, CSS/JS injection, and S3 upload.

PDF Generation

Rpdf
resp <- request("https://api.screenshotdev.com/v1/pdf") |>
  req_url_query(
    url = "https://example.com",
    access_key = "YOUR_ACCESS_KEY",
    format = "a4",
    landscape = "false",
    margin = "20px"
  ) |>
  req_perform()
writeBin(resp_body_raw(resp), "page.pdf")

Batch Screenshots

Rbatch
library(httr2)
library(purrr)

urls <- c(
  "https://example.com",
  "https://github.com",
  "https://news.ycombinator.com"
)

walk(urls, function(url) {
  img <- take_screenshot(url, format = "webp")
  host <- sub("https?://", "", url) |> sub("/.*", "", x = _)
  filename <- paste0(host, ".webp")
  writeBin(img, filename)
  message(sprintf("  %s (%d bytes)", filename, length(img)))
})

Async with Webhooks

Rwebhooks
resp <- request("https://api.screenshotdev.com/v1/screenshot") |>
  req_method("POST") |>
  req_headers("Content-Type" = "application/json") |>
  req_body_json(list(
    url = "https://example.com",
    access_key = "YOUR_ACCESS_KEY",
    webhook_url = "https://your-app.com/api/webhooks/screenshot",
    format = "webp",
    full_page = TRUE
  )) |>
  req_perform()
job <- resp_body_json(resp)
message(sprintf("Job queued: %s", job$id))

Custom CSS/JS Injection

Rinject
resp <- request(base_url) |>
  req_url_query(
    url = "https://example.com",
    access_key = api_key,
    inject_css = "body { font-family: 'Inter', sans-serif; } .ad { display: none; }",
    inject_js = "document.querySelectorAll('[data-cookie-banner]').forEach(el => el.remove())",
    block_ads = "true"
  ) |>
  req_perform()
writeBin(resp_body_raw(resp), "clean.webp")

S3 Upload

Rs3
library(aws.s3)

# 1. Capture
img <- take_screenshot("https://example.com", format = "webp")

# 2. Upload
put_object(
  file = rawConnection(img),
  object = "captures/example-com.webp",
  bucket = "my-screenshots",
  headers = list("Content-Type" = "image/webp")
)

Try it live

Test the API in the interactive playground — pre-filled with R snippets.

Setup

Installation

Install the HTTP library used in the examples above.

Terminalinstall
install.packages("httr2")

Uses httr2. Any HTTP client that supports GET requests will work.

More Examples

Code examples in other languages

Start capturing screenshots
with R today.

100 free API calls per month. No credit card. First screenshot in under 30 seconds.

Take Screenshots with R — ScreenshotDEV