Available now. The Go SDK is published on the Go module proxy and works with the install command below. See the package page on pkg.go.dev. If you prefer not to use it, you can also call the REST API from Go with any HTTP client (Call a tool over REST).
The Go SDK is the official Go client. Its methods, field names, allowed values and error codes are generated from the same API contract as the server, so they match the API.
Requirements and install
Go 1.21 or newer. It uses only the standard library. A client is safe to share between goroutines.
go get github.com/Davixlabs/davix-h2i-sdk-go
Create a client
Create an API key first (Create and rotate API keys) and keep it in an environment variable or a secrets manager, never in the code.
import (
"context"
"errors"
"log"
"os"
h2i "github.com/Davixlabs/davix-h2i-sdk-go"
)
client, err := h2i.New(os.Getenv("DAVIX_H2I_API_KEY"))
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
The address defaults to https://app.h2i.davixlabs.io. From version 2 on, the module path ends in /v2.
Run a tool
result, err := client.Images().Resize(ctx, h2i.Params{
"images": "photo.jpg",
"width": 800,
"resizeMode": h2i.ResizeFitCover,
})
Tools are grouped like the API: client.Images().Resize(...), client.Pdfs().Merge(...), client.Ai().Documents().Summarize(...). Every method takes a context first, then h2i.Params with the names of the API reference.
Find every tool and its fields in the tool list; each tool page has an API reference.
The result
| What | Meaning |
|---|---|
result.Data |
The answer: parsed JSON, text or bytes; result.Map() gives a JSON object |
result.Status, result.RequestID |
HTTP status and the id to quote to support |
result.Files() |
Every result file in the answer, each with a signed url |
result.Accepted(), result.JobID() |
True when the server accepted a background job (HTTP 202), and its id |
result.Attempts, result.IdempotencyKey |
How many tries it took and the key used |
Send files and download results
A file parameter accepts a path string, h2i.FilePath("a.png") to mark a path explicitly, or h2i.File{Data: bytes, Filename: "a.png"} for bytes you already have. A path is read from disk only for fields the API defines as files.
Results are files with a signed address. Download them with the client; the API key is not sent with the download, and only addresses of your Davix H2I host are accepted.
err = client.DownloadTo(ctx, result.Files()[0]["url"].(string), "photo-800.jpg") // saves the file
data, err := client.Download(ctx, address) // or returns the bytes
Options
Client options
| Option | What it does |
|---|---|
h2i.WithBaseURL(url) |
Another Davix H2I address. It must be https (http is allowed only for localhost) |
h2i.WithTimeout(d) |
Time for one attempt, 60 seconds by default |
h2i.WithMaxRetries(n), h2i.WithMaxRetryDelay(d) |
2 retries and a longest wait of 30 seconds by default |
h2i.WithoutAutoIdempotencyKey() |
Turns the automatic key off, see Retries |
h2i.WithValidation() |
Check parameters before sending |
h2i.WithBearerAuth() |
Send the key as a Bearer token instead of X-Api-Key |
h2i.WithHTTPClient(c) |
Your own http.Client, for a proxy for example. Redirects are always refused |
Options of one call
Options of one call are extra arguments after the parameters: h2i.IdempotencyKey(key), h2i.Header(name, value), h2i.Timeout(d), h2i.MaxRetries(n), h2i.NoRetry(), h2i.Validate() and h2i.Strict().
client.Images().Resize(ctx, h2i.Params{"images": "photo.jpg", "width": 800}, h2i.IdempotencyKey("order-42"), h2i.Timeout(2*time.Minute))
Retries and idempotency
The client retries only what is safe to repeat: a rate limit answer (429, it waits as long as the server says), and 503 or 504 answers that are marked retryable, for calls that cannot do the work twice. By default it makes one idempotency key for each call and reuses it on every retry, so a retry never charges you twice. You can give your own key, which must be 8 to 128 characters from letters, digits and . _ : -, and you can turn retries or the automatic key off. See Error codes and rate limits.
Errors
result, err := client.Images().Resize(ctx, h2i.Params{"images": "photo.jpg", "width": 800})
var failure *h2i.Error
if errors.As(err, &failure) {
if failure.Code == h2i.CodeMonthlyQuotaExceeded {
log.Println("Allowance used up:", failure.Hint)
} else {
log.Println(failure.Status, failure.Code, failure.Hint, failure.RequestID)
}
}
Every failure is an *h2i.Error; find it with errors.As. Its Kind says what went wrong: KindValidation, KindUpload, KindAuth, KindPlan, KindQuota, KindRateLimit, KindProcessing, KindServer, KindNotFound, KindConflict, KindAI, KindKey, KindJob, KindSignedURL, KindAPI, KindNetwork, KindTimeout or KindConfig (the SDK was used wrongly). It also has Code, Status, Hint, Details, RequestID, RenewalURL, Retryable, RetryAfter and Body. Every code is a constant such as h2i.CodeMonthlyQuotaExceeded.
Quote the request id when you contact support. The meaning of each code is in Error codes and rate limits.
Look up fields, limits and your plan
client.Schema("images.resize") // type, limits, default and allowed values of each field
client.RuntimeCapabilities(ctx) // what the server offers
client.AccountCapabilities(ctx) // your plan, limits and usage
client.RefreshContract(ctx) // use the live contract instead of the bundled one
The schema of a tool gives each field’s type, limits, default and allowed values. The account information shows what your plan allows and what you have used.
Reach a new endpoint
If the API gets an endpoint this version does not know yet, use the generic request method. It only reaches the public /v1/ API.
client.Request(ctx, "GET", "/v1/usage", h2i.GenericRequest{})
client.Request(ctx, "GET", "/v1/jobs", h2i.GenericRequest{Query: map[string]any{"limit": 10}})
Name your app in the History
Every processing call accepts an optional app parameter. Its text is shown under each result in your History, for example My shop importer.
Safe by design
- The API key is sent only in a header. It is never put in an address or a body, never printed (showing the client does not reveal it), and never sent to a redirect or to a download address.
- The client refuses plain http for any host except your own computer, and refuses an address with a password in it.
- Redirects are not followed.
Other clients: Node SDK, Python SDK, PHP SDK, Java SDK, .NET SDK, Ruby SDK.
Checked against the Go SDK source code and API contract 3.0.0.
