Code Examples

Take Screenshots with Elixir

Ready-to-copy Elixir code for capturing website screenshots, generating PDFs, and processing pages at scale. Uses Req.

Quick Start

Screenshot in Elixir — minimal example

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

Elixirquickstart
{:ok, resp} = Req.get("https://api.screenshotdev.com/v1/screenshot",
  params: [url: "https://example.com", access_key: "YOUR_ACCESS_KEY"]
)
File.write!("screenshot.png", resp.body)
Full Example

Production-ready Elixir script

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

Elixirfull example
defmodule Screenshot do
  @api_key "YOUR_ACCESS_KEY"
  @base_url "https://api.screenshotdev.com/v1/screenshot"

  def take(url, opts \\ []) do
    params = [
      url: url,
      access_key: @api_key,
      format: Keyword.get(opts, :format, "webp"),
      width: Keyword.get(opts, :width, 1280),
      full_page: Keyword.get(opts, :full_page, false),
      dark_mode: Keyword.get(opts, :dark_mode, false)
    ]

    case Req.get(@base_url, params: params) do
      {:ok, %{status: 200, body: body}} ->
        {:ok, body}

      {:ok, %{status: status, body: body}} ->
        {:error, "Screenshot failed (#{status}): #{body}"}

      {:error, reason} ->
        {:error, "Request failed: #{inspect(reason)}"}
    end
  end
end

{:ok, img} = Screenshot.take("https://example.com", format: "webp", width: 1440, full_page: true)
File.write!("screenshot.webp", img)
IO.puts("Saved screenshot.webp (#{byte_size(img)} bytes)")
Advanced

More Elixir examples

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

PDF Generation

Elixirpdf
{:ok, resp} = Req.get("https://api.screenshotdev.com/v1/pdf",
  params: [
    url: "https://example.com",
    access_key: "YOUR_ACCESS_KEY",
    format: "a4",
    landscape: false,
    margin: "20px"
  ]
)
File.write!("page.pdf", resp.body)

Batch Screenshots

Elixirbatch
urls = [
  "https://example.com",
  "https://github.com",
  "https://news.ycombinator.com"
]

urls
|> Task.async_stream_nolink(fn url ->
  {:ok, img} = Screenshot.take(url, format: "webp")
  host = URI.parse(url).host
  filename = "#{host}.webp"
  File.write!(filename, img)
  {filename, byte_size(img)}
end, max_concurrency: 5)
|> Enum.each(fn {:ok, {name, size}} ->
  IO.puts("  #{name} (#{size} bytes)")
end)

Async with Webhooks

Elixirwebhooks
{:ok, resp} = Req.post("https://api.screenshotdev.com/v1/screenshot",
  json: %{
    url: "https://example.com",
    access_key: "YOUR_ACCESS_KEY",
    webhook_url: "https://your-app.com/api/webhooks/screenshot",
    format: "webp",
    full_page: true
  }
)
IO.puts("Job queued: #{resp.body["id"]}")

Custom CSS/JS Injection

Elixirinject
{:ok, resp} = Req.get(@base_url,
  params: [
    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
  ]
)
File.write!("clean.webp", resp.body)

S3 Upload

Elixirs3
# 1. Capture
{:ok, img} = Screenshot.take("https://example.com", format: "webp")

# 2. Upload with ExAws
ExAws.S3.put_object(
  "my-screenshots",
  "captures/example-com.webp",
  img,
  content_type: "image/webp"
)
|> ExAws.request!()

Try it live

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

Setup

Installation

Install the HTTP library used in the examples above.

Terminalinstall
{:req, "~> 0.5"}

Uses Req. Any HTTP client that supports GET requests will work.

More Examples

Code examples in other languages

Start capturing screenshots
with Elixir today.

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

Take Screenshots with Elixir — ScreenshotDEV