Skip to main content

v10 -> v11

Client Initialization

require 'bandwidth-sdk'

Bandwidth.configure do |config|
config.username = 'YOUR_USERNAME'
config.password = 'YOUR_PASSWORD'
end
account_id = '9900000'

Custom URLs

# Currently Unsupported

Messaging

Create Message

api_instance = Bandwidth::MessagesApi.new
account_id = '9900000'
message_request = Bandwidth::MessageRequest.new({application_id: '93de2206-9669-4e07-948d-329f4b722ee2', to: ["+15554443333", "+15552223333"], from: '+15551113333'})

begin
result = api_instance.create_message(account_id, message_request)
p result
rescue Bandwidth::ApiError => e
puts "Error when calling MessagesApi->create_message: #{e}"
end

List Messages

api_instance = Bandwidth::MessagesApi.new
account_id = '9900000'
opts = {
message_id: '9e0df4ca-b18d-40d7-a59f-82fcdf5ae8e6',
source_tn: '+15554443333',
destination_tn: '+15554443333',
message_status: Bandwidth::MessageStatusEnum::RECEIVED,
message_direction: Bandwidth::ListMessageDirectionEnum::INBOUND,
carrier_name: 'Verizon',
message_type: Bandwidth::MessageTypeEnum::SMS,
error_code: 9902,
from_date_time: '2022-09-14T18:20:16.000Z',
to_date_time: '2022-09-14T18:20:16.000Z',
sort: 'sourceTn:desc',
page_token: 'gdEewhcJLQRB5',
limit: 50
}

begin
result = api_instance.list_messages(account_id, opts)
p result
rescue Bandwidth::ApiError => e
puts "Error when calling MessagesApi->list_messages: #{e}"
end

Media

List Media

api_instance = Bandwidth::MediaApi.new
account_id = '9900000'
opts = {
continuation_token: '1XEi2tsFtLo1JbtLwETnM1ZJ+PqAa8w6ENvC5QKvwyrCDYII663Gy5M4s40owR1tjkuWUif6qbWvFtQJR5/ipqbUnfAqL254LKNlPy6tATCzioKSuHuOqgzloDkSwRtX0LtcL2otHS69hK343m+SjdL+vlj71tT39'
}

begin
result = api_instance.list_media(account_id, opts)
p result
rescue Bandwidth::ApiError => e
puts "Error when calling MediaApi->list_media: #{e}"
end

Get Media

api_instance = Bandwidth::MediaApi.new
account_id = '9900000'
media_id = '14762070468292kw2fuqty55yp2b2/0/bw.png'

begin
result = api_instance.get_media(account_id, media_id)
p result
rescue Bandwidth::ApiError => e
puts "Error when calling MediaApi->get_media: #{e}"
end

Upload Media

api_instance = Bandwidth::MediaApi.new
account_id = '9900000'
media_id = '14762070468292kw2fuqty55yp2b2/0/bw.png'
body = File.new('/path/to/some/file')
opts = {
content_type: 'audio/wav',
cache_control: 'no-cache'
}

begin
api_instance.upload_media(account_id, media_id, body, opts)
rescue Bandwidth::ApiError => e
puts "Error when calling MediaApi->upload_media: #{e}"
end

Delete Media

api_instance = Bandwidth::MediaApi.new
account_id = '9900000'
media_id = '14762070468292kw2fuqty55yp2b2/0/bw.png'

begin
api_instance.delete_media(account_id, media_id)
rescue Bandwidth::ApiError => e
puts "Error when calling MediaApi->delete_media: #{e}"
end

BXML

Imports

require 'bandwidth-sdk'

include Bandwidth::Bxml # This include allows for Bandwidth::Bxml to be excluded from verb initialization

Bandwidth::Bxml::Hangup.new
# OR
Hangup.new # Only works if the include statement above is present

Response

hangup = Bandwidth::Bxml::Hangup.new
response = Bandwidth::Bxml::Response.new(hangup)

puts response.to_bxml()

Bridge

bridge_attributes = {
bridge_complete_url: 'https://bridge.com',
bridge_complete_method: 'POST',
bridge_complete_fallback_url: 'https://bridge.com',
bridge_complete_fallback_method: 'POST',
bridge_target_complete_url: 'https://bridge.com',
bridge_target_complete_method: 'POST',
bridge_target_complete_fallback_url: 'https://bridge.com',
bridge_target_complete_fallback_method: 'POST',
username: 'bridge_username',
password: 'bridge_password',
fallback_username: 'bridge_fallback_username',
fallback_password: 'bridge_fallback_password',
tag: 'bridge_tag'
}

bridge = Bandwidth::Bxml::Bridge.new('bridge_call_id', bridge_attributes)

Conference

conference_attributes = {
mute: true,
hold: true,
call_ids_to_coach: 'conference',
conference_event_url: 'https://conference.com',
conference_event_method: 'POST',
conference_event_fallback_url: 'https://conference.com',
conference_event_fallback_method: 'POST',
username: 'conference_username',
password: 'conference_password',
fallback_username: 'conference_fallback_username',
fallback_password: 'conference_fallback_password',
tag: 'conference_tag',
callback_timeout: 5.0
}

conference = Bandwidth::Bxml::Conference.new('conference_name', conference_attributes)

Forward

forward_attributes = {
to: '+19195551234',
from: '+19195554321',
call_timeout: 5,
diversion_treatment: 'propogate',
diversion_reason: 'user-busy',
uui: '93d6f3c0be5845960b744fa28015d8ede84bd1a4;encoding=base64,asdf;encoding=jwt'
}

forward = Bandwidth::Bxml::Forward.new(forward_attributes)

Gather

gather_attributes = {
gather_url: 'https://gather.com',
gather_method: 'POST',
gather_fallback_url: 'https://gather.com',
gather_fallback_method: 'POST',
username: 'gather_username',
password: 'gather_password',
fallback_username: 'gather_fallback_username',
fallback_password: 'gather_fallback_password',
tag: 'gather_tag',
terminating_digits: '5',
max_digits: 5,
inter_digit_timeout: 5,
first_digit_timeout: 5,
repeat_count: 5
}

play_audio = Bandwidth::Bxml::PlayAudio.new('https://audio.url/audio1.wav')
speak_sentence = Bandwidth::Bxml::SpeakSentence.new('speak this sentence')

gather = Bandwidth::Bxml::Gather.new([play_audio, speak_sentence], gather_attributes)

Hangup

hangup = Bandwidth::Bxml::Hangup.new

Pause

pause = Bandwidth::Bxml::Pause.new({duration: 5})

Pause Recording

pause_recording = Bandwidth::Bxml::PauseRecording.new

Play Audio

play_audio_attributes = {
username: 'play_audio_username',
password: 'play_audio_password'
}

play_audio = Bandwidth::Bxml::PlayAudio.new('https://audio.url/audio1.wav', play_audio_attributes)

Record

record_attributes = {
record_complete_url: 'https://record.com',
record_complete_method: 'POST',
record_complete_fallback_url: 'https://record.com',
record_complete_fallback_method: 'POST',
recording_available_url: 'https://record.com',
recording_available_method: 'POST',
transcribe: true,
transcription_available_url: 'https://record.com',
transcription_available_method: 'POST',
username: 'record_username',
password: 'record_password',
fallback_username: 'record_fallback_username',
fallback_password: 'record_fallback_password',
tag: 'record_tag',
terminating_digits: '5',
max_duration: 5,
silence_timeout: 5,
file_format: 'wav'
}

record = Bandwidth::Bxml::Record.new(record_attributes)

Redirect

redirect_attributes = {
redirect_url: 'https://redirect.com',
redirect_method: 'POST',
redirect_fallback_url: 'https://redirect.com',
redirect_fallback_method: 'POST',
username: 'redirect_username',
password: 'redirect_password',
fallback_username: 'redirect_fallback_username',
fallback_password: 'redirect_fallback_password',
tag: 'redirect_tag'
}

redirect = Bandwidth::Bxml::Redirect.new(redirect_attributes)

Resume Recording

resume_recording = Bandwidth::Bxml::ResumeRecording.new

Ring

ring_attributes = {
duration: 5.0,
answer_call: true
}

ring = Bandwidth::Bxml::Ring.new(ring_attributes)

Send DTMF

send_dtmf_attributes = {
tone_duration: 5,
tone_interval: 5
}

send_dtmf = Bandwidth::Bxml::SendDtmf.new('1234', send_dtmf_attributes)

Speak Sentence

speak_sentence_attributes = {
voice: 'julie',
gender: 'female',
locale: 'en_US'
}

speak_sentence = Bandwidth::Bxml::SpeakSentence.new('speak this sentence', speak_sentence_attributes)

Start Gather

start_gather_attributes = {
dtmf_url: 'https://start_gather.com',
dtmf_method: 'POST',
username: 'start_gather_username',
password: 'start_gather_password',
tag: 'start_gather_tag'
}

start_gather = Bandwidth::Bxml::StartGather.new(start_gather_attributes)

Start Recording

start_recording_attributes = {
recording_available_url: 'https://start_recording.com',
recording_available_method: 'POST',
transcribe: true,
transcription_available_url: 'https://start_recording.com',
transcription_available_method: 'POST',
username: 'start_recording_username',
password: 'start_recording_password',
tag: 'start_recording_tag',
file_format: 'wav',
multi_channel: true
}

start_recording = Bandwidth::Bxml::StartRecording.new(start_recording_attributes)

Start Stream

start_stream_attributes = {
name: 'start_stream_name',
tracks: 'inbound',
destination: 'https://start_stream.com',
stream_event_url: 'https://start_stream.com',
stream_event_method: 'POST',
username: 'start_stream_username',
password: 'start_stream_password'
}

stream_param = Bandwidth::Bxml::StreamParam.new({name: 'stream_param_name', value: 'stream_param_value'})

start_stream = Bandwidth::Bxml::StartStream.new([stream_param], start_stream_attributes)

Stop Gather

stop_gather = Bandwidth::Bxml::StopGather.new

Stop Recording

stop_recording = Bandwidth::Bxml::StopRecording.new

Stop Stream

stop_stream = Bandwidth::Bxml::StopStream.new({name: 'stop_stream_name'})

Tag

tag = Bandwidth::Bxml::Tag.new('tag_value')

Transfer

transfer_attributes = {
transfer_caller_id: '+19195551234',
call_timeout: 5,
transfer_complete_url: 'https://transfer.com',
transfer_complete_method: 'POST',
transfer_complete_fallback_url: 'https://transfer.com',
transfer_complete_fallback_method: 'POST',
username: 'transfer_username',
password: 'transfer_password',
fallback_username: 'transfer_fallback_username',
fallback_password: 'transfer_fallback_password',
tag: 'transfer_tag',
diversion_treatment: 'propogate',
diversion_reason: 'user-busy'
}

phone_number = Bandwidth::Bxml::PhoneNumber.new('+19195551234')
sip_uri = Bandwidth::Bxml::SipUri.new('sip:1-999-123-4567@voip-provider.example.net')

transfer = Bandwidth::Bxml::Transfer.new([phone_number, sip_uri], transfer_attributes)

Calls

Create Call

api_instance = Bandwidth::CallsApi.new
account_id = '9900000'
create_call = Bandwidth::CreateCall.new({to: '+19195551234', from: '+19195554321', application_id: '1234-qwer-5679-tyui', answer_url: 'https://www.myCallbackServer.com/webhooks/answer'})

begin
result = api_instance.create_call(account_id, create_call)
p result
rescue Bandwidth::ApiError => e
puts "Error when calling CallsApi->create_call: #{e}"
end

Get Call Information

api_instance = Bandwidth::CallsApi.new
account_id = '9900000'
call_id = 'c-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85'

begin
result = api_instance.get_call_state(account_id, call_id)
p result
rescue Bandwidth::ApiError => e
puts "Error when calling CallsApi->get_call_state: #{e}"
end

Update Call

api_instance = Bandwidth::CallsApi.new
account_id = '9900000'
call_id = 'c-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85'
update_call = Bandwidth::UpdateCall.new

begin
api_instance.update_call(account_id, call_id, update_call)
rescue Bandwidth::ApiError => e
puts "Error when calling CallsApi->update_call: #{e}"
end

Replace Call BXML

api_instance = Bandwidth::CallsApi.new
account_id = '9900000'
call_id = 'c-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85'
body = '<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<Bxml>
<SpeakSentence>This is a test sentence.</SpeakSentence>
</Bxml>'

begin
api_instance.update_call_bxml(account_id, call_id, body)
rescue Bandwidth::ApiError => e
puts "Error when calling CallsApi->update_call_bxml: #{e}"
end

Conferences

List Conferences

api_instance = Bandwidth::ConferencesApi.new
account_id = '9900000'
opts = {
name: 'my-custom-name',
min_created_time: '2022-06-21T19:13:21Z',
max_created_time: '2022-06-21T19:13:21Z',
page_size: 500,
page_token: 'page_token_example'
}

begin
result = api_instance.list_conferences(account_id, opts)
p result
rescue Bandwidth::ApiError => e
puts "Error when calling ConferencesApi->list_conferences: #{e}"
end

Get Conference Information

api_instance = Bandwidth::ConferencesApi.new
account_id = '9900000'
conference_id = 'conf-fe23a767-a75a5b77-20c5-4cca-b581-cbbf0776eca9'

begin
result = api_instance.get_conference(account_id, conference_id)
p result
rescue Bandwidth::ApiError => e
puts "Error when calling ConferencesApi->get_conference: #{e}"
end

Update Conference

api_instance = Bandwidth::ConferencesApi.new
account_id = '9900000'
conference_id = 'conf-fe23a767-a75a5b77-20c5-4cca-b581-cbbf0776eca9'
update_conference = Bandwidth::UpdateConference.new

begin
api_instance.update_conference(account_id, conference_id, update_conference)
rescue Bandwidth::ApiError => e
puts "Error when calling ConferencesApi->update_conference: #{e}"
end

Update Conference BXML

api_instance = Bandwidth::ConferencesApi.new
account_id = '9900000'
conference_id = 'conf-fe23a767-a75a5b77-20c5-4cca-b581-cbbf0776eca9'
body = '<?xml version="1.0" encoding="UTF-8"?>
<Bxml>
<StopRecording/>
</Bxml>'

begin
api_instance.update_conference_bxml(account_id, conference_id, body)
rescue Bandwidth::ApiError => e
puts "Error when calling ConferencesApi->update_conference_bxml: #{e}"
end

Get Conference Member

api_instance = Bandwidth::ConferencesApi.new
account_id = '9900000'
conference_id = 'conf-fe23a767-a75a5b77-20c5-4cca-b581-cbbf0776eca9'
member_id = 'c-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85'

begin
result = api_instance.get_conference_member(account_id, conference_id, member_id)
p result
rescue Bandwidth::ApiError => e
puts "Error when calling ConferencesApi->get_conference_member: #{e}"
end

Update Conference Member

api_instance = Bandwidth::ConferencesApi.new
account_id = '9900000'
conference_id = 'conf-fe23a767-a75a5b77-20c5-4cca-b581-cbbf0776eca9'
member_id = 'c-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85'
update_conference_member = Bandwidth::UpdateConferenceMember.new

begin
api_instance.update_conference_member(account_id, conference_id, member_id, update_conference_member)
rescue Bandwidth::ApiError => e
puts "Error when calling ConferencesApi->update_conference_member: #{e}"
end

List Conference Recordings

api_instance = Bandwidth::ConferencesApi.new
account_id = '9900000'
conference_id = 'conf-fe23a767-a75a5b77-20c5-4cca-b581-cbbf0776eca9'

begin
result = api_instance.list_conference_recordings(account_id, conference_id)
p result
rescue Bandwidth::ApiError => e
puts "Error when calling ConferencesApi->list_conference_recordings: #{e}"
end

Get Conference Recording Information

api_instance = Bandwidth::ConferencesApi.new
account_id = '9900000'
conference_id = 'conf-fe23a767-a75a5b77-20c5-4cca-b581-cbbf0776eca9'
recording_id = 'r-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85'

begin
result = api_instance.get_conference_recording(account_id, conference_id, recording_id)
p result
rescue Bandwidth::ApiError => e
puts "Error when calling ConferencesApi->get_conference_recording: #{e}"
end

Download Conference Recording

api_instance = Bandwidth::ConferencesApi.new
account_id = '9900000'
conference_id = 'conf-fe23a767-a75a5b77-20c5-4cca-b581-cbbf0776eca9'
recording_id = 'r-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85'

begin
result = api_instance.download_conference_recording(account_id, conference_id, recording_id)
p result
rescue Bandwidth::ApiError => e
puts "Error when calling ConferencesApi->download_conference_recording: #{e}"
end

Recordings

List Account Call Recordings

api_instance = Bandwidth::RecordingsApi.new
account_id = '9900000'
opts = {
to: '+19195551234',
from: '+19195554321',
min_start_time: '2022-06-21T19:13:21Z',
max_start_time: '2022-06-21T19:13:21Z'
}

begin
result = api_instance.list_account_call_recordings(account_id, opts)
p result
rescue Bandwidth::ApiError => e
puts "Error when calling RecordingsApi->list_account_call_recordings: #{e}"
end

Update Recording

api_instance = Bandwidth::RecordingsApi.new
account_id = '9900000'
call_id = 'c-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85'
update_call_recording = Bandwidth::UpdateCallRecording.new({state: Bandwidth::RecordingStateEnum::PAUSED})

begin
api_instance.update_call_recording_state(account_id, call_id, update_call_recording)
rescue Bandwidth::ApiError => e
puts "Error when calling RecordingsApi->update_call_recording_state: #{e}"
end

List Call Recordings

api_instance = Bandwidth::RecordingsApi.new
account_id = '9900000'
call_id = 'c-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85'

begin
result = api_instance.list_call_recordings(account_id, call_id)
p result
rescue Bandwidth::ApiError => e
puts "Error when calling RecordingsApi->list_call_recordings: #{e}"
end

Get Call Recording

api_instance = Bandwidth::RecordingsApi.new
account_id = '9900000'
call_id = 'c-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85'
recording_id = 'r-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85'

begin
result = api_instance.get_call_recording(account_id, call_id, recording_id)
p result
rescue Bandwidth::ApiError => e
puts "Error when calling RecordingsApi->get_call_recording: #{e}"
end

Delete Recording

api_instance = Bandwidth::RecordingsApi.new
account_id = '9900000'
call_id = 'c-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85'
recording_id = 'r-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85'

begin
api_instance.delete_recording(account_id, call_id, recording_id)
rescue Bandwidth::ApiError => e
puts "Error when calling RecordingsApi->delete_recording: #{e}"
end

Download Recording

api_instance = Bandwidth::RecordingsApi.new
account_id = '9900000'
call_id = 'c-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85'
recording_id = 'r-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85'

begin
result = api_instance.download_call_recording(account_id, call_id, recording_id)
p result
rescue Bandwidth::ApiError => e
puts "Error when calling RecordingsApi->download_call_recording: #{e}"
end

Delete Recording Media

api_instance = Bandwidth::RecordingsApi.new
account_id = '9900000'
call_id = 'c-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85'
recording_id = 'r-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85'

begin
api_instance.delete_recording_media(account_id, call_id, recording_id)
rescue Bandwidth::ApiError => e
puts "Error when calling RecordingsApi->delete_recording_media: #{e}"
end

Get Transcription

api_instance = Bandwidth::RecordingsApi.new
account_id = '9900000'
call_id = 'c-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85'
recording_id = 'r-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85'

begin
result = api_instance.get_call_transcription(account_id, call_id, recording_id)
p result
rescue Bandwidth::ApiError => e
puts "Error when calling RecordingsApi->get_call_transcription: #{e}"
end

Create Transcription Request

api_instance = Bandwidth::RecordingsApi.new
account_id = '9900000'
call_id = 'c-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85'
recording_id = 'r-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85'
transcribe_recording = Bandwidth::TranscribeRecording.new

begin
api_instance.transcribe_call_recording(account_id, call_id, recording_id, transcribe_recording)
rescue Bandwidth::ApiError => e
puts "Error when calling RecordingsApi->transcribe_call_recording: #{e}"
end

Delete Transcription

api_instance = Bandwidth::RecordingsApi.new
account_id = '9900000'
call_id = 'c-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85'
recording_id = 'r-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85'

begin
api_instance.delete_call_transcription(account_id, call_id, recording_id)
rescue Bandwidth::ApiError => e
puts "Error when calling RecordingsApi->delete_call_transcription: #{e}"
end

Statistics

Get Account Statistics

api_instance = Bandwidth::StatisticsApi.new
account_id = '9900000'

begin
result = api_instance.get_statistics(account_id)
p result
rescue Bandwidth::ApiError => e
puts "Error when calling StatisticsApi->get_statistics: #{e}"
end

Telephone Number Lookup

Create Lookup

api_instance = Bandwidth::PhoneNumberLookupApi.new
account_id = '9900000'
lookup_request = Bandwidth::LookupRequest.new({tns: ['+15554443333']})

begin
result = api_instance.create_lookup(account_id, lookup_request)
p result
rescue Bandwidth::ApiError => e
puts "Error when calling PhoneNumberLookupApi->create_lookup: #{e}"
end

Get Lookup Request Status

api_instance = Bandwidth::PhoneNumberLookupApi.new
account_id = '9900000'
request_id = '004223a0-8b17-41b1-bf81-20732adf5590'

begin
result = api_instance.get_lookup_status(account_id, request_id)
p result
rescue Bandwidth::ApiError => e
puts "Error when calling PhoneNumberLookupApi->get_lookup_status: #{e}"
end

Multi-Factor Authentication

Voice MFA Code

api_instance = Bandwidth::MFAApi.new
account_id = '9900000'
code_request = Bandwidth::CodeRequest.new({to: '+19195551234', from: '+19195554321', application_id: '66fd98ae-ac8d-a00f-7fcd-ba3280aeb9b1', message: 'Your temporary {NAME} {SCOPE} code is {CODE}', digits: 6})

begin
result = api_instance.generate_voice_code(account_id, code_request)
p result
rescue Bandwidth::ApiError => e
puts "Error when calling MFAApi->generate_voice_code: #{e}"
end

Messaging MFA Code

api_instance = Bandwidth::MFAApi.new
account_id = '9900000'
code_request = Bandwidth::CodeRequest.new({to: '+19195551234', from: '+19195554321', application_id: '66fd98ae-ac8d-a00f-7fcd-ba3280aeb9b1', message: 'Your temporary {NAME} {SCOPE} code is {CODE}', digits: 6})

begin
result = api_instance.generate_messaging_code(account_id, code_request)
p result
rescue Bandwidth::ApiError => e
puts "Error when calling MFAApi->generate_messaging_code: #{e}"
end

Verify MFA Code

api_instance = Bandwidth::MFAApi.new
account_id = '9900000'
verify_code_request = Bandwidth::VerifyCodeRequest.new({to: '+19195551234', expiration_time_in_minutes: 3, code: '123456'})

begin
result = api_instance.verify_code(account_id, verify_code_request)
p result
rescue Bandwidth::ApiError => e
puts "Error when calling MFAApi->verify_code: #{e}"
end