Guide
How to use Twilio Voice in Capacitor
Using @capgo/capacitor-twilio-voice
Integrates the Twilio Voice SDK into Capacitor.
Install
bun add @capgo/capacitor-twilio-voice
bunx cap sync
bunx cap sync copies the plugin's native code into your native projects. Run it again after every plugin upgrade.
Import
import { CapacitorTwilioVoice } from '@capgo/capacitor-twilio-voice';
API at a glance
| Method | Description |
|---|---|
login |
Authenticate the user with Twilio Voice using an access token. |
logout |
Log out the current user and unregister from Twilio Voice. |
isLoggedIn |
Check if the user is currently logged in and has a valid access token. |
makeCall |
Initiate an outgoing call to a phone number or client. |
acceptCall |
Accept an incoming call. |
rejectCall |
Reject an incoming call. |
endCall |
End an active call. |
muteCall |
Mute or unmute the microphone during an active call. |
setSpeaker |
Enable or disable speakerphone mode. |
getCallStatus |
Get the current status of the active call. |
checkMicrophonePermission |
Check if microphone permission has been granted. |
requestMicrophonePermission |
Request microphone permission from the user. |
Examples
login()
Authenticate the user with Twilio Voice using an access token.
import { CapacitorTwilioVoice } from '@capgo/capacitor-twilio-voice';
const result = await CapacitorTwilioVoice.login({
accessToken: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...'
});
console.log('Login successful:', result.success);
logout()
Log out the current user and unregister from Twilio Voice.
import { CapacitorTwilioVoice } from '@capgo/capacitor-twilio-voice';
const result = await CapacitorTwilioVoice.logout();
console.log('Logout successful:', result.success);
isLoggedIn()
Check if the user is currently logged in and has a valid access token.
import { CapacitorTwilioVoice } from '@capgo/capacitor-twilio-voice';
const status = await CapacitorTwilioVoice.isLoggedIn();
if (status.isLoggedIn && status.hasValidToken) {
console.log('User identity:', status.identity);
} else {
// Re-authenticate the user
}
makeCall()
Initiate an outgoing call to a phone number or client.
import { CapacitorTwilioVoice } from '@capgo/capacitor-twilio-voice';
// Call a phone number
const result = await CapacitorTwilioVoice.makeCall({
to: '+1234567890'
});
console.log('Call SID:', result.callSid);
// Call another Twilio client with a readable name for CallKit Recents
await CapacitorTwilioVoice.makeCall({
to: 'client:alice',
displayName: 'Alice Smith'
});
// Call a PSTN number using a specific caller ID
await CapacitorTwilioVoice.makeCall({
to: '+1234567890',
callerId: '+10987654321'
});
acceptCall()
Accept an incoming call.
import { CapacitorTwilioVoice } from '@capgo/capacitor-twilio-voice';
CapacitorTwilioVoice.addListener('callInviteReceived', async (data) => {
console.log('Incoming call from:', data.from);
const result = await CapacitorTwilioVoice.acceptCall({
callSid: data.callSid
});
console.log('Call accepted:', result.success);
});
rejectCall()
Reject an incoming call.
import { CapacitorTwilioVoice } from '@capgo/capacitor-twilio-voice';
CapacitorTwilioVoice.addListener('callInviteReceived', async (data) => {
if (shouldRejectCall(data.from)) {
await CapacitorTwilioVoice.rejectCall({
callSid: data.callSid
});
}
});
The table above lists the 12 core methods. Listener and version helpers, and the full contract of each method, are documented in the GitHub repository.
Listen to events
addListener returns a handle. Call handle.remove() when the screen unmounts, or CapacitorTwilioVoice.removeAllListeners() to clear every listener.
Full reference
Keep going from Using @capgo/capacitor-twilio-voice
If you are using Using @capgo/capacitor-twilio-voice to plan native plugin work, connect it with @capgo/capacitor-twilio-voice for the implementation detail in @capgo/capacitor-twilio-voice, Getting Started for the implementation detail in Getting Started, Capgo Plugin Directory for the product workflow in Capgo Plugin Directory, Capacitor Plugins by Capgo for the implementation detail in Capacitor Plugins by Capgo, and Adding or Updating Plugins for the implementation detail in Adding or Updating Plugins.