Go is the language of choice for high-performance backend services, APIs, and infrastructure tools. Its fast HTTP client, excellent concurrency primitives, and small binary footprint make it ideal for services that call external APIs at scale. Integrating SnapAPI into a Go application is straightforward: the standard library HTTP client handles all API communication, goroutines handle concurrent screenshot capture, and channels coordinate results back to the caller.
Basic SnapAPI Client in Go
The simplest SnapAPI Go integration uses net/http directly. Create a snapapi package in your Go project:
package snapapi
import (
"fmt"
"io"
"net/http"
"net/url"
"os"
)
const baseURL = "https://snapapi.pics/screenshot"
type Client struct {
APIKey string
HTTPClient *http.Client
}
func New() *Client {
return &Client{
APIKey: os.Getenv("SNAPAPI_KEY"),
HTTPClient: &http.Client{Timeout: 30 * time.Second},
}
}
func (c *Client) Capture(targetURL string, width int) ([]byte, error) {
params := url.Values{}
params.Set("access_key", c.APIKey)
params.Set("url", targetURL)
params.Set("viewport_width", fmt.Sprintf("%d", width))
resp, err := c.HTTPClient.Get(baseURL + "?" + params.Encode())
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("snapapi: status %d", resp.StatusCode)
}
return io.ReadAll(resp.Body)
}
Concurrent Screenshot Capture with Goroutines
Go's goroutines make concurrent screenshot capture of multiple URLs trivial. Use a worker pool pattern to capture many URLs in parallel while limiting concurrency:
func CaptureAll(client *Client, urls []string, workers int) map[string][]byte {
jobs := make(chan string, len(urls))
results := make(chan struct{ url string; data []byte }, len(urls))
for i := 0; i < workers; i++ {
go func() {
for u := range jobs {
data, _ := client.Capture(u, 1280)
results <- struct{ url string; data []byte }{u, data}
}
}()
}
for _, u := range urls {
jobs <- u
}
close(jobs)
out := make(map[string][]byte, len(urls))
for range urls {
r := <-results
out[r.url] = r.data
}
return out
}
Set workers to 10 for bulk captures. Each goroutine processes URLs from the jobs channel until it is closed. Results are collected through the results channel into a map keyed by URL.
Uploading Screenshots to S3 from Go
Store captured screenshots to S3 using the AWS SDK for Go v2:
import (
"bytes"
"context"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
func UploadToS3(ctx context.Context, s3Client *s3.Client,
bucket, key string, data []byte) error {
_, err := s3Client.PutObject(ctx, &s3.PutObjectInput{
Bucket: &bucket,
Key: &key,
Body: bytes.NewReader(data),
ContentType: aws.String("image/png"),
})
return err
}
HTTP Handler for On-Demand Screenshot Capture
Expose screenshot capture as an HTTP endpoint in your Go service:
func ScreenshotHandler(snap *snapapi.Client) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
target := r.URL.Query().Get("url")
if target == "" {
http.Error(w, "url required", http.StatusBadRequest)
return
}
data, err := snap.Capture(target, 1280)
if err != nil {
http.Error(w, "capture failed", http.StatusBadGateway)
return
}
w.Header().Set("Content-Type", "image/png")
w.Write(data)
}
}
Getting Started
Sign up free at snapapi.pics/dashboard. No credit card required. 200 screenshots per month on the free plan. Full parameter reference at snapapi.pics/docs.