Skip to content

First inbound call to your webhook ​

Goal: buy a number, point it at your server, and answer callers with a menu — in about 30 lines.

1. Buy a number and route it to your webhook ​

ts
import { Kolwire } from "@kolwire/sdk";
const vi = new Kolwire();

const { data } = await vi.numbers.search({ country: "US", area_code: "415" });
const number = await vi.numbers.buy({
  phone_number: data[0].phone_number,
  voice_config: { mode: "webhook", url: "https://example.com/voice" },
});
console.log("Call", number.phone_number);
python
from kolwire import Kolwire
client = Kolwire()

results = client.numbers.search(country="US", area_code="415")
number = client.numbers.buy(
    phone_number=results.data[0].phone_number,
    voice_config={"mode": "webhook", "url": "https://example.com/voice"},
)
print("Call", number.phone_number)

Local development

Expose your machine with any tunnel (cloudflared tunnel --url http://localhost:3000, ngrok http 3000) and use that URL.

2. Answer with commands ​

When a call arrives we POST a signed event (call.initiated, then call.answered, call.gather.completed, …) to your URL. Reply with { "commands": [...] } to control the call.

ts
import express from "express";
import { Kolwire, VoiceResponse, say } from "@kolwire/sdk";

const vi = new Kolwire({ webhookSecret: process.env.KOLWIRE_WEBHOOK_SECRET });
const app = express();

// Keep the raw body: signatures are computed over the exact bytes we sent.
app.post("/voice", express.raw({ type: "application/json" }), async (req, res) => {
  const event = await vi.webhooks.constructEvent(req.body, req.headers);

  switch (event.type) {
    case "call.initiated":
      return res.json(
        new VoiceResponse()
          .answer()
          .gather({ input: ["dtmf", "speech"], max_digits: 1, prompt: say("Press 1 for sales, 2 for support.") }),
      );
    case "call.gather.completed":
      if (event.data.digits === "1") return res.json(new VoiceResponse().say("Connecting you to sales.").enqueue("sales"));
      return res.json(new VoiceResponse().say("Our assistant will help you.").connectAgent("agt_support"));
    default:
      return res.json({ commands: [] });
  }
});

app.listen(3000);
python
import os
from fastapi import FastAPI, Request
from kolwire import Kolwire, VoiceResponse, say

client = Kolwire(webhook_secret=os.environ["KOLWIRE_WEBHOOK_SECRET"])
app = FastAPI()

@app.post("/voice")
async def voice(request: Request):
    event = client.webhooks.construct_event(await request.body(), request.headers)

    if event.type == "call.initiated":
        return VoiceResponse().answer().gather(
            input=["dtmf", "speech"], max_digits=1, prompt=say("Press 1 for sales, 2 for support.")
        ).to_dict()
    if event.type == "call.gather.completed":
        if event.data.digits == "1":
            return VoiceResponse().say("Connecting you to sales.").enqueue("sales").to_dict()
        return VoiceResponse().say("Our assistant will help you.").connect_agent("agt_support").to_dict()
    return {"commands": []}

The signing secret comes from the webhook endpoint or number configuration in the dashboard. See webhook security.

3. Call your number ​

You'll hear the menu. Then look at what happened:

ts
for await (const call of vi.calls.list({ direction: "inbound", limit: 5 })) {
  const insights = await vi.calls.insights(call.id);
  console.log(call.from, call.status, insights.summary);
  break;
}
python
call = client.calls.list(direction="inbound", limit=5).get_page().data[0]
print(call.id, call.status)
print(client.calls.insights(call.id).summary)

Every call is recorded and transcribed by default (per-number recording_enabled / transcription_enabled override it). When post-call processing finishes you receive call.recording.ready, call.transcript.ready and call.intelligence.ready.

Prefer no server? ​

  • Route the number straight to an AI agent: voice_config: { mode: "agent", agent_id }.
  • Or to a saved call flow: voice_config: { mode: "flow", flow_id }.