Skip to content
shotsmith
Features Docs Guides Pricing Changelog Sign inStart free

How to take website screenshots in Go

Capture website screenshots and PDFs from Go with net/http: save files, set options, and serve screenshots from an HTTP handler.

3 min read

In Go, the usual way to screenshot a page is chromedp, which means running and babysitting a Chrome process next to your service. With shotsmith, Chrome runs on our side and a screenshot is one net/http request. Nothing to install beyond the standard library.

1. Get an API key

Create a free account (100 renders a month, no card) and export your access key as SHOTSMITH_ACCESS_KEY.

2. Save a screenshot to a file

Check the status before writing the file: on errors, the body is JSON telling you what went wrong, not an image.

package main

import (
	"io"
	"net/http"
	"os"
)

func main() {
	res, err := http.Get("https://shotsmith.dev/v1/take?access_key=YOUR_ACCESS_KEY&url=https%3A%2F%2Fshotsmith.dev%2Fsample&full_page=true&block_cookie_banners=true&format=png")
	if err != nil {
		panic(err)
	}
	defer res.Body.Close()

	if res.StatusCode != http.StatusOK {
		body, _ := io.ReadAll(res.Body)
		panic(string(body))
	}

	out, err := os.Create("capture.png")
	if err != nil {
		panic(err)
	}
	defer out.Close()
	io.Copy(out, res.Body)
}

Build the query with url.Values rather than string concatenation, so URLs with their own query strings are encoded correctly. The handler below shows how.

3. Serve thumbnails from an HTTP handler

This handler returns a 640px-wide WebP thumbnail of any URL, using Go 1.22’s method-aware routing. cache=true means repeat requests within four hours are instant and don’t use your quota. Errors are passed through unchanged, so your client still sees the error_code.

package main

import (
	"io"
	"log"
	"net/http"
	"net/url"
	"os"
)

// GET /thumbnail?url=https://shotsmith.dev/sample returns a 640px-wide WebP thumbnail.
func thumbnail(w http.ResponseWriter, r *http.Request) {
	target := r.URL.Query().Get("url")
	if target == "" {
		http.Error(w, "url is required", http.StatusBadRequest)
		return
	}

	params := url.Values{}
	params.Set("access_key", os.Getenv("SHOTSMITH_ACCESS_KEY"))
	params.Set("url", target)
	params.Set("format", "webp")
	params.Set("image_width", "640")
	params.Set("block_cookie_banners", "true")
	params.Set("cache", "true")

	res, err := http.Get("https://shotsmith.dev/v1/take?" + params.Encode())
	if err != nil {
		http.Error(w, "could not reach the screenshot API", http.StatusBadGateway)
		return
	}
	defer res.Body.Close()

	// On errors the body is JSON with error_code and error_message: pass it on as-is.
	w.Header().Set("Content-Type", res.Header.Get("Content-Type"))
	if res.StatusCode == http.StatusOK {
		w.Header().Set("Cache-Control", "public, max-age=3600")
	}
	w.WriteHeader(res.StatusCode)
	io.Copy(w, res.Body)
}

func main() {
	http.HandleFunc("GET /thumbnail", thumbnail)
	log.Fatal(http.ListenAndServe(":8080", nil))
}

Anyone who can reach this endpoint spends your renders. In production, check target against the hosts you expect, or sign URLs on the server and let browsers fetch the image directly (see signed URLs).

4. PDFs and other formats

Change format to pdf, webp, avif, html or markdown. PDFs take paper and margin options, such as pdf_paper_format=a4 and pdf_margin=1cm; the PDF docs list them all.

5. Many pages at once

For batches, Go’s goroutines make it tempting to fire hundreds of requests. Your plan has a requests-per-minute limit, and bursts return 429 with a Retry-After header. The simpler route is one bulk request: up to 100 URLs with shared options, delivered by webhook.

6. Errors and timeouts

Every error is JSON with a stable error_code (see the error reference), and failed renders aren’t billed. Synchronous requests must finish within 18 seconds; for anything slower, use async rendering with webhooks. Give your http.Client a timeout of 30 seconds or so, so a network problem can’t hang a goroutine.