Calling using twilio API from the browser. Useful for testing when developing phone calling based software. Uses @twilio/voice-sdk
.
In twilio create a new number and an app, e.g. twilio-caller
and link it to the number. Also create an API key.
Then create a Function in Twilio with path /twilio-caller/handle-call
and set the app's Webhook (Request URL) to the full URL of the function. This function will simply forward the call to @twilio/voice-sdk
in the browser:
const assert = require('node:assert');
exports.handler = function(context, event, callback) {
const callerId = '+123456789'; // Update with your twilio number
// Only handle outgoing calls
assert(event.To !== callerId.replaceAll(/[^\d+]/g, ''));
const twiml = new Twilio.twiml.VoiceResponse();
// set the callerId (from)
let dial = twiml.dial({ callerId });
dial.number(event.To);
callback(null, twiml);
};
Ref:
- https://www.twilio.com/docs/serverless/functions-assets/functions/invocation
- https://www.twilio.com/docs/voice/twiml/dial
Make sure you have Node.js. Now check out this repo and run yarn
. Make a .env
file with data from Twilio admin:
TWILIO_ACCOUNT_SID
TWIML_APP_SID
TWILIO_API_KEY
TWILIO_API_SECRET
Next generate a token:
node --experimental-strip-types makeAccessToken.ts
yarn dev