Code Examples

Take Screenshots with Go

Ready-to-copy Go code for capturing website screenshots, generating PDFs, and processing pages at scale. Uses net/http (stdlib).

Quick Start

Screenshot in Go — minimal example

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

Goquickstart
resp, err := http.Get("https://api.screenshotdev.com/v1/screenshot?url=https://example.com&access_key=YOUR_ACCESS_KEY")
if err != nil { log.Fatal(err) }
defer resp.Body.Close()
f, _ := os.Create("screenshot.png")
defer f.Close()
io.Copy(f, resp.Body)
Full Example

Production-ready Go script

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

Gofull example
package main

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

const apiKey = "YOUR_ACCESS_KEY"
const baseURL = "https://api.screenshotdev.com/v1/screenshot"

func takeScreenshot(targetURL, format string, width int, fullPage bool) ([]byte, error) {
	params := url.Values{
		"url":        {targetURL},
		"access_key": {apiKey},
		"format":     {format},
		"width":      {fmt.Sprintf("%d", width)},
		"full_page":  {fmt.Sprintf("%t", fullPage)},
	}
	resp, err := http.Get(baseURL + "?" + params.Encode())
	if err != nil {
		return nil, fmt.Errorf("request failed: %w", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		body, _ := io.ReadAll(resp.Body)
		return nil, fmt.Errorf("screenshot failed (%d): %s", resp.StatusCode, body)
	}
	return io.ReadAll(resp.Body)
}

func main() {
	img, err := takeScreenshot("https://example.com", "webp", 1440, true)
	if err != nil {
		log.Fatal(err)
	}
	if err := os.WriteFile("screenshot.webp", img, 0644); err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Saved screenshot.webp (%d bytes)\n", len(img))
}
Advanced

More Go examples

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

PDF Generation

Gopdf
params := url.Values{
	"url":        {"https://example.com"},
	"access_key": {"YOUR_ACCESS_KEY"},
	"format":     {"a4"},
	"landscape":  {"false"},
	"margin":     {"20px"},
}
resp, err := http.Get("https://api.screenshotdev.com/v1/pdf?" + params.Encode())
if err != nil {
	log.Fatal(err)
}
defer resp.Body.Close()

f, _ := os.Create("page.pdf")
defer f.Close()
io.Copy(f, resp.Body)

Batch Screenshots

Gobatch
urls := []string{
	"https://example.com",
	"https://github.com",
	"https://news.ycombinator.com",
}

var wg sync.WaitGroup
for _, u := range urls {
	wg.Add(1)
	go func(target string) {
		defer wg.Done()
		params := url.Values{
			"url":        {target},
			"access_key": {apiKey},
			"format":     {"webp"},
		}
		resp, err := http.Get(baseURL + "?" + params.Encode())
		if err != nil {
			log.Printf("failed %s: %v", target, err)
			return
		}
		defer resp.Body.Close()

		parsed, _ := url.Parse(target)
		filename := parsed.Hostname() + ".webp"
		data, _ := io.ReadAll(resp.Body)
		os.WriteFile(filename, data, 0644)
		fmt.Printf("  %s (%d bytes)\n", filename, len(data))
	}(u)
}
wg.Wait()

Async with Webhooks

Gowebhooks
payload, _ := json.Marshal(map[string]any{
	"url":         "https://example.com",
	"access_key":  "YOUR_ACCESS_KEY",
	"webhook_url": "https://your-app.com/api/webhooks/screenshot",
	"format":      "webp",
	"full_page":   true,
})

resp, err := http.Post(
	"https://api.screenshotdev.com/v1/screenshot",
	"application/json",
	bytes.NewReader(payload),
)
if err != nil {
	log.Fatal(err)
}
defer resp.Body.Close()

var job struct{ ID string `json:"id"` }
json.NewDecoder(resp.Body).Decode(&job)
fmt.Printf("Job queued: %s\n", job.ID)

Custom CSS/JS Injection

Goinject
params := url.Values{
	"url":        {"https://example.com"},
	"access_key": {"YOUR_ACCESS_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"},
}
resp, err := http.Get(baseURL + "?" + params.Encode())
if err != nil {
	log.Fatal(err)
}
defer resp.Body.Close()

data, _ := io.ReadAll(resp.Body)
os.WriteFile("clean.webp", data, 0644)

S3 Upload

Gos3
import (
	"github.com/aws/aws-sdk-go-v2/config"
	"github.com/aws/aws-sdk-go-v2/service/s3"
)

// 1. Capture the screenshot
params := url.Values{
	"url":        {"https://example.com"},
	"access_key": {"YOUR_ACCESS_KEY"},
	"format":     {"webp"},
}
resp, _ := http.Get(baseURL + "?" + params.Encode())
defer resp.Body.Close()
data, _ := io.ReadAll(resp.Body)

// 2. Upload to S3
cfg, _ := config.LoadDefaultConfig(context.TODO())
client := s3.NewFromConfig(cfg)
client.PutObject(context.TODO(), &s3.PutObjectInput{
	Bucket:      aws.String("my-screenshots"),
	Key:         aws.String("captures/example-com.webp"),
	Body:        bytes.NewReader(data),
	ContentType: aws.String("image/webp"),
})

Try it live

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

Setup

Installation

Install the HTTP library used in the examples above.

Terminalinstall
# No dependencies — uses net/http from the standard library

Uses net/http (stdlib). Any HTTP client that supports GET requests will work.

More Examples

Code examples in other languages

Start capturing screenshots
with Go today.

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

Take Screenshots with Go — ScreenshotDEV