ScreenshotAPI

Ruby

Official screenshotapi_to gem for Ruby — capture screenshots, PDFs, and rendered HTML from scripts and Rails with a zero-dependency client.

The official screenshotapi_to gem is a small net/http client with no runtime dependencies. It captures screenshots, PDFs, and rendered HTML and raises typed errors for API failures.

Gem: screenshotapi_to on RubyGems · Require: screenshotapi · Source & examples: github.com/miketromba/screenshotapi-ruby · Ruby 3.0+.

Installation

Add the gem to your Gemfile (note the require: so bundle loads the right file):

gem "screenshotapi_to", require: "screenshotapi"
bundle install

Or install it directly:

gem install screenshotapi_to

Authentication

Create an API key in the dashboard and keep it in an environment variable. The SDK sends it in the x-api-key header.

export SCREENSHOTAPI_KEY="sk_live_your_key_here"
require "screenshotapi"

client = ScreenshotAPI::Client.new(ENV.fetch("SCREENSHOTAPI_KEY"))

Keep API keys on the server. Never expose them in client-side code or commit them to source control.

Quick Start

Capture a URL and save it to disk

save captures the screenshot, writes the file, and returns the response metadata.

require "screenshotapi"

client = ScreenshotAPI::Client.new(ENV.fetch("SCREENSHOTAPI_KEY"))

metadata = client.save(url: "https://example.com", path: "screenshot.png")

puts "Screenshot ID: #{metadata.screenshot_id}"
puts "Credits remaining: #{metadata.credits_remaining}"

Or work with the raw image bytes

screenshot returns a Result with the image bytes plus metadata.

result = client.screenshot(url: "https://example.com", type: "webp")

File.binwrite("screenshot.webp", result.image)
puts result.content_type            # "image/webp"
puts result.metadata.duration_ms

Methods

ScreenshotAPI::Client.new(api_key, base_url:, timeout:)

ParameterTypeDefaultDescription
api_keyString— (required)Your API key
base_urlStringhttps://screenshotapi.toAPI base URL (proxies, tests)
timeoutInteger60Open and read timeout in seconds

client.screenshot(**options)

Returns a ScreenshotAPI::Result:

result.image                       # String (binary) — raw image or PDF bytes
result.content_type                # "image/png", "image/webp", "application/pdf", …
result.metadata.credits_remaining  # Integer
result.metadata.screenshot_id      # String — include this when contacting support
result.metadata.duration_ms        # Integer

client.save(path:, **options)

Captures, writes the bytes to path:, and returns a ScreenshotAPI::Metadata (the metadata shape above).

Options

Pass any screenshot parameter as a snake_case keyword argument; the client converts them to API parameters for you.

OptionTypeDefaultDescription
urlStringRequired unless html is setURL to capture
htmlStringRaw HTML to render (switches to POST)
widthInteger1440Viewport width in pixels (max 1920)
heightInteger900Viewport height in pixels (max 10000)
full_pageBooleanfalseCapture the full scrollable page
typeString"png""png", "jpeg", "webp", or "pdf"
qualityInteger100JPEG/WebP quality, 1–100
color_schemeStringPage default"light" or "dark"
wait_untilString"networkidle2""load", "domcontentloaded", "networkidle0", "networkidle2"
wait_for_selectorStringCSS selector to wait for
delayInteger0Extra wait after load (ms, max 30000)
block_adsBooleanfalseBlock common ad networks
remove_cookie_bannersBooleanfalseAuto-remove cookie consent dialogs
css_injectStringCSS injected before capture
js_injectStringJavaScript evaluated before capture
stealth_modeBooleanfalseAnti-bot-detection browser fingerprint
device_pixel_ratioInteger1Retina/HiDPI scale (1, 2, or 3)
timezoneStringServer defaultIANA timezone, e.g. "America/New_York"
localeStringServer defaultBCP 47 locale, e.g. "en-US"
cache_ttlInteger0Cache identical captures for N seconds
preload_fontsBooleanfalsePreload Google Fonts before capture
remove_elementsArray<String>CSS selectors to remove
remove_popupsBooleanfalseRemove common modals/overlays
mockup_deviceString"browser", "iphone", or "macbook" (PNG output)
geo_latitude, geo_longitude, geo_accuracyNumericGeolocation override (GET requests)
geo_locationHashGeolocation as { latitude:, longitude:, accuracy: } (HTML/POST requests)
result = client.screenshot(
  url: "https://example.com/pricing",
  width: 1440,
  height: 1200,
  full_page: true,
  type: "webp",
  quality: 85,
  color_scheme: "dark",
  wait_until: "networkidle2",
  wait_for_selector: "main",
  delay: 500,
  block_ads: true,
  remove_cookie_banners: true,
  device_pixel_ratio: 2,
  cache_ttl: 300,
  remove_elements: [".newsletter", "#cookie-banner"]
)

File.binwrite("pricing.webp", result.image)

Render HTML & generate PDFs

Pass html: to render a raw string — the client uses POST /api/v1/screenshot with a JSON body. Combine with type: "pdf" for documents.

# Render HTML to PNG
result = client.screenshot(
  html: "<main><h1>Hello from Ruby</h1></main>",
  width: 800,
  height: 600,
  type: "png"
)

# Save a URL as a PDF
client.save(url: "https://example.com/report", type: "pdf", path: "report.pdf")

Error Handling

The SDK raises typed errors. All inherit from ScreenshotAPI::APIError.

require "screenshotapi"

client = ScreenshotAPI::Client.new(ENV.fetch("SCREENSHOTAPI_KEY"))

begin
  result = client.screenshot(url: "https://example.com")
  File.binwrite("screenshot.png", result.image)
rescue ScreenshotAPI::AuthenticationError
  warn "API key missing or malformed (401)"
rescue ScreenshotAPI::InvalidAPIKeyError
  warn "API key revoked or invalid (403)"
rescue ScreenshotAPI::InsufficientCreditsError => e
  warn "Out of credits (402). Balance: #{e.balance}"
rescue ScreenshotAPI::ScreenshotFailedError => e
  warn "Capture failed server-side (500): #{e.message}"
rescue ScreenshotAPI::NetworkError => e
  warn "Network error: #{e.message}"
rescue ScreenshotAPI::APIError => e
  warn "ScreenshotAPI error #{e.status}: #{e.message}"
end
ErrorWhen
AuthenticationError401 — API key missing or malformed
InsufficientCreditsError402 — no credits remaining (exposes #balance)
InvalidAPIKeyError403 — API key revoked or invalid
ScreenshotFailedError500 — capture failed server-side
NetworkErrorConnection failure or timeout
APIErrorBase class (exposes #status)

Framework Recipes

Rails Controller

Stream the screenshot back to the browser and map SDK errors to HTTP responses.

class ScreenshotsController < ApplicationController
  def show
    url = params.require(:url)
    result = client.screenshot(url: url, type: "webp", quality: 80)

    response.headers["Cache-Control"] = "public, max-age=3600"
    send_data result.image, type: result.content_type, disposition: "inline"
  rescue ScreenshotAPI::InsufficientCreditsError
    render json: { error: "Out of screenshot credits" }, status: :payment_required
  rescue ScreenshotAPI::APIError => e
    render json: { error: e.message }, status: :bad_gateway
  end

  private

  def client
    @client ||= ScreenshotAPI::Client.new(ENV.fetch("SCREENSHOTAPI_KEY"))
  end
end

Runnable examples ship with the gem: plain_ruby.rb and rails_controller.rb.

Next steps

On this page