Skip to main content

New Node SDK

Client Initialization

const username = process.env.BW_USERNAME;
const password = process.env.BW_PASSWORD;

const config = new Configuration({
username,
password
});

const callsApi = new CallsApi(config);
const conferencesApi = new ConferencesApi(config);
// etc... for the other included APIs

Messaging

Create Message

const messageRequest = {
to: [USER_NUMBER],
from: BW_NUMBER,
text: 'text',
applicationId: BW_MESSAGING_APPLICATION_ID,
tag: 'tag',
priority: 'high';,
expiration: new Date(Date.now() + 60).toISOString()
};

const { status, data } = await messagesApi.createMessage(BW_ACCOUNT_ID, messageRequest);

List Messages

const { status, data } = await messagesApi.listMessages(
BW_ACCOUNT_ID,
undefined,
BW_NUMBER,
undefined,
undefined,
'OUBOUND'
);

Media

List Media

const { status, data } = await mediaApi.listMedia(BW_ACCOUNT_ID);

Get Media

const mediaFileName = 'nodejs_media_file';
const { status, data } = await mediaApi.getMedia(BW_ACCOUNT_ID, mediaFileName);
fs.writeFile('./tests/fixtures/test.jpeg', Buffer.from(data), (err) => {
if (err) throw err;
});

Upload Media

const mediaFileName = 'nodejs_media_file';
const mediaFileData = fs.readFileSync('./nodejs.jpeg');
const { status } = await mediaApi.uploadMedia(
BW_ACCOUNT_ID,
mediaFileName,
mediaFileData
);

Delete Media

const mediaFileName = 'nodejs_media_file';
const { status } = await mediaApi.deleteMedia(BW_ACCOUNT_ID, mediaFileName);

BXML

Imports

import { Bxml } from 'bandwidth-sdk';

Bridge

const attributes = {
bridgeCompleteUrl: 'https://initial.com',
bridgeCompleteMethod: 'POST',
bridgeCompleteFallbackUrl: 'https://initial.com',
bridgeCompleteFallbackMethod: 'POST',
bridgeTargetCompleteUrl: 'https://initial.com',
bridgeTargetCompleteMethod: 'POST',
bridgeTargetCompleteFallbackUrl: 'https://initial.com',
bridgeTargetCompleteFallbackMethod: 'POST',
username: 'initialUsername',
password: 'initialPassword',
fallbackUsername: 'initialFallbackUsername',
fallbackPassword: 'initialFallbackPassword',
tag: 'initialTag'
};

const bridge = new Bxml.Bridge(
'c-95ac8d6e-1a31c52e-b38f-4198-93c1-51633ec68f8d',
attributes
);
const response = new Bxml.Response(bridge);
response.toBxml();

Conference

const attributes = {
mute: true,
hold: true,
callIdsToCoach: 'initial',
conferenceEventUrl: 'https://initial.com',
conferenceEventMethod: 'POST',
conferenceEventFallbackUrl: 'https://initial.com',
conferenceEventFallbackMethod: 'POST',
username: 'initialUsername',
password: 'initialPassword',
fallbackUsername: 'initialFallbackUsername',
fallbackPassword: 'initialFallbackPassword',
tag: 'initialTag',
callbackTimeout: 5.1
};

const conference = new Bxml.Conference('conferenceName', attributes);
const response = new Bxml.Response(conference);
response.toBxml();

Forward

const attributes = {
to: '+19195551234',
from: '+19195554321',
callTimeout: 5,
diversionTreatment: 'propagate',
diversionReason: 'user-busy',
uui: '93d6f3c0be5845960b744fa28015d8ede84bd1a4;encoding=base64,asdf;encoding=jwt'
};

const forward = new Bxml.Forward(attributes);
const response = new Bxml.Response(forward);
response.toBxml();

Gather

const attributes = {
gatherUrl: 'https://initial.com',
gatherMethod: 'POST',
gatherFallbackUrl: 'https://initial.com',
gatherFallbackMethod: 'POST',
username: 'initialUsername',
password: 'initialPassword',
fallbackUsername: 'initialFallbackUsername',
fallbackPassword: 'initialFallbackPassword',
tag: 'initialTag',
terminatingDigits: '5',
maxDigits: 5,
interDigitTimeout: 5,
firstDigitTimeout: 5,
repeatCount: 5
};
const playAudio = new Bxml.PlayAudio('https://audio.url/audio1.wav');
const speakSentence = new Bxml.SpeakSentence(
'<lang xml:lang="es-MX">Hola</lang>nodejs speak sentence <emphasis>SSML test</emphasis>'
);

let gather = new Gather(attributes, playAudio);
gather.addAudioVerbs(speakSentence);
const response = new Bxml.Response(gather);
response.toBxml();

Hangup

const hangup = new Bxml.Hangup();
const response = new Bxml.Response(hangup);
response.toBxml();

Pause

const attributes = {
duration: 5
};

const pause = new Bxml.Pause(attributes);
const response = new Bxml.Response(pause);
response.toBxml();

Pause Recording

const pauseRecording = new Bxml.PauseRecording();
const response = new Bxml.Response(pauseRecording);
response.toBxml();

Play Audio

const attributes = {
username: 'initialUsername',
password: 'initialPassword'
};

const playAudio = new Bxml.PlayAudio(
'https://audio.url/audio1.wav',
attributes
);
const response = new Bxml.Response(playAudio);
response.toBxml();

Record

const attributes = {
recordCompleteUrl: 'https://initial.com',
recordCompleteMethod: 'POST',
recordCompleteFallbackUrl: 'https://initial.com',
recordCompleteFallbackMethod: 'POST',
recordingAvailableUrl: 'https://initial.com',
recordingAvailableMethod: 'POST',
transcribe: true,
transcriptionAvailableUrl: 'https://initial.com',
transcriptionAvailableMethod: 'POST',
username: 'initialUsername',
password: 'initialPassword',
fallbackUsername: 'initialFallbackUsername',
fallbackPassword: 'initialFallbackPassword',
tag: 'initialTag',
terminatingDigits: '5',
maxDuration: 5,
silenceTimeout: 5,
fileFormat: 'wav'
};

const record = new Bxml.Record(attributes);
const response = new Bxml.Response(record);
response.toBxml();

Redirect

const attributes = {
redirectUrl: 'https://initial.com',
redirectMethod: 'POST',
redirectFallbackUrl: 'https://initial.com',
redirectFallbackMethod: 'POST',
username: 'initialUsername',
password: 'initialPassword',
fallbackUsername: 'initialFallbackUsername',
fallbackPassword: 'initialFallbackPassword',
tag: 'initialTag'
};

const redirect = new Bxml.Redirect(attributes);
const response = new Bxml.Response(redirect);
response.toBxml();

Resume Recording

const resumeRecording = new Bxml.ResumeRecording();
const response = new Bxml.Response(resumeRecording);
response.toBxml();

Ring

const attributes = {
duration: 5.1,
answerCall: true
};

const ring = new Bxml.Ring(attributes);
const response = new Bxml.Response(ring);
response.toBxml();

Send DTMF

const attributes = {
toneDuration: 5,
toneInterval: 5
};

const sendDtmf = new Bxml.SendDtmf('1234', attributes);
const response = new Bxml.Response(sendDtmf);
response.toBxml();

Speak Sentence

const attributes = {
voice: 'julie',
gender: 'female',
locale: 'en_US'
};

const speakSentence = new Bxml.SpeakSentence(
'<lang xml:lang="es-MX">Hola</lang>nodejs speak sentence <emphasis>SSML test</emphasis>',
attributes
);
const response = new Bxml.Response(speakSentence);
response.toBxml();

Start Gather

const attributes = {
dtmfUrl: 'https://initial.com',
dtmfMethod: 'POST',
username: 'initialUsername',
password: 'initialPassword',
tag: 'initialTag'
};

const startGather = new Bxml.StartGather(attributes);
const response = new Bxml.Response(startGather);
response.toBxml();

Start Recording

const attributes = {
recordingAvailableUrl: 'https://initial.com',
recordingAvailableMethod: 'POST',
transcribe: true,
transcriptionAvailableUrl: 'https://initial.com',
transcriptionAvailableMethod: 'POST',
username: 'initialUsername',
password: 'initialPassword',
tag: 'initialTag',
fileFormat: 'wav',
multiChannel: true
};

const startRecording = new Bxml.StartRecording(attributes);
const response = new Bxml.Response(startRecording);
response.toBxml();

Start Stream

const attributes = {
name: 'initialName',
tracks: 'inbound',
destination: 'https://initial.com',
streamEventUrl: 'https://initial.com',
streamEventMethod: 'POST',
username: 'initialUsername',
password: 'initialPassword'
};
const streamParam1 = new Bxml.StreamParam({
name: 'streamParamName1',
value: 'streamParamValue1'
});
const streamParam2 = new Bxml.StreamParam({
name: 'streamParamName2',
value: 'streamParamValue2'
});

let startStream = new StartStream(attributes, streamParam1);
startStream.addStreamParams(streamParam2);
const response = new Bxml.Response(startStream);
response.toBxml();

Start Transcription

const attributes = {
name: 'initialName',
tracks: 'inbound',
transcriptionEventUrl: 'https://initial.com',
transcriptionEventMethod: 'POST',
username: 'initialUsername',
password: 'initialPassword',
destination: 'https://initial.com',
stabilized: true
};

const customParam1 = new Bxml.CustomParam({
name: 'customParamName1',
value: 'customParamValue1'
});
const customParam2 = new Bxml.CustomParam({
name: 'customParamName2',
value: 'customParamValue2'
});

let startTranscription = new StartTranscription(attributes, customParam1);
startTranscription.addCustomParams(customParam2);
const response = new Bxml.Response(startTranscription);
response.toBxml();

Stop Gather

const stopGather = new Bxml.StopGather();
const response = new Bxml.Response(stopGather);
response.toBxml();

Stop Recording

const stopRecording = new Bxml.StopRecording();
const response = new Bxml.Response(stopRecording);
response.toBxml();

Stop Stream

const attributes = {
name: 'initialName'
};

const stopStream = new Bxml.StopStream(attributes);
const response = new Bxml.Response(stopStream);
response.toBxml();

Stop Transcription

const attributes = {
name: 'initialName'
};

const stopTranscription = new Bxml.StopTranscription(attributes);
const response = new Bxml.Response(stopTranscription);
response.toBxml();

Tag

const tag = new Bxml.Tag('testTag');
const response = new Bxml.Response(tag);
response.toBxml();

Transfer

const attributes = {
transferCallerId: '+19195551234',
callTimeout: 5,
transferCompleteUrl: 'https://initial.com',
transferCompleteMethod: 'POST',
transferCompleteFallbackUrl: 'https://initial.com',
transferCompleteFallbackMethod: 'POST',
username: 'initialUsername',
password: 'initialPassword',
fallbackUsername: 'initialFallbackUsername',
fallbackPassword: 'initialFallbackPassword',
tag: 'initialTag',
diversionTreatment: 'propagate',
diversionReason: 'user-busy'
};
const phoneNumber = new Bxml.PhoneNumber('+19195551234');
const sipUri = new Bxml.SipUri('sip:1-999-123-4567@voip-provider.example.net');

let transfer = new Transfer(attributes, phoneNumber);
transfer.addTransferRecipients(sipUri);
const response = new Bxml.Response(transfer);
response.toBxml();

Calls

Create Call

const amdConfig = {
mode: 'async',
detectionTimeout: 5.0,
silenceTimeout: 5.0,
speechThreshold: 5.0,
speechendThreshold: 5.0,
delayResult: true,
callbackUrl: BASE_CALLBACK_URL + '/machineDetection',
callbackMethod: CallbackMethodEnum.Post
};

const callBody = {
applicationId: BW_VOICE_APPLICATION_ID,
to: USER_NUMBER,
from: BW_NUMBER,
displayName: 'NodeJS SDK',
answerUrl: `${BASE_CALLBACK_URL}/callbacks/answer`,
answerMethod: CallbackMethodEnum.Post,
disconnectUrl: `${BASE_CALLBACK_URL}/callbacks/disconnect`,
disconnectMethod: CallbackMethodEnum.Get,
machineDetection: amdConfig,
callTimeout: 30.0,
callbackTimeout: 15.0
};

const { status, data } = await callsApi.createCall(BW_ACCOUNT_ID, callBody);

Get Calls

const { status, data } = await callsApi.listCalls(BW_ACCOUNT_ID);

Get Call Information

const callId = 'c-abc12345-6defabc1-2345-6def-abc1-23456defabc1';
const { status, data } = await callsApi.getCallState(BW_ACCOUNT_ID, callId);

Update Call

const callId = 'c-abc12345-6defabc1-2345-6def-abc1-23456defabc1';
const updateCallBody = {
state: CallStateEnum.Active,
redirectUrl: `${BASE_CALLBACK_URL}/bxml/pause`
};

const { status: updateStatus } = await callsApi.updateCall(
BW_ACCOUNT_ID,
callId,
updateCallBody
);

Replace Call BXML

const callId = 'c-abc12345-6defabc1-2345-6def-abc1-23456defabc1';
const updateBxml =
'<?xml version="1.0" encoding="UTF-8"?><Bxml><SpeakSentence locale="en_US" gender="female" voice="susan">This is a test bxml response</SpeakSentence><Pause duration="3"/></Bxml>';

const { status: updateStatus } = await callsApi.updateCallBxml(
BW_ACCOUNT_ID,
callId,
updateBxml
);

Conferences

List Conferences

const { status, data } = await conferencesApi.listConferences(BW_ACCOUNT_ID);

Get Conference Information

const conferenceId = 'conf-abc12345-6defabc1-2345-6def-abc1-23456defabc1';
const { status, data } = await conferencesApi.getConference(
BW_ACCOUNT_ID,
conferenceId
);

Update Conference

const conferenceId = 'conf-abc12345-6defabc1-2345-6def-abc1-23456defabc1';
const updateConferenceBody = {
status: ConferenceStateEnum.Active,
redirectUrl: `${BASE_CALLBACK_URL}/bxml/pause`,
redirectMethod: RedirectMethodEnum.Post,
username: 'username',
password: 'password',
redirectFallback_url: `${BASE_CALLBACK_URL}/bxml/pause`,
redirectFallback_method: RedirectMethodEnum.Post,
fallbackUsername: 'username',
fallbackPassword: 'password'
};

const { status } = await conferencesApi.updateConference(
BW_ACCOUNT_ID,
conferenceId,
updateConferenceBody
);

Update Conference BXML

const conferenceId = 'conf-abc12345-6defabc1-2345-6def-abc1-23456defabc1';
const updateBxml =
'<?xml version="1.0" encoding="UTF-8"?><Bxml><StartRecording/><SpeakSentence locale="en_US" gender="female" voice="susan">This should be a conference recording.</SpeakSentence><StopRecording/></Bxml>';

const { status } = await conferencesApi.updateConferenceBxml(
BW_ACCOUNT_ID,
conferenceId,
updateBxml
);

Get Conference Member

const conferenceId = 'conf-abc12345-6defabc1-2345-6def-abc1-23456defabc1';
const memberId = 'c-abc12345-6defabc1-2345-6def-abc1-23456defabc1';
const { status, data } = await conferencesApi.getConferenceMember(
BW_ACCOUNT_ID,
conferenceId,
memberId
);

Update Conference Member

const conferenceId = 'conf-abc12345-6defabc1-2345-6def-abc1-23456defabc1';
const memberId = 'c-abc12345-6defabc1-2345-6def-abc1-23456defabc1';
const updateConferenceMember = { mute: false };

const { status } = await conferencesApi.updateConferenceMember(
BW_ACCOUNT_ID,
conferenceId,
memberId,
updateConferenceMember
);

List Conference Recordings

const conferenceId = 'c-abc12345-6defabc1-2345-6def-abc1-23456defabc1';
const { status, data } = await conferencesApi.listConferenceRecordings(
BW_ACCOUNT_ID,
conferenceId
);

Get Conference Recording Information

const conferenceId = 'conf-abc12345-6defabc1-2345-6def-abc1-23456defabc1';
const recordingId = 'r-abc12345-6def-abc1-2345-6defabc12345';
const { status, data } = await conferencesApi.getConferenceRecording(
BW_ACCOUNT_ID,
conferenceId,
recordingId
);

Download Conference Recording

const conferenceId = 'conf-95ac8d8d-28e06798-2afe-434c-b0f4-666a79cd47f8';
const recordingId = 'r-abc12345-6def-abc1-2345-6defabc12345';
const { status, data } = await conferencesApi.downloadConferenceRecording(
BW_ACCOUNT_ID,
conferenceId,
recordingId
);

Recordings

List Account Call Recordings

const { status, data } =
await recordingsApi.listAccountCallRecordings(BW_ACCOUNT_ID);

Update Recording

const callId = 'c-abc12345-6defabc1-2345-6def-abc1-23456defabc1';
const pauseRecording = { state: RecordingStateEnum.Paused };
const { status } = await recordingsApi.updateCallRecordingState(
BW_ACCOUNT_ID,
callId,
pauseRecording
);

List Call Recordings

const callId = 'c-abc12345-6defabc1-2345-6def-abc1-23456defabc1';
const { status, data } = await recordingsApi.listCallRecordings(
BW_ACCOUNT_ID,
callId
);

Get Call Recording

const callId = 'c-abc12345-6defabc1-2345-6def-abc1-23456defabc1';
const recordingId = 'r-abc12345-6def-abc1-2345-6defabc12345';
const { status, data } = await recordingsApi.getCallRecording(
BW_ACCOUNT_ID,
callId,
recordingId
);

Delete Recording

const callId = 'c-abc12345-6defabc1-2345-6def-abc1-23456defabc1';
const recordingId = 'r-abc12345-6def-abc1-2345-6defabc12345';
const { status } = await recordingsApi.deleteRecording(
BW_ACCOUNT_ID,
callId,
recordingId
);

Download Recording

const callId = 'c-abc12345-6defabc1-2345-6def-abc1-23456defabc1';
const recordingId = 'r-abc12345-6def-abc1-2345-6defabc12345';
const { status, data } = await recordingsApi.downloadCallRecording(
BW_ACCOUNT_ID,
callId,
recordingId
);

Delete Recording Media

const callId = 'c-abc12345-6defabc1-2345-6def-abc1-23456defabc1';
const recordingId = 'r-abc12345-6def-abc1-2345-6defabc12345';
const { status } = await recordingsApi.deleteRecordingMedia(
BW_ACCOUNT_ID,
callId,
recordingId
);

Get Transcription

const callId = 'c-abc12345-6defabc1-2345-6def-abc1-23456defabc1';
const recordingId = 'r-abc12345-6def-abc1-2345-6defabc12345';
const { status, data } = await recordingsApi.getCallTranscription(
BW_ACCOUNT_ID,
callId,
recordingId
);

Create Transcription Request

const callId = 'c-abc12345-6defabc1-2345-6def-abc1-23456defabc1';
const recordingId = 'r-abc12345-6def-abc1-2345-6defabc12345';
const transcribeRecording = {
callbackUrl: `${BASE_CALLBACK_URL}/transcriptions`,
tag: callId
};
const { status } = await recordingsApi.transcribeCallRecording(
BW_ACCOUNT_ID,
callId,
recordingId,
transcribeRecording
);

Delete Transcription

const callId = 'c-abc12345-6defabc1-2345-6def-abc1-23456defabc1';
const recordingId = 'r-abc12345-6def-abc1-2345-6defabc12345';
const { status } = await recordingsApi.deleteCallTranscription(
BW_ACCOUNT_ID,
callId,
recordingId
);

Telephone Number Lookup

Create Lookup

const lookupRequest = {
tns: [BW_NUMBER]
};

const { status, data } = await phoneNumberLookupApi.createLookup(
BW_ACCOUNT_ID,
lookupRequest
);

Get Lookup Request Status

const lookupRequestId = '004223a0-8b17-41b1-bf81-20732adf5590';
const { status, data } = await phoneNumberLookupApi.getLookupStatus(
BW_ACCOUNT_ID,
lookupRequestId
);

Multi-Factor Authentication

Voice MFA Code

const codeRequest = {
to: USER_NUMBER,
from: BW_NUMBER,
applicationId: BW_VOICE_APPLICATION_ID,
message: 'Your temporary {NAME} {SCOPE} code is {CODE}',
digits: 6
};

const { status, data } = await mfaApi.generateVoiceCode(
BW_ACCOUNT_ID,
codeRequest
);

Messaging MFA Code

const codeRequest = {
to: USER_NUMBER,
from: BW_NUMBER,
applicationId: BW_MESSAGING_APPLICATION_ID,
message: 'Your temporary {NAME} {SCOPE} code is {CODE}',
digits: 6
};

const { status, data } = await mfaApi.generateVoiceCode(
BW_ACCOUNT_ID,
codeRequest
);

Verify MFA Code

const verifyRequest = {
to: USER_NUMBER,
scope: '2FA',
expirationTimeInMinutes: 3,
code: '12345'
};

const { status, data } = await mfaApi.verifyCode(BW_ACCOUNT_ID, verifyRequest);