Back to Blog
Language How-Tos

How to Build a Click-to-Call Feature in Twilio Using Node.js

Build a click-to-call endpoint in Node.js that triggers a Twilio outbound call connecting a web visitor to your sales team in real time.

DA
Danial A
Senior Twilio Consultant, Telphi Consulting
June 22, 2026
8 min read
Twilio
Node.js
JavaScript
How-To
Code
How to Build a Click-to-Call Feature in Twilio Using Node.js

This tutorial builds a click-to-call feature using Express.js and Twilio in Node.js that accepts a visitor's phone number from a web form, dials them back, and connects them to a support or sales agent. It targets developers who want a "Call Me Now" button that reduces friction compared to a contact form by putting the visitor on a live call within seconds. You will build an Express POST endpoint that initiates the Twilio call and a TwiML route that bridges the visitor to your agent once the call connects.

Prerequisites and Setup

Install the packages with npm install twilio express dotenv and store your Twilio credentials alongside the agent's phone number in a .env file. The click-to-call flow works in two steps: first Twilio dials the visitor's number, and once they answer Twilio fetches TwiML from your /connect-agent route and dials the agent, bridging both parties. The visitor's number comes from the form submission so it must be validated as E.164 format on the server side before being passed to the API. Expose your server with npx ngrok http 3000 during development since the TwiML URL must be publicly accessible.

npm install twilio express dotenv

# .env
TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
TWILIO_AUTH_TOKEN=your_auth_token_here
TWILIO_PHONE_NUMBER=+15551234567
AGENT_PHONE_NUMBER=+15559998888

The Complete Code

The /call endpoint validates the submitted phone number, calls client.calls.create() with the visitor as to and a url pointing to the /connect-agent route, and returns the call SID as a JSON response to the browser. The /connect-agent route returns a Dial TwiML verb that instructs Twilio to call the agent's number once the visitor answers. The callerId attribute on the Dial verb ensures the agent sees your Twilio number on their screen rather than the visitor's number, keeping your team's personal numbers private. A brief Say prompt before the Dial gives the agent a moment to prepare before being connected.

require('dotenv').config();
const express = require('express');
const twilio  = require('twilio');

const app    = express();
app.use(express.urlencoded({ extended: false }));
app.use(express.json());

const client = twilio(
  process.env.TWILIO_ACCOUNT_SID,
  process.env.TWILIO_AUTH_TOKEN
);
const FROM  = process.env.TWILIO_PHONE_NUMBER;
const AGENT = process.env.AGENT_PHONE_NUMBER;
const E164  = /^+[1-9]d{7,14}$/;

app.get('/', (req, res) => {
  res.send(
    '<form method="POST" action="/call">' +
    '<input name="phone" placeholder="+15551234567" required />' +
    '<button>Call Me Now</button></form>'
  );
});

app.post('/call', async (req, res) => {
  const visitorPhone = (req.body.phone || '').trim();
  if (!E164.test(visitorPhone)) {
    return res.status(400).json({ error: 'Invalid phone number format. Use E.164.' });
  }
  try {
    const call = await client.calls.create({
      to:   visitorPhone,
      from: FROM,
      url:  req.protocol + '://' + req.get('host') + '/connect-agent',
    });
    res.json({ message: 'Calling you now!', sid: call.sid });
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
});

app.post('/connect-agent', (req, res) => {
  const twiml = new twilio.twiml.VoiceResponse();
  twiml.say({ voice: 'alice' }, 'Please hold while we connect you to a specialist.');
  const dial = twiml.dial({ callerId: FROM });
  dial.number(AGENT);
  res.type('text/xml');
  res.send(twiml.toString());
});

app.listen(3000, () => console.log('Click-to-call server on port 3000'));

Testing and Debugging

Submit the form with your own mobile number and wait for Twilio to call you within a few seconds. When you answer, you should hear the brief hold message and then be connected to the agent number you configured. If the visitor's phone rings but the agent side is never called, verify the ngrok URL resolves correctly to /connect-agent and that Express is returning valid TwiML with a 200 status. Error code 13224 in the Twilio Console means Twilio could not fetch the TwiML from /connect-agent; confirm your server is running and the route is registered before the call is initiated.

Taking It Further

Add agent availability logic that checks a schedule or database before creating the call and plays a voicemail TwiML if no agents are available instead of dialling an unanswered number. Implement a call queue using Twilio TaskRouter so multiple simultaneous click-to-call requests are handled in order when all agents are busy. Log each click-to-call event to your CRM with the visitor's phone, page URL from the Referer header, and resulting call SID for lead attribution and conversion tracking. Add a pre-connection whisper using a Twilio Conference so the agent hears a brief context note about which page or product the visitor was on before they are connected.

Contact Telphi Consulting to implement a production click-to-call solution with agent routing, queueing, and CRM integration in Node.js.

Share this article:
0 views

Ready to Transform Your Business Communications?

Get a free consultation with our VoIP experts and discover how we can help you save costs, improve efficiency, and scale your business.

Comments (0)

Join the discussion and share your thoughts (AI-moderated for quality)

Protected by AI moderation

Be the first to comment

No comments yet. Share your thoughts below.