Conversation events in Go
Send a live transcript from your Go app. Sparkle sends a signed webhook the moment someone makes a decision, asks a question or commits to something.
Plain HTTP, no SDK to install. Test keys are never billed.
Integrate with any Go application
Sparkle is an HTTP API, so it works from net/http, Chi, Gin, Echo or a CLI: anything that can make an HTTPS request.
-
Create a conversation with your webhook URL. Keep the
webhook.secretit returns: it is shown once. -
Post each turn once it is final. Every POST carries your
X-API-Keyand a freshIdempotency-Key.
package main
import (
"bytes"
"crypto/rand"
"encoding/json"
"net/http"
"os"
)
const api = "https://api.sparkle.ai/public/v1"
func post(path string, body any) (*http.Response, error) {
data, _ := json.Marshal(body)
req, _ := http.NewRequest(http.MethodPost, api+path, bytes.NewReader(data))
req.Header.Set("X-API-Key", os.Getenv("SPARKLE_API_KEY"))
req.Header.Set("Idempotency-Key", rand.Text())
req.Header.Set("Content-Type", "application/json")
return http.DefaultClient.Do(req)
}
func main() {
res, err := post("/conversations", map[string]any{
"title": "Acme renewal call",
"webhook_url": "https://yourapp.com/hooks/sparkle",
})
if err != nil {
panic(err)
}
defer res.Body.Close()
var created struct {
Conversation struct{ ID string } `json:"conversation"`
}
if err := json.NewDecoder(res.Body).Decode(&created); err != nil {
panic(err)
}
post("/conversations/"+created.Conversation.ID+"/turns", map[string]any{
"turns": []map[string]any{{
"id": "t2", "final": true, "speaker": "dana", "name": "Dana",
"channel": "remote", "start": 21.0, "end": 23.4,
"text": "I'll get the SSO fix out by Friday.",
}},
})
} Receive moments with net/http
Each moment arrives as a moment.detected POST. Its Sparkle-Signature header
is an HMAC of a timestamp and the body, keyed with the webhook's whsec_ secret.
- Check the signature on the raw body, before parsing it.
- Reject a timestamp more than 5 minutes from your clock.
- Accept any matching v1: while a rotated secret overlaps, each delivery carries two.
- Answer 2xx within 30 seconds. A retry repeats the event id, so skip one you have handled.
io.ReadAll reads the body as Sparkle sent it. handleSparkle is a plain handler function: Chi mounts it as it is, Gin through gin.WrapF.
For a live UI, the conversation's event stream sends the same events as server-sent events.
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"io"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
)
func verify(secret, header string, body []byte) bool {
var t string
var signatures [][]byte
for part := range strings.SplitSeq(header, ",") {
key, value, _ := strings.Cut(strings.TrimSpace(part), "=")
switch key {
case "t":
t = value
case "v1":
if signature, err := hex.DecodeString(value); err == nil {
signatures = append(signatures, signature)
}
}
}
signedAt, err := strconv.ParseInt(t, 10, 64)
if err != nil || time.Since(time.Unix(signedAt, 0)).Abs() > 5*time.Minute {
return false
}
mac := hmac.New(sha256.New, []byte(secret))
mac.Write([]byte(t + "."))
mac.Write(body)
expected := mac.Sum(nil)
for _, signature := range signatures {
if hmac.Equal(signature, expected) {
return true
}
}
return false
}
func handleSparkle(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
secret := os.Getenv("SPARKLE_WEBHOOK_SECRET")
if err != nil || !verify(secret, r.Header.Get("Sparkle-Signature"), body) {
http.Error(w, "invalid signature", http.StatusBadRequest)
return
}
var event struct {
Type string `json:"type"`
Data struct {
Kind string `json:"kind"`
Text string `json:"text"`
} `json:"data"`
}
if err := json.Unmarshal(body, &event); err != nil {
http.Error(w, "invalid body", http.StatusBadRequest)
return
}
// A retry repeats the event's id: skip one you have already handled.
if event.Type == "moment.detected" {
log.Println(event.Data.Kind, event.Data.Text)
}
w.WriteHeader(http.StatusNoContent)
}
func main() {
http.HandleFunc("POST /hooks/sparkle", handleSparkle)
log.Fatal(http.ListenAndServe(":3000", nil))
} Send your first transcript
Create a test key, then run the samples above against it. Test conversations are never billed.