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.
Detailed documentation about Infobip API can be found here. The current version of this library includes this subset of Infobip products:
For infobip-api-python-client versioning we use Semantic Versioning scheme.
Published under MIT License.
Python 3.8 is minimum supported version by this library.
Pull the library by using the following command:
pip install infobip-api-python-clientBefore initializing the client first thing you need to do is to set configuration and authentication.
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>"},
)With configuration set up you can initialize the API client.
api_client = ApiClient(client_config)Now you are ready use the API.
Here's a basic example of sending the SMS message.
from infobip_api_client.models import SmsRequest, SmsMessage, SmsMessageContent, SmsTextContent, SmsDestination, SmsResponse
from infobip_api_client.api.sms_api import SmsApi
sms_request = SmsRequest(
messages=[
SmsMessage(
destinations=[
SmsDestination(
to="41793026727",
),
],
sender="InfoSMS",
content=SmsMessageContent(actual_instance=SmsTextContent(text="This is a dummy SMS message sent using Python library"))
)
]
)
api_instance = SmsApi(api_client)
api_response: SmsResponse = api_instance.send_sms_messages(sms_request=sms_request)
print(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_messages(sms_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_messages(sms_request=sms_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_idAll you need to do is specify your endpoint when sending SMS in the webhooks.delivery.url field of your request, or subscribe for reports by contacting our support team at [email protected].
e.g. https://{yourDomain}/delivery-reports
Example of webhook implementation using Flask:
from infobip_api_client.models import SmsDeliveryResult
@app.route("/api/delivery-reports", methods=["POST"])
def delivery_report():
delivery_results = 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.
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)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.
from infobip_api_client.models import SmsPreviewRequest
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)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:
from infobip_api_client.models import SmsInboundMessageResult
@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))For the 2FA quick start guide please check these examples.
For the Calls quick start guide please check these examples
For Email quick start guide, view these examples.
For Moments quick start guide, view these examples.
This project follows a pragmatic Semantic Versioning approach.
For full details on how versions are managed, please see our Versioning guide.
Feel free to open an issue on the repository if you see any problem or want to request a feature.
If you want to contribute to this library in any way, please follow the guidelines in CONTRIBUTING file.
For anything that requires our immediate attention, contact us @ [email protected].