Conversation events in Java
Send a live transcript from your Java 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 Java application
Sparkle is an HTTP API, so it works from Spring Boot, Quarkus, Micronaut, Jakarta EE or a plain main method: 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.
import com.fasterxml.jackson.databind.ObjectMapper;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.UUID;
public class Sparkle {
static final String API = "https://api.sparkle.ai/public/v1";
static final HttpClient http = HttpClient.newHttpClient();
static String post(String path, String json) throws Exception {
var request = HttpRequest.newBuilder(URI.create(API + path))
.header("X-API-Key", System.getenv("SPARKLE_API_KEY"))
.header("Idempotency-Key", UUID.randomUUID().toString())
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(json))
.build();
return http.send(request, HttpResponse.BodyHandlers.ofString()).body();
}
public static void main(String[] args) throws Exception {
var created = post("/conversations", """
{"title": "Acme renewal call",
"webhook_url": "https://yourapp.com/hooks/sparkle"}""");
var id = new ObjectMapper().readTree(created).at("/conversation/id").asText();
post("/conversations/" + id + "/turns", """
{"turns": [{
"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 Spring Boot
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.
@RequestBody byte[] hands over the body as Sparkle sent it. With the JDK's own com.sun.net.httpserver, read exchange.getRequestBody().readAllBytes().
For a live UI, the conversation's event stream sends the same events as server-sent events.
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HexFormat;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SparkleWebhook {
static boolean verify(String secret, String header, byte[] body) throws Exception {
String t = "";
var signatures = new ArrayList<byte[]>();
for (String part : header.split(",")) {
String[] pair = part.trim().split("=", 2);
if (pair.length < 2) continue;
if (pair[0].equals("t")) t = pair[1];
if (pair[0].equals("v1")) {
try {
signatures.add(HexFormat.of().parseHex(pair[1]));
} catch (IllegalArgumentException notHex) {
// Can't match; another v1 decides.
}
}
}
if (!t.matches("\\d{1,18}")
|| Math.abs(Instant.now().getEpochSecond() - Long.parseLong(t)) > 300) {
return false;
}
var mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
mac.update((t + ".").getBytes(StandardCharsets.UTF_8));
byte[] expected = mac.doFinal(body);
for (byte[] signature : signatures) {
// MessageDigest.isEqual compares in constant time.
if (MessageDigest.isEqual(signature, expected)) return true;
}
return false;
}
@PostMapping("/hooks/sparkle")
public ResponseEntity<String> receive(
@RequestHeader(name = "Sparkle-Signature", defaultValue = "") String header,
@RequestBody byte[] body) throws Exception {
if (!verify(System.getenv("SPARKLE_WEBHOOK_SECRET"), header, body)) {
return ResponseEntity.badRequest().body("invalid signature");
}
JsonNode event = new ObjectMapper().readTree(body);
// A retry repeats the event's id: skip one you have already handled.
if (event.path("type").asText().equals("moment.detected")) {
System.out.println(event.at("/data/kind").asText() + " " + event.at("/data/text").asText());
}
return ResponseEntity.noContent().build();
}
} Send your first transcript
Create a test key, then run the samples above against it. Test conversations are never billed.