Ruby

Take screenshots using Ruby with our official SDK or plain HTTP requests.

Installation

gem install lambdashot

Or add to your Gemfile:

gem 'lambdashot'

Quick Start

require 'lambdashot'

client = LambdaShot::Client.new('YOUR_ACCESS_KEY', 'YOUR_SECRET_KEY')

# Take a screenshot
screenshot = client.take(
  url: 'https://example.com',
  format: 'png',
  viewport_width: 1920,
  viewport_height: 1080
)

# Save to file
File.binwrite('screenshot.png', screenshot)

Generate Signed URL

url = client.generate_signed_url(
  url: 'https://example.com',
  format: 'png',
  block_ads: true
)

puts url
# https://api.lambdashot.com/take?url=...&signature=...

Options

screenshot = client.take(
  url: 'https://example.com',

  # Output
  format: 'png',
  image_quality: 90,

  # Viewport
  viewport_width: 1920,
  viewport_height: 1080,
  viewport_device: 'iphone_15_pro',
  device_scale_factor: 2,

  # Full page
  full_page: true,
  full_page_scroll: true,

  # Blocking
  block_ads: true,
  block_cookie_banners: true,
  block_trackers: true,

  # Customization
  dark_mode: true,
  delay: 2,
  wait_for_selector: '.content-loaded'
)

Without SDK (Net::HTTP)

require 'net/http'
require 'uri'

params = {
  access_key: 'YOUR_ACCESS_KEY',
  url: 'https://example.com',
  format: 'png',
  viewport_width: 1920
}

uri = URI("https://api.lambdashot.com/take")
uri.query = URI.encode_www_form(params)

response = Net::HTTP.get_response(uri)
File.binwrite('screenshot.png', response.body)

Video Recording

video = client.animate(
  url: 'https://example.com',
  scenario: 'scroll',
  duration: 10,
  format: 'mp4'
)

File.binwrite('video.mp4', video)

Error Handling

begin
  screenshot = client.take(url: 'https://example.com')
rescue LambdaShot::Error => e
  case e.code
  when 'timeout_error'
    puts 'Screenshot timed out, retrying...'
  when 'access_key_invalid'
    puts 'Check your API key'
  else
    puts "Error: #{e.message}"
  end
end

Rails Integration

# config/initializers/lambdashot.rb
LambdaShot.configure do |config|
  config.access_key = ENV['LAMBDASHOT_ACCESS_KEY']
  config.secret_key = ENV['LAMBDASHOT_SECRET_KEY']
end

# app/controllers/screenshots_controller.rb
class ScreenshotsController < ApplicationController
  def create
    screenshot = LambdaShot.take(
      url: params[:url],
      format: 'png'
    )

    send_data screenshot,
      type: 'image/png',
      filename: 'screenshot.png'
  end
end