Guides · Ruby · Net::HTTP
Take a screenshot of a URL in Ruby with Net::HTTP
The simplest request the API takes: a URL in, PNG bytes out. No full_page, no extra options — you get exactly what a visitor with a 1280×800 browser window would see above the fold. That is the right default more often than people expect: link previews, monitoring thumbnails and visual smoke tests all want the fold, not a 40,000-pixel scroll of the footer.
Below is a complete, runnable Ruby program using Net::HTTP, no third-party dependency needed. It reads your API key from the SNAPDOK_KEY environment variable — free keys take about thirty seconds and need no card.
require "json"
require "net/http"
require "uri"
uri = URI("https://snapdok.io/v1/render")
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true, read_timeout: 90) do |http|
req = Net::HTTP::Post.new(uri)
req["Authorization"] = "Bearer #{ENV.fetch("SNAPDOK_KEY")}"
req["Content-Type"] = "application/json"
req.body = JSON.generate({
url: "https://your-app.com/report/42",
format: "png",
width: 1280,
height: 800,
})
http.request(req)
end
raise "snapdok error #{res.code}: #{res.body}" unless res.is_a?(Net::HTTPSuccess)
File.binwrite("screenshot.png", res.body)
The two dimensions are the viewport, not the output size: the page lays itself out as if the browser window were 1280×800, and the image comes back at exactly those pixels (multiply by device_scale if you set it). Responsive sites react to the width — ask for 390 and you get the mobile layout, no user-agent spoofing involved.
Two habits worth starting on day one. Check the HTTP status before writing bytes to disk — errors arrive as JSON, and saving {"error":"UNAUTHORIZED"} as screenshot.png is the classic first-week bug. And log the X-Cache header: identical requests within 24 hours return HIT, cost nothing, and come back in milliseconds — failed renders are never metered either way.
Same endpoint, one more trick: if the page you are rendering has a form on it, adding "pdf_forms": true to a PDF render brings it back with real, fillable AcroForm fields — a PDF people can type into, not a picture of one. How that works.
Notes for Net::HTTP
Stdlib only — no gems. The traps Net::HTTP is famous for, all handled in the sample: TLS is not automatic (set use_ssl: true or you will POST in the clear to port 80 and get a redirect); the default read_timeout of 60 s is close enough to real render times that raising it is wise; and the response body is a binary-ready String, but write it with File.binwrite — plain File.write on Windows would mangle line-ending bytes in the PDF. Check is_a?(Net::HTTPSuccess): Net::HTTP never raises on HTTP error statuses.
Response headers worth reading
| Header | Meaning |
|---|---|
X-Cache | HIT = served from the 24h cache, free. |
X-Quota-Remaining | Metered renders left this month. |
X-RateLimit-Remaining | Requests left in the current second. |
Full parameter reference: the docs. Hard numbers on caps and timeouts: limits.
Related guides
Same task, other stacks
- Take a screenshot of a URL in Python with requests
- Take a screenshot of a URL in Python with httpx
- Take a screenshot of a URL in Node.js with built-in fetch
- Take a screenshot of a URL in Node.js with axios
- Take a screenshot of a URL in PHP with cURL extension
More with Ruby + Net::HTTP
- Convert an HTML form to a fillable PDF
- Get a fillable PDF of just the form — no nav, no footer
- Generate a fillable invoice PDF from a web page
- Turn an online registration form into a fillable PDF
- Wait for a JavaScript-rendered form, then make it fillable
- Verify a fillable PDF render without opening the file
- Convert a URL to PDF
- Take a full-page screenshot
- Capture very tall pages as numbered tiles
- Take a retina-quality (2x) screenshot
- Take a JPEG screenshot at a custom viewport size
- Use the 24-hour render cache to cut costs
- Capture a JavaScript-heavy page after it settles
- Wait for a specific element before capturing
- Add page numbers and headers to a PDF