v2.4.0 · STABLE · PKG.GO.DEV
SDKS · GO

The Go SDK.

Context-aware by design — every call takes a context.Context for cancellation and deadlines. Errors are typed values you inspect with errors.As, and list endpoints expose Go 1.23 range-over-func iterators that page for you.

go-sdk/v2 · 2.4.0 Go 1.22+ context-aware Apache-2.0

Install

Add the module with go get. It depends only on the standard library and golang.org/x.

SHELLinstall
$ go get github.com/threatdefendr/go-sdk/v2

Authenticate

Build one client and share it; it is safe for concurrent use. Read the token from the environment and set the workspace once.

GOclient.go
import td "github.com/threatdefendr/go-sdk/v2" c := td.NewClient(td.Config{ Token: os.Getenv("TD_TOKEN"), Workspace: "acme-prod", })

Quickstart

Pass a context to every call. Here, isolate a host and read the action id back — the call blocks until the response or the context's deadline, whichever comes first.

GOisolate a host
ctx := context.Background() resp, err := c.Contain.IsolateHost(ctx, td.IsolateHostInput{ HostID: "h_47193", Reason: "IRONVEIL signed-driver match", }) if err != nil { log.Fatal(err) } fmt.Println(resp.ActionID)

Pagination

List endpoints return a range-over-func iterator: loop with for ... range and the SDK fetches each page as you go, yielding the error in the loop so you handle it inline.

GOrange over every page
for det, err := range c.Detections.List(ctx, td.ListDetections{State: "LIVE"}) { if err != nil { log.Fatal(err) } fmt.Println(det.ID, det.Title) }

Retries & deadlines

Set MaxRetries for automatic backoff on 429 and 5xx; bound any call with a context deadline so a slow upstream can't hang a request.

GObackoff + per-call deadline
c := td.NewClient(td.Config{ Token: os.Getenv("TD_TOKEN"), Workspace: "acme-prod", MaxRetries: 5, // backoff on 429 / 5xx }) ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() resp, err := c.Cases.Create(ctx, input)
Cancellation propagates. Cancelling the context aborts the in-flight request and any pending retry — wire your server's request context straight through and a dropped client frees the call immediately.

Errors

API failures come back as a *td.Error. Use errors.As to reach the status, retry hint, and request id.

GOtyped error handling
resp, err := c.Cases.Create(ctx, input) var apiErr *td.Error if errors.As(err, &apiErr) { switch apiErr.Status { case 429: time.Sleep(apiErr.RetryAfter) case 401, 403: refresh() default: log.Printf("td api %d req=%s", apiErr.Status, apiErr.RequestID) } }

Where to go next

← PREV TypeScript SDK NEXT → CLI