Skip to main content

v14 -> v15

Client Initialization

import bandwidth

configuration = bandwidth.Configuration(
username = 'API_USERNAME',
password = 'API_PASSWORD'
)
api_client = bandwidth.ApiClient(configuration)

account_id = "5500000"

Messaging

Create Message

from bandwidth.api import messages_api
from bandwidth.model.messaging_request_error import MessagingRequestError
from bandwidth.model.create_message_request_error import CreateMessageRequestError
from bandwidth.model.message_request import MessageRequest
from bandwidth.model.message import Message

messaging_api_instance = messages_api.MessagesApi(api_client)

message_request = MessageRequest(
application_id="93de2206-9669-4e07-948d-329f4b722ee2",
to=["+17777777777"],
_from="+18888888888",
text="Greetings!"
)
try:
api_response = messaging_api_instance.create_message(account_id, message_request)
print(api_response)
except bandwidth.ApiException as e:
print("Exception when calling MessagesApi->create_message: %s\n" % e)

List Messages

from bandwidth.api import messages_api

messaging_api_instance = messages_api.MessagesApi(api_client)

message_id = "9e0df4ca-b18d-40d7-a59f-82fcdf5ae8e6"

try:
api_response = messaging_api_instance.list_messages(account_id, message_id=message_id)
print(api_response)
except bandwidth.ApiException as e:
print("Exception when calling MessagesApi->list_messages: %s\n" % e)

Media

List Media

api_instance = media_api.MediaApi(api_client)
account_id = "9900000" # str | Your Bandwidth Account ID.
continuation_token = "1XEi2tsFtLo1JbtLwETnM1ZJ+PqAa8w6ENvC5QKvwyrCDYII663Gy5M4s40owR1tjkuWUif6qbWvFtQJR5/ipqbUnfAqL254LKNlPy6tATCzioKSuHuOqgzloDkSwRtX0LtcL2otHS69hK343m+SjdL+vlj71tT39" # str | Continuation token used to retrieve subsequent media. (optional)

# example passing only required values which don't have defaults set
try:
api_response = api_instance.list_media(account_id)
pprint(api_response)
except bandwidth.ApiException as e:
print("Exception when calling MediaApi->list_media: %s\n" % e)

# example passing only required values which don't have defaults set
# and optional values
try:
api_response = api_instance.list_media(account_id, continuation_token=continuation_token)
pprint(api_response)
except bandwidth.ApiException as e:
print("Exception when calling MediaApi->list_media: %s\n" % e)

Get Media

api_instance = media_api.MediaApi(api_client)
account_id = "9900000" # str | Your Bandwidth Account ID.
media_id = "14762070468292kw2fuqty55yp2b2/0/bw.png" # str | Media ID to retrieve.

try:
api_response = api_instance.get_media(account_id, media_id)
pprint(api_response)
except bandwidth.ApiException as e:
print("Exception when calling MediaApi->get_media: %s\n" % e)

Upload Media

api_instance = media_api.MediaApi(api_client)
account_id = "9900000" # str | Your Bandwidth Account ID.
media_id = "14762070468292kw2fuqty55yp2b2/0/bw.png" # str | Media ID to retrieve.
body = open('/path/to/file', 'rb') # file_type |
content_type = "audio/wav" # str | The media type of the entity-body. (optional)
cache_control = "no-cache" # str | General-header field is used to specify directives that MUST be obeyed by all caching mechanisms along the request/response chain. (optional)

# example passing only required values which don't have defaults set
try:
api_instance.upload_media(account_id, media_id, body)
except bandwidth.ApiException as e:
print("Exception when calling MediaApi->upload_media: %s\n" % e)

# example passing only required values which don't have defaults set
# and optional values
try:
api_instance.upload_media(account_id, media_id, body, content_type=content_type, cache_control=cache_control)
except bandwidth.ApiException as e:
print("Exception when calling MediaApi->upload_media: %s\n" % e)

Delete Media

api_instance = media_api.MediaApi(api_client)
account_id = "9900000" # str | Your Bandwidth Account ID.
media_id = "14762070468292kw2fuqty55yp2b2/0/bw.png" # str | Media ID to retrieve.

try:
api_instance.delete_media(account_id, media_id)
except bandwidth.ApiException as e:
print("Exception when calling MediaApi->delete_media: %s\n" % e)

BXML

Imports

from bandwidth.model.bxml.response import Response    # <Response />
from bandwidth.model.bxml.bxml import Bxml # <Bxml />
from bandwidth.model.bxml.verbs import *
from bandwidth.model.bxml.verbs import SpeakSentence

Bridge

from bandwidth.model.bxml.verbs import Bridge

bridge = Bridge(
target_call="c-95ac8d6e-1a31c52e-b38f-4198-93c1-51633ec68f8d"
)

Conference

from bandwidth.model.bxml.verbs import Conference, PlayAudio

play_audio = PlayAudio(
url="https://audio.url/audio1.wav"
)

conference = Conference(
name="my-conference-name",
audio_and_recording_verbs=[play_audio]
)

Forward

from bandwidth.model.bxml.verbs import Forward

forward = Forward(
to="+10987654321",
_from="+11234567890"
)

Gather

from bandwidth.model.bxml.verbs import Gather

gather = Gather(
audio_verbs=[other_verbs], # For nested audio producers (speak sentence/play audio)
gather_url="https://gather.url/nextBXML"
)

# or
gather.add_verb(other_verbs) # For nested audio producers (speak sentence/play audio)

Hangup

from bandwidth.model.bxml.verbs import Hangup

hangup = Hangup()

Pause

from bandwidth.model.bxml.verbs import Pause

pause = Pause(duration=2)

Pause Recording

from bandwidth.model.bxml.verbs import PauseRecording

pause_recording = PauseRecording()

Play Audio

from bandwidth.model.bxml.verbs import Play Audio

play_audio = PlayAudio(
audio_uri="https://audio.url/audio1.wav"
)

Record

from bandwidth.model.bxml.verbs import Record

record = Record(
record_complete_url="https://myapp.com/nextBXML",
recording_available_url="https://myapp.com/recordingAvailable",
max_duration=10
)

Redirect

from bandwidth.model.bxml.verbs import Redirect

redirect = Redirect(
redirect_url="http://flow.url/newFlow"
)

Resume Recording

from bandwidth.model.bxml.verbs import ResumeRecording

resume_recording = ResumeRecording()

Ring

from bandwidth.model.bxml.verbs import Ring

ring = Ring(
duration=10,
answer_call=False
)

Send DTMF

from bandwidth.model.bxml.verbs import SendDtmf

send_dtmf = SendDtmf(digits="12w34")

Speak Sentence

from bandwidth.model.bxml.verbs import SpeakSentence

speak_sentence = SpeakSentence(
sentence="This is a test.",
voice="julie"
)

Start Gather

from bandwidth.model.bxml.verbs import StartGather

start_gather = StartGather(
dtmf_url="https://startgather.url/callback"
)

Start Recording

from bandwidth.model.bxml.verbs import StartRecording

start_recording = StartRecording(
recording_available_url="https://myapp.com/noBXML"
)

Start Stream

from bandwidth.model.bxml.verbs import StartStream
from bandwidth.model.bxml.verbs import StreamParam

stream_param = StreamParam(
name="internal_id",
value="call_ABC"
)

start_stream = StartStream(
name="live_audience",
tracks="both",
destination="wss://live-studio-audience.myapp.example.com",
stream_events_url="https://myapp.example.com/noBXML",
stream_params=[stream_param]
)

Stop Gather

from bandwidth.model.bxml.verbs import StopGather

stop_gather = StopGather()

Stop Recording

from bandwidth.model.bxml.verbs import StopRecording

stop_recording = StopRecording()

Stop Stream

from bandwidth.model.bxml.verbs import StopStream

stop_stream = StopStream(
name="live_audience",
)

Tag

from bandwidth.model.bxml.verbs import Tag

tag = Tag(
tag="audio playing"
)

Transfer

from bandwidth.voice.bxml.verbs import Transfer, PhoneNumber, SipUri

phone_number = PhoneNumber(
number="+11234567892"
)

sip_uri = SipUri(
uri="sip:user@server.com"
)

transfer = Transfer(
transfer_caller_id="+11234567891",
transfer_to=[phone_number, sip_uri]
)

Calls

Create Call

from bandwidth.api import calls_api

calls_api_instance = calls_api.CallsApi(api_client)

create_call = CreateCall(
to="+19195551234",
_from="+19195554321",
application_id="1234-qwer-5679-tyui",
answer_url="https://www.myCallbackServer.com/webhooks/answer",
)

try:
api_response = calls_api_instance.create_call(account_id, create_call)
print(api_response)
except bandwidth.ApiException as e:
print("Exception when calling CallsApi->create_call: %s\n" % e)

Get Call Information

from bandwidth.api import calls_api

calls_api_instance = calls_api.CallsApi(api_client)

call_id = "c-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85"

try:
api_response = calls_api_instance.get_call_state(account_id, call_id)
print(api_response)
except bandwidth.ApiException as e:
print("Exception when calling CallsApi->get_call_state: %s\n" % e)

Update Call

from bandwidth.api import calls_api
from bandwidth.model.update_call import UpdateCall
from bandwidth.model.call_state_enum import CallStateEnum

calls_api_instance = calls_api.CallsApi(api_client)
call_id = "c-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85"

update_call = UpdateCall(
state=CallStateEnum("active"),
redirect_url="https://myServer.com/bandwidth/webhooks/redirect",
)

try:
calls_api_instance.update_call(account_id, call_id, update_call)
except bandwidth.ApiException as e:
print("Exception when calling CallsApi->update_call: %s\n" % e)

Replace Call BXML

from bandwidth.api import calls_api
from bandwidth.model.bxml.bxml import Bxml
from bandwidth.model.bxml.verbs import SpeakSentence

calls_api_instance = calls_api.CallsApi(api_client)

call_id = "c-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85"

bxml = Bxml()
speak_sentence = SpeakSentence(
text='This is a test.',
voice="julie"
)

bxml.add_verb(speak_sentence)

try:
calls_api_instance.update_call_bxml(account_id, call_id, bxml.to_bxml())
except bandwidth.ApiException as e:
print("Exception when calling CallsApi->update_call_bxml: %s\n" % e)

Conferences

List Conferences

from bandwidth.api import conferences_api

conferences_api_instance = conferences_api.ConferencesApi(api_client)

try:
api_response = conferences_api_instance.list_conferences(account_id)
print(api_response)
except bandwidth.ApiException as e:
print("Exception when calling ConferencesApi->list_conferences: %s\n" % e)

Get Conference Information

from bandwidth.api import conferences_api

conferences_api_instance = conferences_api.ConferencesApi(api_client)

conference_id = "conf-fe23a767-a75a5b77-20c5-4cca-b581-cbbf0776eca9"

try:
api_response = conferences_api_instance.get_conference(account_id, conference_id)
print(api_response)
except bandwidth.ApiException as e:
print("Exception when calling ConferencesApi->get_conference: %s\n" % e)

Update Conference

from bandwidth.api import conferences_api
from bandwidth.model.update_conference import UpdateConference
from bandwidth.model.conference_state_enum import ConferenceStateEnum

conferences_api_instance = conferences_api.ConferencesApi(api_client)

conference_id = "conf-fe23a767-a75a5b77-20c5-4cca-b581-cbbf0776eca9"
update_conference = UpdateConference(
status=ConferenceStateEnum("active"),
redirect_url="https://myServer.com/bandwidth/webhooks/conferenceRedirect"
)

try:
conferences_api_instance.update_conference(account_id, conference_id, update_conference)
except bandwidth.ApiException as e:
print("Exception when calling ConferencesApi->update_conference: %s\n" % e)

Update Conference BXML

from bandwidth.api import conferences_api

conferences_api_instance = conferences_api.ConferencesApi(api_client)

conference_id = "conf-fe23a767-a75a5b77-20c5-4cca-b581-cbbf0776eca9"

bxml = Bxml()
speak_sentence = SpeakSentence(
text='This is a test.',
voice="julie"
)

bxml.add_verb(speak_sentence)

try:
conferences_api_instance.update_conference_bxml(account_id, conference_id, bxml.to_bxml())
except bandwidth.ApiException as e:
print("Exception when calling ConferencesApi->update_conference_bxml: %s\n" % e)

Get Conference Member

from bandwidth.api import conferences_api

conferences_api_instance = conferences_api.ConferencesApi(api_client)

conference_id = "conf-fe23a767-a75a5b77-20c5-4cca-b581-cbbf0776eca9"
member_id = "c-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85"

try:
api_response = conferences_api_instance.get_conference_member(account_id, conference_id, member_id)
print(api_response)
except bandwidth.ApiException as e:
print("Exception when calling ConferencesApi->get_conference_member: %s\n" % e)

Update Conference Member

from bandwidth.api import conferences_api
from bandwidth.model.update_conference_member import UpdateConferenceMember

conferences_api_instance = conferences_api.ConferencesApi(api_client)

conference_id = "conf-fe23a767-a75a5b77-20c5-4cca-b581-cbbf0776eca9"
member_id = "c-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85"

update_conference_member = UpdateConferenceMember(
mute=False,
hold=False,
call_ids_to_coach=["c-25ac29a2-1331029c-2cb0-4a07-b215-b22865662d85"],
)

try:
conferences_api_instance.update_conference_member(account_id, conference_id, member_id, update_conference_member)
except bandwidth.ApiException as e:
print("Exception when calling ConferencesApi->update_conference_member: %s\n" % e)

List Conference Recordings

from bandwidth.api import conferences_api

conferences_api_instance = conferences_api.ConferencesApi(api_client)

conference_id = "conf-fe23a767-a75a5b77-20c5-4cca-b581-cbbf0776eca9"

try:
api_response = conferences_api_instance.list_conference_recordings(account_id, conference_id)
print(api_response)
except bandwidth.ApiException as e:
print("Exception when calling ConferencesApi->list_conference_recordings: %s\n" % e)

Get Conference Recording Information

from bandwidth.api import conferences_api

conferences_api_instance = conferences_api.ConferencesApi(api_client)

conference_id = "conf-fe23a767-a75a5b77-20c5-4cca-b581-cbbf0776eca9"
recording_id = "r-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85"

try:
api_response = conferences_api_instance.get_conference_recording(account_id, conference_id, recording_id)
print(api_response)
except bandwidth.ApiException as e:
print("Exception when calling ConferencesApi->get_conference_recording: %s\n" % e)

Download Conference Recording

from bandwidth.api import conferences_api

conferences_api_instance = conferences_api.ConferencesApi(api_client)

conference_id = "conf-fe23a767-a75a5b77-20c5-4cca-b581-cbbf0776eca9"
recording_id = "r-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85"

try:
api_response = conferences_api_instance.download_conference_recording(account_id, conference_id, recording_id)
print(api_response)
except bandwidth.ApiException as e:
print("Exception when calling ConferencesApi->download_conference_recording: %s\n" % e)

Recordings

List Account Call Recordings

from bandwidth.api import recordings_api

recordings_api_instance = recordings_api.RecordingsApi(api_client)

try:
api_response = recordings_api_instance.list_account_call_recordings(account_id)
print(api_response)
except bandwidth.ApiException as e:
print("Exception when calling RecordingsApi->list_account_call_recordings: %s\n" % e)

Update Recording

from bandwidth.api import recordings_api
from bandwidth.model.update_call_recording import UpdateCallRecording
from bandwidth.model.recording_state_enum import RecordingStateEnum

recordings_api_instance = recordings_api.RecordingsApi(api_client)

call_id = "c-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85"
update_call_recording = UpdateCallRecording(
state=RecordingStateEnum("paused"),
)

try:
recordings_api_instance.update_call_recording_state(account_id, call_id, update_call_recording)
except bandwidth.ApiException as e:
print("Exception when calling RecordingsApi->update_call_recording_state: %s\n" % e)

List Call Recordings

from bandwidth.api import recordings_api

recordings_api_instance = recordings_api.RecordingsApi(api_client)

call_id = "c-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85"

try:
api_response = recordings_api_instance.list_call_recordings(account_id, call_id)
print(api_response)
except bandwidth.ApiException as e:
print("Exception when calling RecordingsApi->list_call_recordings: %s\n" % e)

Get Call Recording

from bandwidth.api import recordings_api

recordings_api_instance = recordings_api.RecordingsApi(api_client)

call_id = "c-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85"
recording_id = "r-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85"

try:
api_response = recordings_api_instance.get_call_recording(account_id, call_id, recording_id)
print(api_response)
except bandwidth.ApiException as e:
print("Exception when calling RecordingsApi->get_call_recording: %s\n" % e)

Delete Recording

from bandwidth.api import recordings_api

recordings_api_instance = recordings_api.RecordingsApi(api_client)

call_id = "c-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85"
recording_id = "r-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85"

try:
recordings_api_instance.delete_recording(account_id, call_id, recording_id)
except bandwidth.ApiException as e:
print("Exception when calling RecordingsApi->delete_recording: %s\n" % e)

Download Recording

from bandwidth.api import recordings_api

recordings_api_instance = recordings_api.RecordingsApi(api_client)

call_id = "c-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85"
recording_id = "r-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85"

try:
api_response = recordings_api_instance.download_call_recording(account_id, call_id, recording_id)
print(api_response)
except bandwidth.ApiException as e:
print("Exception when calling RecordingsApi->download_call_recording: %s\n" % e)

Delete Recording Media

from bandwidth.api import recordings_api

recordings_api_instance = recordings_api.RecordingsApi(api_client)

call_id = "c-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85"
recording_id = "r-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85"

try:
recordings_api_instance.delete_recording_media(account_id, call_id, recording_id)
except bandwidth.ApiException as e:
print("Exception when calling RecordingsApi->delete_recording_media: %s\n" % e)

Get Transcription

from bandwidth.api import recordings_api

recordings_api_instance = recordings_api.RecordingsApi(api_client)

call_id = "c-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85"
recording_id = "r-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85"

try:
api_response = recordings_api_instance.get_call_transcription(account_id, call_id, recording_id)
print(api_response)
except bandwidth.ApiException as e:
print("Exception when calling RecordingsApi->get_call_transcription: %s\n" % e)

Create Transcription Request

from bandwidth.api import recordings_api
from bandwidth.model.transcribe_recording import TranscribeRecording

recordings_api_instance = recordings_api.RecordingsApi(api_client)

call_id = "c-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85"
recording_id = "r-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85"

transcribe_recording = TranscribeRecording(
callback_url="https://myServer.com/bandwidth/webhooks/transcriptionAvailable"
)

try:
recordings_api_instance.transcribe_call_recording(account_id, call_id, recording_id, transcribe_recording)
except bandwidth.ApiException as e:
print("Exception when calling RecordingsApi->transcribe_call_recording: %s\n" % e)

Delete Transcription

from bandwidth.api import recordings_api

recordings_api_instance = recordings_api.RecordingsApi(api_client)

call_id = "c-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85"
recording_id = "r-15ac29a2-1331029c-2cb0-4a07-b215-b22865662d85"

try:
recordings_api_instance.delete_call_transcription(account_id, call_id, recording_id)
except bandwidth.ApiException as e:
print("Exception when calling RecordingsApi->delete_call_transcription: %s\n" % e)

Telephone Number Lookup

Create Lookup

from bandwidth.model.lookup_request import LookupRequest

phone_number_lookup_api_instance = phone_number_lookup_api.PhoneNumberLookupApi(api_client)
lookup_request = LookupRequest(
tns=["+15554443333"],
)

try:
api_response = phone_number_lookup_api_instance.create_lookup(account_id, lookup_request)
print(api_response)
except bandwidth.ApiException as e:
print("Exception when calling PhoneNumberLookupApi->create_lookup: %s\n" % e)

Get Lookup Request Status

phone_number_lookup_api_instance = phone_number_lookup_api.PhoneNumberLookupApi(api_client)

request_id = "004223a0-8b17-41b1-bf81-20732adf5590"

try:
api_response = phone_number_lookup_api_instance.get_lookup_status(account_id, request_id)
print(api_response)
except bandwidth.ApiException as e:
print("Exception when calling PhoneNumberLookupApi->get_lookup_status: %s\n" % e)

Multi-Factor Authentication

Voice MFA Code

from bandwidth.api import mfa_api
from bandwidth.model.code_request import CodeRequest

mfa_api_instance = mfa_api.MFAApi(api_client)

code_request = CodeRequest(
to="+15553334444",
_from="+15554443333",
application_id="66fd98ae-ac8d-a00f-7fcd-ba3280aeb9b1",
scope="2FA",
message="Your temporary {NAME} {SCOPE} code is {CODE}",
digits=6,
)

try:
api_response = mfa_api_instance.generate_voice_code(account_id, code_request)
print(api_response)
except bandwidth.ApiException as e:
print("Exception when calling MFAApi->generate_messaging_code: %s\n" % e)

Messaging MFA Code

from bandwidth.api import mfa_api
from bandwidth.model.code_request import CodeRequest

mfa_api_instance = mfa_api.MFAApi(api_client)

code_request = CodeRequest(
to="+15553334444",
_from="+15554443333",
application_id="66fd98ae-ac8d-a00f-7fcd-ba3280aeb9b1",
scope="2FA",
message="Your temporary {NAME} {SCOPE} code is {CODE}",
digits=6,
)

try:
api_response = mfa_api_instance.generate_messaging_code(account_id, code_request)
print(api_response)
except bandwidth.ApiException as e:
print("Exception when calling MFAApi->generate_messaging_code: %s\n" % e)

Verify MFA Code

from bandwidth.api import mfa_api
from bandwidth.model.code_request import CodeRequest

mfa_api_instance = mfa_api.MFAApi(api_client)

verify_code_request = VerifyCodeRequest(
to="+19195551234",
scope="2FA",
expiration_time_in_minutes=3,
code="123456",
)

try:
api_response = mfa_api_instance.verify_code(account_id, verify_code_request)
print(api_response)
except bandwidth.ApiException as e:
print("Exception when calling MFAApi->verify_code: %s\n" % e)