Skip to content

Conversation events in Python

Send a live transcript from your Python 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 Python application

Sparkle is an HTTP API, so it works from Django, Flask, FastAPI, a Celery worker or a notebook: anything that can make an HTTPS request.

  1. Create a conversation with your webhook URL. Keep the webhook.secret it returns: it is shown once.
  2. Post each turn once it is final. Every POST carries your X-API-Key and a fresh Idempotency-Key.
Five-minute quickstart
send.py requests
import os
import uuid

import requests

API = "https://api.sparkle.ai/public/v1"
headers = {"X-API-Key": os.environ["SPARKLE_API_KEY"]}

created = requests.post(
    f"{API}/conversations",
    headers={**headers, "Idempotency-Key": str(uuid.uuid4())},
    json={
        "title": "Acme renewal call",
        "webhook_url": "https://yourapp.com/hooks/sparkle",
    },
).json()

requests.post(
    f"{API}/conversations/{created['conversation']['id']}/turns",
    headers={**headers, "Idempotency-Key": str(uuid.uuid4())},
    json={"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 Flask

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.get_data() returns the body as Sparkle sent it. In FastAPI, read await request.body(); in Django, request.body.

For a live UI, the conversation's event stream sends the same events as server-sent events.

server.py Flask, Python 3.7+
import hashlib
import hmac
import json
import os
import time

from flask import Flask, request

app = Flask(__name__)


def verify(secret: str, header: str, body: bytes) -> bool:
    parts = [part.strip().partition("=") for part in header.split(",")]
    t = next((value for key, _, value in parts if key == "t"), "")
    if not (t.isascii() and t.isdigit()):
        return False
    try:
        if abs(int(time.time()) - int(t)) > 300:
            return False
    except ValueError:
        return False
    mac = hmac.new(secret.encode(), f"{t}.".encode() + body, hashlib.sha256)
    expected = mac.hexdigest().encode()
    return any(
        hmac.compare_digest(expected, value.encode())
        for key, _, value in parts
        if key == "v1"
    )


@app.post("/hooks/sparkle")
def sparkle_webhook():
    body = request.get_data()
    header = request.headers.get("Sparkle-Signature", "")
    if not verify(os.environ["SPARKLE_WEBHOOK_SECRET"], header, body):
        return "invalid signature", 400
    event = json.loads(body)
    # A retry repeats event["id"]: skip one you have already handled.
    if event["type"] == "moment.detected":
        moment = event["data"]
        print(moment["kind"], moment["speaker"]["name"], moment["text"])
    return "", 204

Send your first transcript

Create a test key, then run the samples above against it. Test conversations are never billed.