Skip to content

Commit ec3a701

Browse files
committed
add official go client docs/changelog
1 parent b584929 commit ec3a701

2 files changed

Lines changed: 98 additions & 37 deletions

File tree

‎changelog.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ Always improving. Updates to HTML/CSS to Image are posted here.
1616

1717
If you have feature requests, please send them to us: support@htmlcsstoimage.com.
1818

19+
### Official Go client
20+
September 15, 2026
21+
{: .text-delta}
22+
23+
The official HTML/CSS to Image Go client is now available for Go 1.21+. It provides typed requests and responses for HTML/CSS images, URL screenshots, and templates, plus helpers for Google Fonts, PDF options, batches, image deletion, resizing and cropping, and signed URLs.
24+
25+
Install it with `go get github.com/htmlcsstoimage/go-client`. See the [Go examples](/example-code/go/), [client repository](https://github.com/htmlcsstoimage/go-client), and [package documentation on pkg.go.dev](https://pkg.go.dev/github.com/htmlcsstoimage/go-client).
26+
27+
<hr>
28+
1929
### Management API and scoped permissions
2030
September 14, 2026
2131
{: .text-delta}

‎example-code/go.md‎

Lines changed: 88 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,103 @@ parent: Example code
66
permalink: /example-code/go/
77
description: >-
88
Convert HTML to an image (png, jpg or webp) with Go + the HTML/CSS to Image
9-
API. Renders exactly like Google Chrome.
9+
API. Official Go client for Go 1.21+.
1010
---
11-
{% include intro.md language="GoLang" %}
11+
{% include intro.md language="Go" %}
12+
13+
## Official Go client
14+
15+
For typed requests, responses, and signed URL helpers, use the official [`github.com/htmlcsstoimage/go-client`](https://pkg.go.dev/github.com/htmlcsstoimage/go-client) module. Requires Go 1.21 or newer.
16+
17+
```bash
18+
go get github.com/htmlcsstoimage/go-client
19+
```
1220

1321
```go
1422
package main
23+
1524
import (
16-
"bytes"
17-
"encoding/json"
18-
"fmt"
19-
"io/ioutil"
20-
"log"
21-
"net/http"
22-
"time"
25+
"context"
26+
"fmt"
27+
"log"
28+
29+
"github.com/htmlcsstoimage/go-client"
2330
)
31+
32+
func main() {
33+
client := hcti.NewClient("your-api-id", "your-api-key")
34+
35+
image, err := client.CreateImage(context.Background(), hcti.HTMLImageRequest{
36+
HTML: "<div class='box'>Go ✅</div>",
37+
CSS: hcti.Ptr(".box { border: 4px solid #03B875; padding: 20px; font-family: 'Roboto'; }"),
38+
GoogleFonts: hcti.GoogleFonts{"Roboto"},
39+
})
40+
if err != nil {
41+
log.Fatal(err)
42+
}
43+
fmt.Println(image.URL)
44+
}
45+
```
46+
47+
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.
48+
49+
The client also supports URL screenshots, PDF options, templates, batches, image deletion, resizing and cropping, and signed URLs. See the [Go client repository](https://github.com/htmlcsstoimage/go-client) for usage and runnable examples, and the [package documentation on pkg.go.dev](https://pkg.go.dev/github.com/htmlcsstoimage/go-client) for the API reference.
50+
51+
<hr>
52+
53+
## Direct HTTP example
54+
55+
You can also call the API directly using Go's standard library:
56+
57+
```go
58+
package main
59+
60+
import (
61+
"bytes"
62+
"encoding/json"
63+
"fmt"
64+
"io"
65+
"log"
66+
"net/http"
67+
"time"
68+
)
69+
2470
const (
25-
userID = "your_user_id"
26-
apiKey = "your_api_key"
71+
userID = "your_user_id"
72+
apiKey = "your_api_key"
2773
)
74+
2875
func main() {
29-
data := map[string]string{
30-
"html": "<div class='ping'>Pong ✅</div>",
31-
"css": ".ping { padding: 20px; font-family: 'sans-serif'; }",
32-
}
33-
reqBody, err := json.Marshal(data)
34-
if err != nil {
35-
log.Fatalf("unable to marshal data: %s", err.Error())
36-
}
37-
req, err := http.NewRequest("POST", "https://hcti.io/v1/image", bytes.NewReader(reqBody))
38-
if err != nil {
39-
log.Fatalf("unable to create new request: %s", err.Error())
40-
}
41-
req.SetBasicAuth(userID, apiKey)
42-
client := &http.Client{Timeout: time.Second * 10}
43-
resp, err := client.Do(req)
44-
if err != nil {
45-
log.Fatalf("request was unsuccessful: %s", err.Error())
46-
}
47-
defer resp.Body.Close()
48-
body, err := ioutil.ReadAll(resp.Body)
49-
if err != nil {
50-
log.Fatalf("unable to read response body: %s", err.Error())
51-
}
52-
fmt.Println(string(body))
53-
54-
// {"url":"https://hcti.io/v1/image/f1e2762b-1f95-4f99-ab5d-0444b26dfd42"}
76+
data := map[string]string{
77+
"html": "<div class='ping'>Pong ✅</div>",
78+
"css": ".ping { padding: 20px; font-family: 'sans-serif'; }",
79+
}
80+
reqBody, err := json.Marshal(data)
81+
if err != nil {
82+
log.Fatalf("unable to marshal data: %s", err.Error())
83+
}
84+
req, err := http.NewRequest("POST", "https://hcti.io/v1/image", bytes.NewReader(reqBody))
85+
if err != nil {
86+
log.Fatalf("unable to create new request: %s", err.Error())
87+
}
88+
req.SetBasicAuth(userID, apiKey)
89+
req.Header.Set("Content-Type", "application/json")
90+
client := &http.Client{Timeout: time.Second * 10}
91+
resp, err := client.Do(req)
92+
if err != nil {
93+
log.Fatalf("request was unsuccessful: %s", err.Error())
94+
}
95+
defer resp.Body.Close()
96+
body, err := io.ReadAll(resp.Body)
97+
if err != nil {
98+
log.Fatalf("unable to read response body: %s", err.Error())
99+
}
100+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
101+
log.Fatalf("API request failed (HTTP %d)", resp.StatusCode)
102+
}
103+
fmt.Println(string(body))
104+
105+
// {"url":"https://hcti.io/v1/image/f1e2762b-1f95-4f99-ab5d-0444b26dfd42"}
55106
}
56107
```
57108

0 commit comments

Comments
 (0)