Python

As an outcome of this guide, you will send a simple text message to your handset with Infobip SMS API.


Prerequisites

Infobip account

• Working Python 3 environment

Difficulty level

This guide assumes very basic knowledge of Python and basic familiarity with APIs.

Summary of the steps

• Install the Infobip API Python SDK.
• Import the SMS Channel and create an SMS Channel instance.
• Add credentials, phone number, and SMS payload to your instance.
• Send an SMS and print the response to track its progress.

Install the Infobip API Python SDK

pip install infobip-api-python-sdk

Create an SMS Channel instance

You’ll use the instance to input your credentials and access its methods. In this case, we will use the send_sms_message method to add the SMS payload.

Step 1. Import the SMSChannel.

from infobip_channels.sms.channel import SMSChannel

Step 2. Create the SMSChannel instance and add your base_url and api_key that you can access either from your Infobip account or from the Infobip API landing page, provided you have logged in.

channel = SMSChannel.from_auth_params({
    "base_url": "<your_base_url>",
    "api_key": "<your_api_key>"
})

Step 3. Use the send_sms_message method to add the SMS payload.

Key points:

• The destination address in the to field must be in the international format, e.g. 447415774332.
• If using a free trial account, the phone number you use must be the same phone number you’ve registered on signup.
• We recommend you put the send_sms_message method within a variable, so that you can print out the response.

sms_response = channel.send_sms_message({
    "messages": [{
        "destinations": [{
            "to": "<phone_number>"
            }],
        "from": "InfobipSMS",
        "text": "Hi! I'm your first Infobip message. Have a lovely day!"
            }
        ]
    }
)

Add multiple recipients and multiple messages

Add multiple recipients by adding multiple to fields to the destination array. Similarly, create multiple destinations objects to send multiple messages with one request.

Step 4. Print the response to monitor the progress of your SMS.

print(sms_response)

Once you run the code, you should get an SMS on your handset and see the following 200 OK response.

{
  "bulkId": "2034072219640523072",
  "messages": [
    {
      "messageId": "41793026727",
      "status": {
        "description": "Message sent to next instance",
        "groupId": 1,
        "groupName": "PENDING",
        "id": 26,
        "name": "MESSAGE_ACCEPTED"
      },
      "to": "2250be2d4219-3af1-78856-aabe-1362af1edfd2"
    }
  ]
}

For troubleshooting and analytics, Use the auto-generated bulkId to fetch the entire bulk of messages, or the messageId to fetch one specific message.