Language

Ruby Screenshot API

Screenshot API integration for Ruby and Rails applications using Net::HTTP or the Faraday gem. Streamline visual testing, content generation, and background job processing with Sidekiq. Includes production patterns for retry logic, file uploads to Active Storage, and integration with Rails controller actions.

Quick Start

1

Install dependencies

No extra gems needed — Net::HTTP is part of Ruby stdlib. Optionally run gem install faraday for a friendlier HTTP client.

2

Get your API key

Sign up for Screenshotly and get your API key from the dashboard.

3

Copy the code example

Use our Ruby code example as a starting point.

4

Customize and integrate

Modify the code to fit your specific use case and requirements.

Code Example

# Ruby with Net::HTTP
require 'net/http'
require 'json'

def capture_screenshot(url, output_path)
  uri = URI('https://api.screenshotly.app/screenshot')
  
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  
  request = Net::HTTP::Post.new(uri)
  request['Authorization'] = "Bearer #{ENV['SCREENSHOTLY_API_KEY']}"
  request['Content-Type'] = 'application/json'
  request.body = {
    url: url,
    device: 'desktop',
    format: 'png'
  }.to_json
  
  response = http.request(request)
  File.write(output_path, response.body)
end

When to Use Ruby with Screenshotly

Use the Ruby integration for Rails applications, Sidekiq background jobs, and Rake automation tasks. Ruby is the natural fit when you need to generate screenshots from a Rails admin panel, process captures in Sidekiq workers, or store results in Active Storage.

Ruby Best Practices

Use the Faraday gem with faraday-retry middleware for automatic retry with exponential backoff on transient failures.

In Rails, create a ScreenshotService PORO (Plain Old Ruby Object) and register it in an initializer for clean dependency injection.

Attach screenshots directly to Active Storage: record.screenshot.attach(io: StringIO.new(response.body), filename: 'capture.png').

Process large batches with Sidekiq workers — set concurrency limits to match your API plan's rate limit.

Want a step-by-step walkthrough?

Read the full Ruby tutorial →

API Reference

EndpointPOST /api/screenshot
AuthenticationBearer token
Content-Typeapplication/json
View full API docs

Frequently Asked Questions

How do I use Screenshotly with Ruby on Rails?

Create a service object that wraps the API call. Use Rails credentials (Rails.application.credentials.screenshotly_api_key) to store your key securely. Save screenshots to Active Storage or upload directly to S3.

What's the best HTTP client for Ruby integrations?

The Faraday gem is recommended for production use — it supports retries, timeouts, and middleware out of the box. For simpler scripts, Net::HTTP (shown above) works fine without additional dependencies.

How do I handle rate limiting in Ruby?

Check the response status code for 429 (Too Many Requests). Implement exponential backoff with the retryable gem or a custom retry loop. The API returns Retry-After headers to indicate when you can make the next request.

Can I capture screenshots asynchronously in Rails?

Yes. Use Active Job with Sidekiq or Delayed Job to process screenshot captures in the background. This prevents HTTP timeouts and allows you to queue large batches of captures.

Start building with Ruby

Get your API key and start capturing screenshots in minutes.

Other Languages