Conversation events in Node.js
Send a live transcript from your Node.js 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 Node.js application
Sparkle is an HTTP API, so it works from Next.js, Express, Fastify, NestJS, Hono or a plain script: anything that can call fetch.
-
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.
const API = "https://api.sparkle.ai/public/v1";
async function post(path: string, body: unknown): Promise<Response> {
const res = await fetch(`${API}${path}`, {
method: "POST",
headers: {
"X-API-Key": process.env.SPARKLE_API_KEY ?? "",
"Idempotency-Key": crypto.randomUUID(),
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
if (!res.ok) throw new Error(`${res.status} ${await res.text()}`);
return res;
}
const created = await (
await post("/conversations", {
title: "Acme renewal call",
webhook_url: "https://yourapp.com/hooks/sparkle",
})
).json();
// created.webhook.secret signs this conversation's deliveries. It is shown once.
await post(`/conversations/${created.conversation.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 Next.js
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.
request.text() returns the body as Sparkle sent it. In Express, put express.raw({ type: "application/json" }) on the route instead of express.json().
For a live UI, the conversation's event stream sends the same events as server-sent events.
import { createHmac, timingSafeEqual } from "node:crypto";
function verify(secret: string, header: string, body: string): boolean {
const parts = header.split(",").map((part) => part.trim().split("="));
const t = parts.find(([key]) => key === "t")?.[1] ?? "";
if (!/^\d+$/.test(t) || Math.abs(Date.now() / 1000 - Number(t)) > 300) return false;
const expected = createHmac("sha256", secret).update(`${t}.${body}`).digest();
return parts.some(([key, value = ""]) => {
const signature = Buffer.from(value, "hex");
return key === "v1" && signature.length === expected.length &&
timingSafeEqual(signature, expected);
});
}
export async function POST(request: Request): Promise<Response> {
const body = await request.text();
const header = request.headers.get("Sparkle-Signature") ?? "";
if (!verify(process.env.SPARKLE_WEBHOOK_SECRET ?? "", header, body)) {
return new Response("invalid signature", { status: 400 });
}
const event = JSON.parse(body);
// A retry repeats event.id: skip one you have already handled.
if (event.type === "moment.detected") {
console.log(event.data.kind, event.data.speaker.name, event.data.text);
}
return new Response(null, { status: 204 });
} Send your first transcript
Create a test key, then run the samples above against it. Test conversations are never billed.