Skip to main content

Bandwidth's set of APIs

Project description

Bandwidth Python SDK

Test

OS Python
Windows 2016 3.6, 3.7, 3.8, 3.9
Windows 2019 3.6, 3.7, 3.8, 3.9
Ubuntu 18.04 3.6, 3.7, 3.8, 3.9
Ubuntu 20.04 3.6, 3.7, 3.8, 3.9

Getting Started

Installation

pip install bandwidth-sdk

Initialize

from bandwidth.bandwidth_client import BandwidthClient

from bandwidth.messaging.models.message_request import MessageRequest

from bandwidth.voice.models.create_call_request import CreateCallRequest
from bandwidth.voice.bxml.response import Response
from bandwidth.voice.bxml.verbs import *

from bandwidth.multifactorauth.models.two_factor_code_request_schema import TwoFactorCodeRequestSchema
from bandwidth.multifactorauth.models.two_factor_verify_request_schema import TwoFactorVerifyRequestSchema

from bandwidth.phonenumberlookup.controllers.api_controller import APIController, ApiResponse, APIException
from bandwidth.phonenumberlookup.models.accounts_tnlookup_request import AccountsTnlookupRequest

from bandwidth.webrtc.models.session import Session
from bandwidth.webrtc.models.participant import Participant
from bandwidth.webrtc.models.publish_permission_enum import PublishPermissionEnum

bandwidth_client = BandwidthClient(
    voice_basic_auth_user_name='username',
    voice_basic_auth_password='password',
    messaging_basic_auth_user_name='username',
    messaging_basic_auth_password='password',
    multi_factor_auth_basic_auth_user_name='username',
    multi_factor_auth_basic_auth_password='password',
    phone_number_lookup_basic_auth_user_name='username',
    phone_number_lookup_basic_auth_password='password',
    web_rtc_basic_auth_user_name='username',
    web_rtc_basic_auth_password='password'
)
account_id = "12345"

Create A Phone Call

voice_client = bandwidth_client.voice_client.client

##Create phone call
body = CreateCallRequest()
body.mfrom = "+17777777777"
body.to = "+16666666666"
body.application_id = "3-d-4-b-5"
body.answer_url = "https://test.com"

try:
    response = voice_client.create_call(account_id, body=body)
    print(response.body.call_id) #c-3f758f24-a59bb21e-4f23-4d62-afe9-53o2ls3o4saio4l
    print(response.status_code) #201
except ApiErrorResponseException as e:
    print(e.description) #Invalid from: must be an E164 telephone number
    print(e.response_code) #400

Send A Text Message

messaging_client = bandwidth_client.messaging_client.client

body = MessageRequest()
body.application_id = "1-d-b"
body.to = ["+17777777777"]
body.mfrom = "+18888888888"
body.text = "Greetings!"

try:
    response = messaging_client.create_message(account_id, body)
    print(response.body.id) #1570819529611mexbyfr7ugrouuxy
    print(response.status_code) #202
except MessagingException as e:
    print(e.description) #Your request could not be accepted.
    print(e.response_code) #400

Create BXML

response = Response()
speak_sentence = SpeakSentence(
    sentence="Test",
    voice="susan",
    locale="en_US",
    gender="female"
)

response.add_verb(speak_sentence)
print(response.to_bxml())

Create A MFA Request

auth_client = bandwidth_client.multi_factor_auth_client.client

from_phone = "+18888888888"
to_phone = "+17777777777"
messaging_application_id = "1-d-b"
scope = "scope"
digits = 6

body = TwoFactorCodeRequestSchema(
    mfrom = from_phone,
    to = to_phone,
    application_id = messaging_application_id,
    scope = scope,
    digits = digits,
    message = "Your temporary {NAME} {SCOPE} code is {CODE}"
)
auth_client.create_messaging_two_factor(account_id, body)

code = "123456" #This is the user input to validate

body = TwoFactorVerifyRequestSchema(
    to = to_phone,
    application_id = application_id,
    scope = scope,
    code = code,
    expiration_time_in_minutes = 3
)
response = auth_client.create_verify_two_factor(account_id, body)
print("Auth status: " + str(response.body.valid))

Perform a TN Lookup Request

tnLookup_controller = bandwidth_client.phone_number_lookup_client.client
body = AccountsTnlookupRequest()
body.tns = ['+19195551234']

try:
    response = tnLookup_controller.create_tn_lookup_request(account_id, body)
    print(response.status_code)

except APIException as e:
    print("Error:", e.response_code)

requestId = response.body.request_id    # "1234-abcd-5678-efgh"

try:
    response = tnLookup_controller.get_tn_lookup_result(account_id, requestId)
    print(response)

except APIException as e:
    print("Error:", e.response_code)

WebRtc Participant & Session Management

web_rtc_client = bandwidth_client.web_rtc_client.client

create_session_body = Session()
create_session_body.tag = 'new-session'

create_session_response = web_rtc_client.create_session(account_id, create_session_body)
session_id = create_session_response.body.id

create_participant_body = Participant()
create_participant_body.publish_permissions = [
    PublishPermissionEnum.AUDIO,
    PublishPermissionEnum.VIDEO
]
create_participant_body.callback_url = "https://sample.com"

create_participant_response = web_rtc_client.create_participant(account_id, create_participant_body)
participant_id = create_participant_response.body.participant.id

web_rtc_client.add_participant_to_session(account_id, session_id, participant_id)

Supported Python Versions

This package can be used with Python >= 3.0

Documentation

Documentation for this package can be found at https://dev.bandwidth.com/sdks/python.html

Credentials

Information for credentials for this package can be found at https://dev.bandwidth.com/guides/accountCredentials.html

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bandwidth-sdk-13.0.0.tar.gz (73.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bandwidth_sdk-13.0.0-py3-none-any.whl (146.5 kB view details)

Uploaded Python 3

File details

Details for the file bandwidth-sdk-13.0.0.tar.gz.

File metadata

  • Download URL: bandwidth-sdk-13.0.0.tar.gz
  • Upload date:
  • Size: 73.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for bandwidth-sdk-13.0.0.tar.gz
Algorithm Hash digest
SHA256 305eaee8afeef05fa1dbf53afd1b11ca5359e436c659b9ef16f497d41776960f
MD5 20757feab726839aff8648fb6bb8993e
BLAKE2b-256 b7b8b5f04ce41835e0d248b3761bf69ea19287696351a7faa722b7b39887eb3c

See more details on using hashes here.

File details

Details for the file bandwidth_sdk-13.0.0-py3-none-any.whl.

File metadata

  • Download URL: bandwidth_sdk-13.0.0-py3-none-any.whl
  • Upload date:
  • Size: 146.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for bandwidth_sdk-13.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5273e07c97d33e764d88e7a7dd5a79d7fdf2d44434ac8e3a8fd1fb81a7a8759a
MD5 1e1d60c93f25a60aa4b551f63aa97a61
BLAKE2b-256 951d851b080e2a85f99d8896b490868e8ae2454049c0933f90821aefccad9d74

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page