Package: infobip-cpaasx
Infobip CPaaS X Python ClientThis is a Python package for Infobip CpaaS X API and you can use it as a dependency to addInfobip APIsto your application.
To use the package you'll need an Infobip account. If you don't already have one, you can create afree trialaccounthere.The package is built on top ofOpenAPI Specification, generated byInfobip OSCARservice powered byOpenAPI Generator.Table of contents:API documentationGeneral InfoInstallationQuickstartAsk for helpAPI documentationInfobip API Documentation can be foundhere.The current version of this library includes this subset of Infobip products:Application and Entity ManagementNumbersSMSMMSCPaaS X product documentation can be foundhere.General InfoWe use theSemantic Versioningscheme.The library is published underMIT License.Python 3.7 is minimum supported version by this library.InstallationPull the library by using the following command:pipinstallinfobip_cpaasxQuickstartBefore initializing the client first thing you need to do is to set configuration and authentication.ConfigurationLet's first set the configuration. For that you will need your specific URL.
To see your base URL, log in to theInfobip API Resourcehub with your Infobip credentials.frominfobip_cpaasximportApiClient,Configurationclient_config=Configuration(host="<YOUR_BASE_URL>",api_key={"APIKeyHeader":"<YOUR_API_KEY>"},api_key_prefix={"APIKeyHeader":"App"},)Initialize the ClientWith configuration set up you can initialize the API client.api_client=ApiClient(client_config)Now you are ready use the API.Create applicationA basic example how to create an application.application_request=Application(application_name="Application",application_id="application-id")application_api=ApplicationApi(api_client)application_api.create_application(application=application_request)Create entityA basic example how to create an entity.entity_request=Entity(entity_name="Entity",entity_id="entity-id")entity_api=EntityApi(api_client)entity_api.create_entity(entity=entity_request)Send an SMSA basic example how to send an SMS message.sms_request=SmsAdvancedTextualRequest(messages=[SmsTextualMessage(destinations=[SmsDestination(to="41793026727",),],var_from="InfoSMS",text="This is a dummy SMS message sent using Python library",application_id="my-application-id",entity_id="my-entity-id")])sms_api=SmsApi(api_client)api_response:SmsResponse=sms_api.send_sms_message(sms_advanced_textual_request=sms_request)pprint(api_response)To make your code more robust send the message in try block and handle theApiExceptionin catch block.frominfobip_cpaasximportApiException,SmsResponsetry:api_response:SmsResponse=sms_api.send_sms_message(sms_advanced_textual_request=sms_request)exceptApiExceptionasex:print("Error occurred while trying to send SMS message.")In case of failure you can inspect theApiExceptionfor more information.try:api_response:SmsResponse=sms_api.send_sms_message(sms_advanced_textual_request=sms_request)exceptApiExceptionasex:print("Error occurred while trying to send SMS message.")print("Error status:%s\n"%ex.status)print("Error headers:%s\n"%ex.headers)print("Error body:%s\n"%ex.body)Additionally, from the successful response (SmsResponseobject) you can pull out thebulk_idandmessage_id(s) and use them to fetch a delivery report for given message or bulk.
Bulk ID will be received only when you send a message to more than one destination address or multiple messages in a single request.bulk_id=api_response.bulk_idmessage_id=api_response.messages[0].message_idReceive sent SMS reportFor each SMS that you send out, we can send you a message delivery report in real time. All you need to do is specify your endpoint when sending SMS innotify_urlfield ofSmsTextualMessage, or subscribe for reports by contacting our support team.
e.g.https://{yourDomain}/delivery-reportsExample of webhook implementation using Flask:@app.route("/api/delivery-reports",methods=["POST"])defdelivery_report():delivery_results=SmsDeliveryResult.from_json(request.json)forresultindelivery_results.results:print("message{0}sent at{1}".format(result.message_id,result.sent_at))If you prefer to use your own serializer, please pay attention to the supporteddate format.Fetching delivery reportsIf you are for any reason unable to receive real time delivery reports on your endpoint, you can usemessage_idorbulk_idto fetch them.
Each request will return a batch of delivery reports. Please be aware that these can be retrieved only once.api_response=sms_api.get_outbound_sms_message_delivery_reports(bulk_id=bulk_id,message_id=message_id)pprint(api_response)Unicode & SMS previewInfobip API supports Unicode characters and automatically detects encoding. Unicode and non-standard GSM characters use additional space, avoid unpleasant surprises and check how different message configurations will affect your message text, number of characters and message parts.sms_preview_request=SmsPreviewRequest(text="Let's see how many characters will remain unused in this message.")api_response=sms_api.preview_sms_message(sms_preview_request=sms_preview_request)Receive incoming SMSIf you want to receive SMS messages from your subscribers we can have them delivered to you in real time. When you buy and configure a number capable of receiving SMS, specify your endpoint as explainedhere.
e.g.https://{yourDomain}/incoming-sms.Example of webhook implementation using Flask:@app.route("/api/incoming-sms",methods=["POST"])defincoming_sms():message_results=SmsInboundMessageResult(message_count=request.json["message_count"],pending_message_count=request.json["pending_message_count"],results=request.json["results"])forresultinmessage_results.results:print("message text:{0}".format(result.clean_text))Send an MMSA basic example how to send an MMS message.mms_request=MmsAdvancedRequest(bulk_id="bulk-id",messages=[MmsAdvancedMessage(destinations=[MmsDestination(to="41793026727")],message_segments=[MmsAdvancedMessageSegmentText(content_id="content-id",text="Message text"),MmsAdvancedMessageSegmentLink(content_id="content-id",content_type="image/jpeg",content_url="https://api.infobip.com/ott/1/media/infobipLogo")],entity_id="my-entity-id",application_id="my-application-id")])mms_api=MmsApi(api_client)api_response:MmsSendResult=mms_api.send_mms_message(mms_advanced_request=mms_request)pprint(api_response)Get available numbersA basic example for getting a list of available numbers.numbers_api=NumbersApi(api_client)api_response:NumbersResponse=numbers_api.get_available_numbers(capabilities=["SMS"])pprint(api_response)All other examples can be found in tests.Ask for helpFeel free to open issues on the repository for any encountered problem or feature request. For pull requests, go to theCONTRIBUTINGfilerelated to it. This code is auto generated, and we are unable to merge any pull requests form here.This code is auto generated, and we are unable to merge any pull request from here, but we will review and implement changes directly within our pipeline, as described in theCONTRIBUTINGfile.For anything that requires our imminent attention, contact us @support@infobip.com.