Allscreenshots Docs
SDKs

Go SDK

Idiomatic Go SDK with context support

Go SDK

The official Go SDK for the AllScreenshots API with idiomatic Go patterns and context support.

Source code: GitHub | Package: github.com/allscreenshots/allscreenshots-sdk-go

Installation

go get github.com/allscreenshots/allscreenshots-sdk-go

Quick start

package main

import (
    "context"
    "log"
    "os"

    "github.com/allscreenshots/allscreenshots-sdk-go/pkg/allscreenshots"
)

func main() {
    client := allscreenshots.NewClient()

    imageData, err := client.Screenshot(context.Background(),
        &allscreenshots.ScreenshotRequest{
            URL:    "https://github.com",
            Device: "Desktop HD",
        })
    if err != nil {
        log.Fatal(err)
    }

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

Configuration

Functional options

client := allscreenshots.NewClient(
    allscreenshots.WithAPIKey("your-api-key"),
    allscreenshots.WithBaseURL("https://api.allscreenshots.com"),
    allscreenshots.WithTimeout(60*time.Second),
    allscreenshots.WithMaxRetries(3),
    allscreenshots.WithHTTPClient(customHTTPClient),
)

Environment variables

Set ALLSCREENSHOTS_API_KEY to automatically configure authentication:

// Client will read API key from environment
client := allscreenshots.NewClient()

Capture screenshots

Basic capture

imageData, err := client.Screenshot(ctx, &allscreenshots.ScreenshotRequest{
    URL: "https://example.com",
})
if err != nil {
    log.Fatal(err)
}

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

With options

imageData, err := client.Screenshot(ctx, &allscreenshots.ScreenshotRequest{
    URL:                "https://example.com",
    Device:             "Desktop HD",
    Format:             "png",
    FullPage:           true,
    DarkMode:           true,
    BlockAds:           true,
    BlockCookieBanners: true,
    Delay:              1000,
})

Device presets

// Desktop
desktop, _ := client.Screenshot(ctx, &allscreenshots.ScreenshotRequest{
    URL:    "https://example.com",
    Device: "Desktop HD",
})

// Mobile
mobile, _ := client.Screenshot(ctx, &allscreenshots.ScreenshotRequest{
    URL:    "https://example.com",
    Device: "iPhone 15",
})

// Tablet
tablet, _ := client.Screenshot(ctx, &allscreenshots.ScreenshotRequest{
    URL:    "https://example.com",
    Device: "iPad Pro 11",
})

Async jobs

For long-running captures:

// Create async job
job, err := client.ScreenshotAsync(ctx, &allscreenshots.ScreenshotRequest{
    URL:      "https://example.com",
    FullPage: true,
})
if err != nil {
    log.Fatal(err)
}

fmt.Printf("Job ID: %s\n", job.JobID)

// Poll for completion
result, err := client.GetJob(ctx, job.JobID)
if err != nil {
    log.Fatal(err)
}

if result.Status == "completed" {
    imageData, _ := client.GetJobResult(ctx, job.JobID)
    os.WriteFile("screenshot.png", imageData, 0644)
}

Bulk capture

Process multiple URLs efficiently:

bulkJob, err := client.Bulk(ctx, &allscreenshots.BulkRequest{
    URLs: []allscreenshots.BulkURL{
        {URL: "https://example1.com"},
        {URL: "https://example2.com"},
        {URL: "https://example3.com"},
    },
    Defaults: &allscreenshots.BulkDefaults{
        Device: "Desktop HD",
        Format: "png",
    },
})

// Check status
status, _ := client.GetBulkJob(ctx, bulkJob.BulkJobID)

Composition

Combine multiple screenshots into a single image:

composed, err := client.Compose(ctx, &allscreenshots.ComposeRequest{
    URL: "https://example.com",
    Variants: []allscreenshots.Variant{
        {Device: "Desktop HD", Label: "Desktop"},
        {Device: "iPhone 15", Label: "Mobile"},
    },
    Output: &allscreenshots.OutputOptions{
        Layout:  "horizontal",
        Spacing: 20,
    },
})

Schedules

Create recurring screenshot captures:

// Create a schedule
schedule, err := client.Schedules.Create(ctx, &allscreenshots.CreateScheduleRequest{
    Name:     "Daily Homepage",
    URL:      "https://example.com",
    Schedule: "0 9 * * *",
    Timezone: "America/New_York",
})

// List schedules
schedules, _ := client.Schedules.List(ctx)

// Get captures
captures, _ := client.Schedules.GetCaptures(ctx, schedule.ScheduleID)

Usage tracking

Monitor your API usage:

usage, err := client.Usage.Get(ctx)
fmt.Printf("Screenshots: %d/%d\n", usage.ScreenshotCount, usage.Quota)

Error handling

The SDK provides typed errors with helper functions:

imageData, err := client.Screenshot(ctx, request)
if err != nil {
    if allscreenshots.IsRateLimited(err) {
        var rateLimitErr *allscreenshots.RateLimitError
        errors.As(err, &rateLimitErr)
        fmt.Printf("Rate limited. Retry after %d seconds\n", rateLimitErr.RetryAfter)
        return
    }

    if allscreenshots.IsUnauthorized(err) {
        fmt.Println("Invalid API key")
        return
    }

    if allscreenshots.IsValidationError(err) {
        fmt.Printf("Validation error: %s\n", err.Error())
        return
    }

    if allscreenshots.IsServerError(err) {
        fmt.Printf("Server error: %s\n", err.Error())
        return
    }

    log.Fatal(err)
}

License

Apache License 2.0

On this page