Integrations

Add TryVox Calling to Your CRM

A Call button on each lead in your CRM, with the call's status and duration saved on the lead.

This is a calling example under an existing TryVox account. It does not onboard independent CRM customers or implement a shared master wallet. See Sub-accounts for the existing management APIs and the remaining white-label onboarding requirements.

Your CRM keeps its own UI and data. When a user clicks Call on a lead, your CRM backend asks TryVox to place the call, and TryVox's signed webhooks update the lead.

How it works

CRM user ─▶ CRM page ─▶ CRM backend ── TryVox Voice API ──▶ lead's phone
                                            │  lead answers: connect the CRM user's phone
TryVox ── signed webhook ──▶ CRM backend ──▶ lead activity (status, duration)

What the user sees:

Rahul Sharma
+91 98123 45678

[ Call ]

Status: completed
Duration: 02:14

Before you start

  • The setup in Before any guide: API key, KYC, wallet balance, a TryVox number.
  • The shared tryvox.js and .env from the platform guide, plus AGENT_PHONE: the phone to ring when the lead answers.
  • A public HTTPS URL for webhooks, for example ngrok http 3000.

Example

crm.js
// Click-to-call inside your CRM, with call status on the lead.
//
//   CRM page --POST /api/leads/:id/call--> CRM backend --TryVox API--> lead's phone
//   TryVox   --signed webhook-->            CRM backend --> lead activity
//
// Run: npm run crm   (Node 20.6+; uses tryvox.js from this folder)

import http from 'node:http';
import { env, tryvox, isFromTryVox, readBody, send } from './tryvox.js';

const { TRYVOX_NUMBER, PUBLIC_URL, AGENT_PHONE } = env('TRYVOX_NUMBER', 'TRYVOX_WEBHOOK_SECRET', 'PUBLIC_URL', 'AGENT_PHONE');
const PORT = Number(process.env.PORT ?? 3000);

// Your CRM's data. In a real CRM these are database tables.
const leads = new Map([['lead-1042', { id: 'lead-1042', name: 'Rahul Sharma', phone: '+919812345678', calls: [] }]]);
const leadByCall = new Map(); // request_uuid -> lead id

async function callLead(lead) {
  const { data } = await tryvox('POST', '/v1/voice/accounts/{auth_id}/calls', {
    from: TRYVOX_NUMBER,
    to: lead.phone,
    answer_url: `${PUBLIC_URL}/webhooks/tryvox/answer`,
    status_callback_url: `${PUBLIC_URL}/webhooks/tryvox`,
    hangup_url: `${PUBLIC_URL}/webhooks/tryvox`,
    webhook_secret: process.env.TRYVOX_WEBHOOK_SECRET,
  });
  // Voice API calls carry no reference of yours, so remember which lead
  // this request_uuid belongs to. Every webhook for the call carries it.
  leadByCall.set(data.request_uuid, lead.id);
  lead.calls.unshift({ id: data.request_uuid, status: data.status, duration: 0 });
  return data;
}

function recordEvent(event) {
  const lead = leads.get(leadByCall.get(event.CallUUID));
  const call = lead?.calls.find((c) => c.id === event.CallUUID);
  if (!call) return; // not a call this CRM placed
  if (event.Event === 'Hangup') Object.assign(call, { status: event.Status, duration: event.Billsec ?? 0, done: true });
  else if (!call.done) call.status = event.Status;
}

// When the lead answers, connect them to the CRM user's phone.
function answerVoxml() {
  return { voxml_version: '1.0', instructions: [{ verb: 'Dial', numbers: [{ number: AGENT_PHONE }] }] };
}

const page = `<!doctype html><meta charset="utf-8"><title>Lead</title>
<div id="lead"></div>
<script>
  // Calls YOUR backend only. TryVox credentials stay on the server.
  const id = 'lead-1042';
  const mmss = (s) => String(Math.floor(s / 60)).padStart(2, '0') + ':' + String(s % 60).padStart(2, '0');
  async function render() {
    const lead = await (await fetch('/api/leads/' + id)).json();
    const last = lead.calls[0];
    document.getElementById('lead').innerHTML =
      '<b>' + lead.name + '</b><br>' + lead.phone + '<br><button onclick="call()">Call</button>' +
      (last ? '<p>Status: ' + last.status + '<br>Duration: ' + mmss(last.duration) + '</p>' : '');
  }
  async function call() {
    await fetch('/api/leads/' + id + '/call', { method: 'POST' });
    render();
  }
  render();
  setInterval(render, 2000);
</script>`;

http
  .createServer(async (req, res) => {
    const url = new URL(req.url, 'http://localhost');
    const [, api, kind, id, action] = url.pathname.split('/'); // /api/leads/:id/call
    try {
      if (req.method === 'GET' && url.pathname === '/') return send(res, 200, page, 'text/html');

      if (api === 'api' && kind === 'leads' && leads.has(id)) {
        const lead = leads.get(id);
        if (req.method === 'GET' && !action) return send(res, 200, lead);
        if (req.method === 'POST' && action === 'call') return send(res, 201, await callLead(lead));
      }

      if (req.method === 'POST' && url.pathname.startsWith('/webhooks/tryvox')) {
        const raw = await readBody(req);
        if (!isFromTryVox(req.headers['x-tryvox-signature'], raw)) return send(res, 401, { error: 'bad signature' });
        if (url.pathname === '/webhooks/tryvox/answer') return send(res, 200, answerVoxml());
        recordEvent(JSON.parse(raw.toString()));
        return send(res, 200, { ok: true });
      }

      send(res, 404, { error: 'not found' });
    } catch (err) {
      console.error(err);
      send(res, err.status ?? 500, { error: err.message, code: err.code });
    }
  })
  .listen(PORT, () => console.log(`Open http://localhost:${PORT}`));
npm run crm      # then open http://localhost:3000 and click Call

What happens

  1. Call → the page posts to /api/leads/lead-1042/call on your backend. The browser never sees TryVox credentials.
  2. Your backend places the call and gets back a request_uuid. It stores request_uuid → lead_id. That pairing is how every later event finds the lead.
  3. When the lead answers, TryVox fetches answer_url, and the Dial verb rings AGENT_PHONE to connect your user.
  4. Each webhook carries CallUUID (the same request_uuid). recordEvent() looks up the lead and updates the call: ringing → answered → completed, then Billsec as the duration.

Production note

  • Normal Voice API calls have no reference field of yours. There is no client_ref, so the request_uuid → lead_id mapping must be saved in your database when you place the call. Save it before you return, because webhooks can arrive seconds later.
  • Ignore events for unknown CallUUIDs, and write the Hangup result idempotently. It can arrive twice.
  • Keep webhook_secret set on every call. Without it, webhooks are unsigned.

On this page