- Docs
- Example code
- Go - HTML to Image Example
Go - HTML to Image Example
Authentication with Go
Use HTTP Basic authentication with your API ID as the username and API key as the password. Find both in the dashboard. Keep credentials in server-side configuration or environment variables.
Go example code
The image examples use POST https://hcti.io/v1/image. Recordings use POST https://hcti.io/v1/recording and return a recording ID and media URL. See Recordings for timing, actions, and output options. Both creation endpoints require images:create; see authentication and API keys for scoped credentials and access errors.
Official Go client
Section titled “Official Go client”For typed requests, responses, and signed URL helpers, use the official github.com/htmlcsstoimage/go-client module. Requires Go 1.21 or newer.
go get github.com/htmlcsstoimage/go-clientpackage main
import ( "context" "fmt" "log"
"github.com/htmlcsstoimage/go-client")
func main() { client := hcti.NewClient("your-api-id", "your-api-key")
image, err := client.CreateImage(context.Background(), &hcti.HTMLImageRequest{ HTML: "<div class='box'>Go ✅</div>", CSS: hcti.Ptr(".box { border: 4px solid #03B875; padding: 20px; font-family: 'Roboto'; }"), GoogleFonts: hcti.GoogleFonts{"Roboto"}, }) if err != nil { log.Fatal(err) } fmt.Println(image.URL)}The module’s package name is hcti. Alternatively, use hcti.NewClientFromEnv() to load HCTI_API_ID and HCTI_API_KEY; it returns a client and an error. Optional request fields use pointers: hcti.Ptr(false), hcti.Ptr(0), and hcti.Ptr("") send explicit values, while nil leaves the field unspecified.
Recordings
Section titled “Recordings”Record HTML/CSS or a webpage as an MP4, WebM, or GIF with CreateRecording:
recording, err := client.CreateRecording(ctx, &hcti.URLRecordingRequest{ URL: "https://example.com", RecordingOptions: hcti.RecordingOptions{ DurationMS: 10000, Actions: []hcti.RecordingAction{hcti.RecordingSimpleScrollAction{}}, Output: &hcti.RecordingOutputOptions{Format: hcti.Ptr(hcti.RecordingFormatMP4)}, },})if err != nil { log.Fatal(err)}fmt.Println(recording.RecordingURL)Embed or request RecordingURL directly; it waits until the recording is ready. Use &hcti.HTMLRecordingRequest{HTML: ...} for HTML/CSS. See Recordings for triggers, actions, and output options.
The client also supports URL screenshots, PDF options, templates, batches, image deletion, resizing and cropping, and signed URLs. See the Go client repository for usage and runnable examples, and the package documentation on pkg.go.dev for the API reference.
Direct HTTP example
Section titled “Direct HTTP example”You can also call the API directly using Go’s standard library:
package main
import ( "bytes" "encoding/json" "fmt" "io" "log" "net/http" "time")
const ( userID = "your_user_id" apiKey = "your_api_key")
func main() { data := map[string]string{ "html": "<div class='ping'>Pong ✅</div>", "css": ".ping { padding: 20px; font-family: 'sans-serif'; }", } reqBody, err := json.Marshal(data) if err != nil { log.Fatalf("unable to marshal data: %s", err.Error()) } req, err := http.NewRequest("POST", "https://hcti.io/v1/image", bytes.NewReader(reqBody)) if err != nil { log.Fatalf("unable to create new request: %s", err.Error()) } req.SetBasicAuth(userID, apiKey) req.Header.Set("Content-Type", "application/json") client := &http.Client{Timeout: time.Second * 10} resp, err := client.Do(req) if err != nil { log.Fatalf("request was unsuccessful: %s", err.Error()) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { log.Fatalf("unable to read response body: %s", err.Error()) } if resp.StatusCode < 200 || resp.StatusCode >= 300 { log.Fatalf("API request failed (HTTP %d)", resp.StatusCode) } fmt.Println(string(body))
// {"url":"https://hcti.io/v1/image/f1e2762b-1f95-4f99-ab5d-0444b26dfd42"}}Building a screenshot worker in Go
Section titled “Building a screenshot worker in Go”A directory or monitoring service can queue a screenshot whenever a listing is added or its website changes. A Go worker reads the URL from the job, calls HCTI, and saves the image URL against that listing’s revision. Serving the directory then only needs to read the saved URL.
Pass a context with a deadline to client operations and use a bounded worker pool for batches of websites. Go’s http.Client is safe for concurrent use and should be reused, so avoid constructing a new HTTP client for every queued item. For a moving website preview, the recording example above can capture a scroll through the page instead of a single frame.
Need help?
Talk to a human. Email support@htmlcsstoimage.com and we’ll help you get started.