Skip to content

Conversation events in Ruby

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

Sparkle is an HTTP API, so it works from Rails, Sinatra, Hanami, a Sidekiq job or a Rake task: 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.rb standard library
require "json"
require "net/http"
require "securerandom"

API = "https://api.sparkle.ai/public/v1"

def post(path, body)
  Net::HTTP.post(
    URI("#{API}#{path}"),
    body.to_json,
    "X-API-Key" => ENV.fetch("SPARKLE_API_KEY"),
    "Idempotency-Key" => SecureRandom.uuid,
    "Content-Type" => "application/json",
  )
end

created = JSON.parse(post("/conversations", {
  title: "Acme renewal call",
  webhook_url: "https://yourapp.com/hooks/sparkle",
}).body)

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 Sinatra

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.body.read returns the body as Sparkle sent it. In a Rails controller, read request.raw_post.

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

app.rb Sinatra, Ruby 3.0+
require "json"
require "openssl"
require "sinatra"

def verify(secret, header, body)
  parts = header.split(",").map { |part| part.strip.split("=", 2) }
  t = parts.find { |key, _| key == "t" }&.last.to_s
  return false unless t.match?(/\A\d+\z/) && (Time.now.to_i - t.to_i).abs <= 300

  expected = OpenSSL::HMAC.hexdigest("SHA256", secret, "#{t}.#{body}")
  parts.any? { |key, value| key == "v1" && OpenSSL.secure_compare(expected, value.to_s) }
end

post "/hooks/sparkle" do
  body = request.body.read
  header = request.env["HTTP_SPARKLE_SIGNATURE"].to_s
  halt 400, "invalid signature" unless verify(ENV.fetch("SPARKLE_WEBHOOK_SECRET"), header, body)

  event = JSON.parse(body)
  # A retry repeats event["id"]: skip one you have already handled.
  if event["type"] == "moment.detected"
    moment = event["data"]
    puts [moment["kind"], moment["speaker"]["name"], moment["text"]].join(" ")
  end
  status 204
end

Send your first transcript

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