Go

Take screenshots using Go with our official SDK or plain HTTP requests.

Installation

go get github.com/lambdashot/lambdashot-go

Quick Start

package main

import (
    "os"
    "github.com/lambdashot/lambdashot-go"
)

func main() {
    client := lambdashot.NewClient("YOUR_ACCESS_KEY", "YOUR_SECRET_KEY")

    screenshot, err := client.Take(lambdashot.TakeOptions{
        URL:           "https://example.com",
        Format:        "png",
        ViewportWidth: 1920,
        ViewportHeight: 1080,
    })
    if err != nil {
        panic(err)
    }

    os.WriteFile("screenshot.png", screenshot, 0644)
}

Generate Signed URL

url := client.GenerateSignedURL(lambdashot.TakeOptions{
    URL:      "https://example.com",
    Format:   "png",
    BlockAds: true,
})

fmt.Println(url)
// https://api.lambdashot.com/take?url=...&signature=...

Options

screenshot, err := client.Take(lambdashot.TakeOptions{
    URL: "https://example.com",

    // Output
    Format:       "png",
    ImageQuality: 90,

    // Viewport
    ViewportWidth:     1920,
    ViewportHeight:    1080,
    ViewportDevice:    "iphone_15_pro",
    DeviceScaleFactor: 2,

    // Full page
    FullPage:       true,
    FullPageScroll: true,

    // Blocking
    BlockAds:           true,
    BlockCookieBanners: true,
    BlockTrackers:      true,

    // Customization
    DarkMode: true,
    Delay:    2,
})

Without SDK (net/http)

package main

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

func main() {
    params := url.Values{}
    params.Set("access_key", "YOUR_ACCESS_KEY")
    params.Set("url", "https://example.com")
    params.Set("format", "png")
    params.Set("viewport_width", "1920")

    resp, err := http.Get("https://api.lambdashot.com/take?" + params.Encode())
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()

    screenshot, _ := io.ReadAll(resp.Body)
    os.WriteFile("screenshot.png", screenshot, 0644)
}

Video Recording

video, err := client.Animate(lambdashot.AnimateOptions{
    URL:      "https://example.com",
    Scenario: "scroll",
    Duration: 10,
    Format:   "mp4",
})
if err != nil {
    panic(err)
}

os.WriteFile("video.mp4", video, 0644)

Error Handling

screenshot, err := client.Take(lambdashot.TakeOptions{
    URL: "https://example.com",
})

if err != nil {
    if lsErr, ok := err.(*lambdashot.Error); ok {
        switch lsErr.Code {
        case "timeout_error":
            fmt.Println("Screenshot timed out, retrying...")
        case "access_key_invalid":
            fmt.Println("Check your API key")
        default:
            fmt.Printf("Error: %s\n", lsErr.Message)
        }
    }
}

Concurrent Screenshots

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

var wg sync.WaitGroup
screenshots := make([][]byte, len(urls))

for i, url := range urls {
    wg.Add(1)
    go func(i int, url string) {
        defer wg.Done()
        screenshot, _ := client.Take(lambdashot.TakeOptions{
            URL:    url,
            Format: "png",
        })
        screenshots[i] = screenshot
    }(i, url)
}

wg.Wait()