Documentation
¶
Overview ¶
Package screenshotapi provides a Go client for the ScreenshotAPI service.
Index ¶
Examples ¶
Constants ¶
const DefaultBaseURL = "https://screenshotapi.to"
const DefaultTimeout = 60 * time.Second
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AuthenticationError ¶
type AuthenticationError struct {
APIError
}
AuthenticationError indicates a missing or malformed API key (HTTP 401).
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a ScreenshotAPI client.
func (*Client) Save ¶
Save takes a screenshot and saves it to the specified file path.
Example ¶
package main
import (
"context"
"fmt"
"log"
"net/http"
"net/http/httptest"
"os"
screenshotapi "github.com/miketromba/screenshotapi-go"
)
func main() {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/png")
w.Header().Set("x-credits-remaining", "199")
w.Header().Set("x-screenshot-id", "ss_example")
w.Header().Set("x-duration-ms", "450")
_, _ = w.Write([]byte("png"))
}))
defer server.Close()
file, err := os.CreateTemp("", "screenshotapi-*.png")
if err != nil {
log.Fatal(err)
}
path := file.Name()
_ = file.Close()
defer os.Remove(path)
client := screenshotapi.NewClient(
"sk_test_abc123",
screenshotapi.WithBaseURL(server.URL),
)
metadata, err := client.Save(context.Background(), screenshotapi.ScreenshotOptions{
URL: "https://example.com",
}, path)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Credits remaining: %d\n", metadata.CreditsRemaining)
}
Output: Credits remaining: 199
func (*Client) Screenshot ¶
Screenshot takes a screenshot with the given options.
Example (AdvancedOptions) ¶
package main
import (
"context"
"fmt"
"log"
"net/http"
"net/http/httptest"
screenshotapi "github.com/miketromba/screenshotapi-go"
)
func main() {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/webp")
w.Header().Set("x-credits-remaining", "198")
w.Header().Set("x-screenshot-id", "ss_advanced")
w.Header().Set("x-duration-ms", "875")
_, _ = w.Write([]byte("webp"))
}))
defer server.Close()
client := screenshotapi.NewClient(
"sk_test_abc123",
screenshotapi.WithBaseURL(server.URL),
)
result, err := client.Screenshot(context.Background(), screenshotapi.ScreenshotOptions{
URL: "https://example.com",
Width: 1440,
Height: 900,
FullPage: true,
Type: screenshotapi.WebP,
Quality: 85,
ColorScheme: screenshotapi.Dark,
WaitUntil: screenshotapi.NetworkIdle2,
BlockAds: true,
RemoveCookieBanners: true,
DevicePixelRatio: 2,
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s %s\n", result.ContentType, result.Metadata.ScreenshotID)
}
Output: image/webp ss_advanced
Example (ErrorHandling) ¶
package main
import (
"context"
"errors"
"fmt"
"net/http"
"net/http/httptest"
screenshotapi "github.com/miketromba/screenshotapi-go"
)
func main() {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusPaymentRequired)
_, _ = w.Write([]byte(`{"error":"Insufficient credits","creditBalance":0}`))
}))
defer server.Close()
client := screenshotapi.NewClient(
"sk_test_abc123",
screenshotapi.WithBaseURL(server.URL),
)
_, err := client.Screenshot(context.Background(), screenshotapi.ScreenshotOptions{
URL: "https://example.com",
})
var creditsErr *screenshotapi.InsufficientCreditsError
if errors.As(err, &creditsErr) {
fmt.Printf("out of credits: %d\n", creditsErr.Balance)
}
}
Output: out of credits: 0
type ColorScheme ¶
type ColorScheme string
const ( Light ColorScheme = "light" Dark ColorScheme = "dark" )
type Geolocation ¶
type Geolocation struct {
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
Accuracy float64 `json:"accuracy,omitempty"`
}
Geolocation configures browser geolocation emulation.
type InsufficientCreditsError ¶
InsufficientCreditsError indicates the account has no credits remaining (HTTP 402).
func (*InsufficientCreditsError) Error ¶
func (e *InsufficientCreditsError) Error() string
type InvalidAPIKeyError ¶
type InvalidAPIKeyError struct {
APIError
}
InvalidAPIKeyError indicates the API key is revoked or invalid (HTTP 403).
type MockupDevice ¶
type MockupDevice string
const ( BrowserMockup MockupDevice = "browser" IPhoneMockup MockupDevice = "iphone" MacBookMockup MockupDevice = "macbook" )
type Option ¶
type Option func(*Client)
Option configures a Client.
func WithBaseURL ¶
WithBaseURL sets a custom base URL for the API.
func WithHTTPClient ¶
WithHTTPClient sets a custom HTTP client.
func WithTimeout ¶
WithTimeout sets the HTTP client timeout.
type ScreenshotFailedError ¶
type ScreenshotFailedError struct {
APIError
}
ScreenshotFailedError indicates the screenshot capture failed server-side (HTTP 500).
type ScreenshotOptions ¶
type ScreenshotOptions struct {
URL string
HTML string
Width int
Height int
FullPage bool
Type ImageType
Quality int
ColorScheme ColorScheme
WaitUntil WaitUntil
WaitForSelector string
Delay int
BlockAds bool
RemoveCookieBanners bool
CSSInject string
JSInject string
StealthMode bool
DevicePixelRatio int
Timezone string
Locale string
CacheTTL int
PreloadFonts bool
RemoveElements []string
RemovePopups bool
MockupDevice MockupDevice
Geolocation *Geolocation
}
ScreenshotOptions configures a screenshot request.