Skip to main content

Infobip Client API Libraries OpenAPI Specification

Project description

Infobip API Python Client

Pypi index MIT License

This is a Python package for Infobip API and you can use it as a dependency to add Infobip APIs to your application. To use the package you'll need an Infobip account. If you don't already have one, you can create a free trial account here.

We use OpenAPI Generator to generate the package code from the OpenAPI specification.

Infobip

Table of contents:

API documentation

Infobip API Documentation can be found here.

General Info

For infobip-api-python-client versioning we use Semantic Versioning scheme.

Published under MIT License.

Python 3.6 is minimum supported version by this library.

Installation

Pull the library by using the following command:

pip install infobip-api-python-client

Quickstart

Before initializing the client first thing you need to do is to set configuration and authentication.

Configuration

Let's first set the configuration. For that you will need your specific URL. To see your base URL, log in to the Infobip API Resource hub with your Infobip credentials.

    from infobip_api_client.api_client import ApiClient, Configuration
    
    client_config = Configuration(
        host="<YOUR_BASE_URL>",
        api_key={"APIKeyHeader": "<YOUR_API_KEY>"},
        api_key_prefix={"APIKeyHeader": "<YOUR_API_PREFIX>"},
    )

Initialize the Client

With configuration set up you can initialize the API client.

	api_client = ApiClient(client_config)

Now you are ready use the API.

Send an SMS

Here's a basic example of sending the SMS message.

    sms_request = SmsAdvancedTextualRequest(
        messages=[
            SmsTextualMessage(
                destinations=[
                    SmsDestination(
                        to="41793026727",
                    ),
                ],
                _from="InfoSMS",
                text="This is a dummy SMS message sent using Python library",
            )
        ])
        
    api_instance = SendSmsApi(api_client)

    api_response: SmsResponse = api_instance.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 the ApiException in catch block.

    from infobip_api_client.exceptions import ApiException
    
    try:
        api_response: SmsResponse = api_instance.send_sms_message(sms_advanced_textual_request=sms_request)
    except ApiException as ex:
        print("Error occurred while trying to send SMS message.")

In case of failure you can inspect the ApiException for more information.

    try:
        api_response: SmsResponse = api_instance.send_sms_message(sms_advanced_binary_request=sms_advanced_binary_request)
    except ApiException as ex:
        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 (SmsResponse object) you can pull out the bulk_id and message_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_id
    message_id = api_response.messages[0].message_id

Receive sent SMS report

For 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 in notify_url field of SmsTextualMessage, or subscribe for reports by contacting our support team. e.g. https://{yourDomain}/delivery-reports

Example of webhook implementation using Flask:

    @app.route("/api/delivery-reports", methods=["POST"])
    def delivery_report():
        delivery_result = SmsDeliveryResult(
            results=request.json["results"]
        )
        
        for result in delivery_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 supported date format.

Fetching delivery reports

If you are for any reason unable to receive real time delivery reports on your endpoint, you can use message_id or bulk_id to fetch them. Each request will return a batch of delivery reports. Please be aware that these can be retrieved only once.

    api_response = api_instance.get_outbound_sms_message_delivery_reports(bulk_id=bulk_id, message_id=message_id, limit=2)
    print(api_response)

Unicode & SMS preview

Infobip 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 = api_instance.preview_sms_message(sms_preview_request=sms_preview_request)

Receive incoming SMS

If 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 explained here. e.g. https://{yourDomain}/incoming-sms.

Example of webhook implementation using Flask:

    @app.route("/api/incoming-sms", methods=["POST"])
    def incoming_sms():
        message_results = SmsInboundMessageResult(
            message_count=request.json["message_count"],
            pending_message_count=request.json["pending_message_count"],
            results=request.json["results"]
        )
        
        for result in message_results.results:
            print("message text: {0}".format(result.clean_text))
        

Two-Factor Authentication (2FA)

For 2FA quick start guide please check these examples.

Ask for help

Feel free to open issues on the repository for any issue or feature request. As per pull requests, for details check the CONTRIBUTING file related to it - in short, we will not merge any pull requests, this code is auto-generated.

If it's something that requires our imminent attention feel free to contact us @ support@infobip.com.

Project details


Download files

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

Source Distribution

infobip-api-python-client-3.0.2.tar.gz (67.2 kB view details)

Uploaded Source

Built Distributions

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

infobip_api_python_client-3.0.2-py3.9.egg (414.9 kB view details)

Uploaded Egg

infobip_api_python_client-3.0.2-py3-none-any.whl (191.5 kB view details)

Uploaded Python 3

File details

Details for the file infobip-api-python-client-3.0.2.tar.gz.

File metadata

File hashes

Hashes for infobip-api-python-client-3.0.2.tar.gz
Algorithm Hash digest
SHA256 4793ec61ca690db30fce54f15504f3f6a830f63410edf39fe7f4b90a20957978
MD5 bc1582c0e4ac9e2bc0dce4a37b1407f3
BLAKE2b-256 2a07e5d9fe79d0f67181ba926a146f2e8bb6ed8871f6cf44f99a1e2049c9b160

See more details on using hashes here.

File details

Details for the file infobip_api_python_client-3.0.2-py3.9.egg.

File metadata

File hashes

Hashes for infobip_api_python_client-3.0.2-py3.9.egg
Algorithm Hash digest
SHA256 9b96775ee5633ff423635c2058ac7d1125cd85fe87b6ccba927862ab4c1d341d
MD5 753dd63a41134e5d4b2a75b1e7a48160
BLAKE2b-256 828e62545b32f90ba2b6ffcb3e90612ec8a466d026ac7c56227b9d6496e447fe

See more details on using hashes here.

File details

Details for the file infobip_api_python_client-3.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for infobip_api_python_client-3.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b07bc47b85d63eff04889a28206d962c5ba86361aa36e5fb40bed75741d219f9
MD5 64b2e134905607720d78caacc3a3546f
BLAKE2b-256 f4dfe7a251c9a4489a3262bdaf625dff5b0d5c37ed04aac12720bb08024718af

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