Back to Blog
Language How-Tos

How to Forward Calls to Another Number in Twilio Using Node.js

Create a Node.js call forwarding webhook that uses TwiML Dial to route inbound Twilio calls based on time of day or other logic.

DA
Danial A
Senior Twilio Consultant, Telphi Consulting
June 22, 2026
8 min read
Twilio
Node.js
JavaScript
How-To
Code
How to Forward Calls to Another Number in Twilio Using Node.js

This tutorial builds a call forwarding system in Node.js using Express and Twilio TwiML that routes inbound calls to different phone numbers based on business hours, day of week, or other configurable rules. It targets developers who need intelligent call routing without expensive PBX hardware, such as forwarding to a mobile number after hours or distributing calls among a small team. You will build a webhook that checks the current time and returns a TwiML Dial verb pointing to the appropriate forwarding destination.

Prerequisites and Setup

Install the packages with npm install twilio express dotenv and configure your Twilio number's voice webhook in the Console to POST to your Express route. Define your forwarding numbers and business hours as constants at the top of the module so they can be changed without touching the routing logic. The business hours check uses JavaScript's Date object with a UTC offset to approximate the correct local time; for precise timezone handling, install the luxon package from npm. Set up ngrok for local testing and make note of the HTTPS URL to paste into the Twilio Console webhook field.

npm install twilio express dotenv

# .env
TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
TWILIO_AUTH_TOKEN=your_auth_token_here
TWILIO_PHONE_NUMBER=+15551234567
OFFICE_NUMBER=+15550001111
MOBILE_NUMBER=+15550002222

The Complete Code

The isBusinessHours() helper converts the current UTC time to Eastern time by subtracting a fixed offset and checks the hour and weekday against configured thresholds. During business hours the Dial verb targets the office number; outside hours it targets the mobile number with a brief after-hours notice. The action attribute on the Dial verb points to a /no-answer route that handles the case where the forwarded call goes unanswered. The /no-answer route checks req.body.DialCallStatus and plays a voicemail prompt if the call was not answered.

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

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

const FROM   = process.env.TWILIO_PHONE_NUMBER;
const OFFICE = process.env.OFFICE_NUMBER;
const MOBILE = process.env.MOBILE_NUMBER;

function isBusinessHours() {
  const now  = new Date();
  const etH  = (now.getUTCHours() - 4 + 24) % 24; // rough ET offset
  const day  = now.getUTCDay();                     // 0 = Sun, 6 = Sat
  return day >= 1 && day <= 5 && etH >= 9 && etH < 18;
}

app.post('/forward', (req, res) => {
  const twiml = new twilio.twiml.VoiceResponse();

  if (isBusinessHours()) {
    const dial = twiml.dial({ timeout: 20, action: '/no-answer', callerId: FROM });
    dial.number(OFFICE);
  } else {
    twiml.say({ voice: 'alice' },
      'Our office is closed. Forwarding you to our after-hours line.'
    );
    const dial = twiml.dial({ timeout: 30, action: '/no-answer', callerId: FROM });
    dial.number(MOBILE);
  }

  res.type('text/xml');
  res.send(twiml.toString());
});

app.post('/no-answer', (req, res) => {
  const dialStatus = req.body.DialCallStatus || '';
  const twiml = new twilio.twiml.VoiceResponse();

  if (['no-answer', 'busy', 'failed'].includes(dialStatus)) {
    twiml.say({ voice: 'alice' },
      'We are unable to take your call right now. ' +
      'Please leave a message after the tone.'
    );
    twiml.record({ timeout: 10, transcribe: true, transcribeCallback: '/transcription' });
  }

  twiml.hangup();
  res.type('text/xml');
  res.send(twiml.toString());
});

app.post('/transcription', (req, res) => {
  console.log('Voicemail transcript:', req.body.TranscriptionText);
  res.sendStatus(204);
});

app.listen(3000, () => console.log('Forwarding server on port 3000'));

Testing and Debugging

Call your Twilio number during business hours and verify the call forwards to the office number; call outside business hours and verify it routes to the mobile number. Temporarily adjust the etH >= 9 && etH < 18 condition to include or exclude the current UTC hour to test each branch without waiting for the actual time to change. Test the no-answer fallback by having the forwarding destination reject the call and verifying the voicemail prompt plays and the recording callback fires. Check the Twilio Console Call Logs if routing does not match your expectations to see exactly which TwiML was fetched and which route handled each request.

Taking It Further

Replace the fixed UTC offset with the luxon package for accurate timezone-aware business hours comparisons that handle daylight saving time automatically. Add a holiday schedule stored in a database and check it before the business hours logic so calls on public holidays route to mobile or voicemail even during normal working hours. Implement a round-robin or simultaneous-ring pattern by placing multiple number elements inside the Dial verb so several team members are called at once and the first to answer gets the call. Combine this forwarding logic with the IVR tutorial to let callers choose their department before being forwarded to the relevant team member.

Contact Telphi Consulting to design an intelligent call routing system with CRM integration and real-time analytics for your Node.js application.

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.