Code Examples

Take Screenshots with Ruby

Ready-to-copy Ruby code for capturing website screenshots, generating PDFs, and processing pages at scale. Uses Net::HTTP (stdlib) or HTTParty.

Quick Start

Screenshot in Ruby — minimal example

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

Rubyquickstart
require "net/http"
require "uri"

uri = URI("https://api.screenshotdev.com/v1/screenshot")
uri.query = URI.encode_www_form(url: "https://example.com", access_key: "YOUR_ACCESS_KEY")
response = Net::HTTP.get_response(uri)
File.binwrite("screenshot.png", response.body)
Full Example

Production-ready Ruby script

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

Rubyfull example
require "net/http"
require "uri"
require "json"

API_KEY = "YOUR_ACCESS_KEY"
BASE_URL = "https://api.screenshotdev.com/v1/screenshot"

def take_screenshot(url, format: "webp", width: 1280, full_page: false, dark_mode: false)
  uri = URI(BASE_URL)
  uri.query = URI.encode_www_form(
    url: url,
    access_key: API_KEY,
    format: format,
    width: width,
    full_page: full_page,
    dark_mode: dark_mode
  )
  response = Net::HTTP.get_response(uri)

  unless response.is_a?(Net::HTTPSuccess)
    raise "Screenshot failed (#{response.code}): #{response.body}"
  end

  response.body
end

img = take_screenshot("https://example.com", format: "webp", width: 1440, full_page: true)
File.binwrite("screenshot.webp", img)
puts "Saved screenshot.webp (#{img.bytesize} bytes)"
Advanced

More Ruby examples

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

PDF Generation

Rubypdf
uri = URI("https://api.screenshotdev.com/v1/pdf")
uri.query = URI.encode_www_form(
  url: "https://example.com",
  access_key: "YOUR_ACCESS_KEY",
  format: "a4",
  landscape: false,
  margin: "20px"
)
response = Net::HTTP.get_response(uri)
File.binwrite("page.pdf", response.body)

Batch Screenshots

Rubybatch
require "net/http"

urls = %w[
  https://example.com
  https://github.com
  https://news.ycombinator.com
]

threads = urls.map do |target|
  Thread.new(target) do |url|
    uri = URI(BASE_URL)
    uri.query = URI.encode_www_form(url: url, access_key: API_KEY, format: "webp")
    response = Net::HTTP.get_response(uri)
    filename = URI(url).host + ".webp"
    File.binwrite(filename, response.body)
    puts "  #{filename} (#{response.body.bytesize} bytes)"
  end
end
threads.each(&:join)

Async with Webhooks

Rubywebhooks
uri = URI("https://api.screenshotdev.com/v1/screenshot")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true

request = Net::HTTP::Post.new(uri, "Content-Type" => "application/json")
request.body = {
  url: "https://example.com",
  access_key: "YOUR_ACCESS_KEY",
  webhook_url: "https://your-app.com/api/webhooks/screenshot",
  format: "webp",
  full_page: true
}.to_json

response = http.request(request)
job = JSON.parse(response.body)
puts "Job queued: #{job['id']}"

Custom CSS/JS Injection

Rubyinject
uri = URI(BASE_URL)
uri.query = URI.encode_www_form(
  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
)
response = Net::HTTP.get_response(uri)
File.binwrite("clean.webp", response.body)

S3 Upload

Rubys3
require "aws-sdk-s3"

# 1. Capture
uri = URI(BASE_URL)
uri.query = URI.encode_www_form(url: "https://example.com", access_key: API_KEY, format: "webp")
img = Net::HTTP.get_response(uri).body

# 2. Upload
s3 = Aws::S3::Client.new
s3.put_object(
  bucket: "my-screenshots",
  key: "captures/example-com.webp",
  body: img,
  content_type: "image/webp"
)

Try it live

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

Setup

Installation

Install the HTTP library used in the examples above.

Terminalinstall
gem install httparty

Uses Net::HTTP (stdlib) or HTTParty. Any HTTP client that supports GET requests will work.

More Examples

Code examples in other languages

Start capturing screenshots
with Ruby today.

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

Take Screenshots with Ruby — ScreenshotDEV