Guides / Java

Take website screenshots in Java

Maintaining your own headless-browser fleet for screenshots means babysitting Chromium versions, memory leaks, fonts, and proxy rules. RasterKit turns all of that into a single HTTPS call: send a URL from your Java code, get back finished image bytes.

The engine runs real Chromium, so modern CSS, web fonts and JavaScript-rendered pages come out exactly as users see them. Flags like full_page, device=mobile, dark_mode and block_cookie_banners handle the cases that make DIY screenshotting painful.

1. Get an API key

Sign up free (magic link, no card) — your key is shown right after sign-in. You get 100 renders/month free across screenshots, PDFs, and images.

2. Make the request

import java.net.URI;
import java.net.http.*;
import java.nio.file.*;
import java.time.Duration;

public class Render {
    public static void main(String[] args) throws Exception {
        String body = """
            {"url": "https://example.com", "full_page": true, "format": "png"}""";
        HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create("https://rasterkit.com/v1/screenshot"))
            .timeout(Duration.ofSeconds(60))
            .header("x-api-key", System.getenv("RASTERKIT_API_KEY"))
            .header("content-type", "application/json")
            .POST(HttpRequest.BodyPublishers.ofString(body))
            .build();

        HttpResponse<byte[]> res = HttpClient.newHttpClient()
            .send(request, HttpResponse.BodyHandlers.ofByteArray());
        if (res.statusCode() != 200) {
            throw new RuntimeException("Render failed: " + res.statusCode() + " " + new String(res.body()));
        }
        Files.write(Path.of("screenshot.png"), res.body());
        System.out.println("Saved screenshot.png");
    }
}

The response body is the file itself — no JSON envelope to unwrap, no second download request. Errors come back as JSON with a stable error code.

3. Tune the output

The Screenshot API reference documents every parameter. The ones people reach for first:

Use cases

FAQ

How do I capture the full page, not just the viewport?

Pass "full_page": true. RasterKit auto-scrolls the page first so lazy-loaded images and infinite-scroll content are included.

Can I screenshot pages that need JavaScript?

Yes — every render runs in real Chromium with JS enabled. Use wait_until: "networkidle", wait_for_selector, or delay_ms for late-rendering apps.

How fast is it?

Typical p50 is 1.5–3 s for a normal page (sync request). Add cache_ttl and repeated captures of the same URL return instantly and free.

Related guides

Get your free API key →