{"id":5519,"date":"2023-02-14T17:05:22","date_gmt":"2023-02-14T16:05:22","guid":{"rendered":"https:\/\/www.smsapi.com\/blog\/?p=5519"},"modified":"2025-07-02T15:12:44","modified_gmt":"2025-07-02T13:12:44","slug":"send-sms-python-api","status":"publish","type":"post","link":"https:\/\/www.smsapi.com\/blog\/send-sms-python-api\/","title":{"rendered":"How to Send SMS Messages Using Python SMS API?"},"content":{"rendered":"\n<p><strong>And now for something completely different! The time has come to discuss another programming library for SMS campaigns. It is written in a peculiar but well-liked language. It\u2019s something for those who enjoy British humour and fast scripts but detest braces. Meet our Python library!<\/strong><\/p>\n\n\n\n<!--more-->\n\n\n\n<p>First, let me direct you to the relevant Python SMS API\u00a0<a href=\"https:\/\/github.com\/smsapi\/smsapi-python-client\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">repository on GitHub<\/a>, where you can find the production code, tests, and examples. Studying it file by file will not be necessary, though. Below, I\u2019ll discuss the essential features of the library, which will allow you to <a href=\"https:\/\/www.smsapi.com\/en\/bulk-sms\" target=\"_blank\" rel=\"noreferrer noopener\">send SMS<\/a> text messages using Python.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button is-style-secondary\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/www.smsapi.com\/en\/sms-api\" target=\"_blank\" rel=\"noreferrer noopener\">SMS API Libraries<\/a><\/div>\n<\/div>\n\n\n\n<p>I will frequently reference the\u00a0<a href=\"https:\/\/www.smsapi.com\/docs\/\" target=\"_blank\" rel=\"noreferrer noopener\">API Documentation<\/a>, which describes communication between any application and the server.\u00a0You are also welcome to read how to send SMS text messages from <a href=\"https:\/\/www.smsapi.com\/blog\/send-sms-with-php-library\/\" target=\"_blank\" rel=\"noreferrer noopener\">PHP<\/a>, <a href=\"https:\/\/www.smsapi.com\/blog\/java-sms-guide-2023\/\" target=\"_blank\" rel=\"noreferrer noopener\">Java<\/a>, <a href=\"https:\/\/www.smsapi.com\/blog\/javascript-sms-api-library-code-snippets-error-codes\/\" target=\"_blank\" rel=\"noreferrer noopener\">JavaScript<\/a>, and <a href=\"https:\/\/www.smsapi.com\/blog\/send-sms-c-library-sms-api\/\" target=\"_blank\" rel=\"noreferrer noopener\">C#<\/a> over the internet.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-smsapi-blog wp-block-embed-smsapi-blog\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"rWelHggB80\"><a href=\"https:\/\/www.smsapi.com\/blog\/api-smsapi-essentials-developer-guide\/\">Send SMS using API \u2013 API SMS Sender \u2013 Developer Guide #01<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; visibility: hidden;\" title=\"&#8220;Send SMS using API \u2013 API SMS Sender \u2013 Developer Guide #01&#8221; &#8212; SMSAPI Blog\" src=\"https:\/\/www.smsapi.com\/blog\/api-smsapi-essentials-developer-guide\/embed\/#?secret=PwjUS1oDAQ#?secret=rWelHggB80\" data-secret=\"rWelHggB80\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">The Python library: installing and configuring SMS sending<\/h2>\n\n\n\n<p>Before you begin coding, we need to prepare a few things. First of all, install the library. It is hosted outside GitHub on the <a target=\"_blank\" rel=\"noreferrer noopener nofollow\" href=\"https:\/\/pypi.org\/project\/smsapi-client\/\">PyPI website<\/a> (Python Package Index). This makes for a lightning-fast installation using the pip package manager.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Installation instructions<\/h3>\n\n\n\n<p>Use the following command line to install the Python file in a few seconds:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>pip install smsapi-client<\/code><\/pre>\n\n\n\n<p>As with the\u00a0<a href=\"https:\/\/www.smsapi.com\/blog\/api\/\" target=\"_blank\" rel=\"noreferrer noopener\">SMSAPI libraries in other languages<\/a>, your program will communicate with the server using HTTP methods: GET, PUT, POST, and DELETE. They will be handled by the external library <a href=\"https:\/\/docs.python-requests.org\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Requests<\/a>, which you must install. It will work \u201cbehind the scenes\u201d in the finished app, so no worries \u2013 you don\u2019t need to be an expert in network protocols to manage <a href=\"https:\/\/www.smsapi.com\/en\/sms-campaigns\" target=\"_blank\" rel=\"noreferrer noopener\">SMS campaigns<\/a> using software code.\u00a0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create an SMSAPI account<\/h2>\n\n\n\n<p>There is one more thing you need to do to send&nbsp;<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/www.smsapi.com\/en\/sms-notifications\">SMS notifications<\/a>&nbsp;in Python:&nbsp;<strong>create and configure your SMSAPI account<\/strong>! You\u2019ll find a quick guide below:<\/p>\n\n\n\n<p>This way, we will commence our cooperation, and you can use the entire SMS API. Also, remember to complete the necessary information:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>personal info<\/strong>: full name, e-mail address and password, phone number;<\/li>\n\n\n\n<li><strong>your company data<\/strong>&nbsp;\u2013 for invoicing;<\/li>\n\n\n\n<li><strong>SMS sender name<\/strong>, which you will read about later;<\/li>\n\n\n\n<li><strong>also helpful<\/strong>: e-mail address for sending invoices and contact phone to your accounting department.<\/li>\n<\/ul>\n\n\n\n<p>Next, in the Customer Panel,&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/ssl.smsapi.com\/react\/oauth\/manage\" target=\"_blank\">generate an OAuth 2<\/a> authentication token. It\u2019s a&nbsp;sort of cryptographic API key. With it, the program can access the SMSAPI server resources. Paste the auth token into the script or place it in your configuration file.<\/p>\n\n\n\n<div class=\"wp-block-smsapi-blog-2021-infobox infobox infobox--attention post-content--full-width\"><div class=\"infobox__icon\"><img decoding=\"async\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMTAwJSIgaGVpZ2h0PSIxMDAlIiB2aWV3Qm94PSIwIDAgMTAgMTYiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgY2xpcC1ydWxlPSJldmVub2RkIiB4bWw6c3BhY2U9InByZXNlcnZlIiB2ZXJzaW9uPSIxLjEiPgogIDxwYXRoIGlkPSJzdmdfMSIgc3R5bGU9ImZpbGw6IHJnYigxNywgMTkyLCAxMjgpOyBmaWxsLXJ1bGU6IG5vbnplcm87IiBkPSJtMi45MTk2NCwwbDQsMGwwLDRsLTEsN2wtMiwwbC0xLC03bDAsLTR6Ii8+CiAgPHBhdGggaWQ9InN2Z18yIiBzdHlsZT0iZmlsbDogcmdiKDE3LCAxOTIsIDEyOCk7IGZpbGwtcnVsZTogbm9uemVybzsiIGQ9Im03LjAwODM3LDE0YzAsMS4xMDUgLTAuODk1LDIgLTIsMmMtMS4xMDUsMCAtMiwtMC44OTUgLTIsLTJjMCwtMS4xMDUgMC44OTUsLTIgMiwtMmMxLjEwNSwwIDIsMC44OTUgMiwyeiIvPgo8L3N2Zz4K\"\/><\/div><div class=\"infobox__title\"><h4>Create a test account<\/h4><\/div><div class=\"infobox__content\">\n<p>We are SMSAPI &#8211; a global SMS messaging provider for companies, startups and the public sector. Create a free test account.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button is-style-primary\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/www.smsapi.com\/en\/signup\" target=\"_blank\" rel=\"noreferrer noopener\">Test SMS API<\/a><\/div>\n<\/div>\n<\/div><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Watch the SMSAPI account tutorial:<\/h3>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-smsapi-blog wp-block-embed-smsapi-blog\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"eiMHlDiZIv\"><a href=\"https:\/\/www.smsapi.com\/blog\/do-it-yourself-customer-portal-walkthrough\/\">Do it yourself #01 &#8211; Customer Portal Walkthrough (video tutorial)<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; visibility: hidden;\" title=\"&#8220;Do it yourself #01 &#8211; Customer Portal Walkthrough (video tutorial)&#8221; &#8212; SMSAPI Blog\" src=\"https:\/\/www.smsapi.com\/blog\/do-it-yourself-customer-portal-walkthrough\/embed\/#?secret=eBkwyq490f#?secret=eiMHlDiZIv\" data-secret=\"eiMHlDiZIv\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Preparations are now complete! Feel free to sign in to the messaging gateway. Now let\u2019s see what beginning to work with code in Python looks like.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The SMS library for Python applications<\/h2>\n\n\n\n<p>Python is liked for its simplicity. In this language, scripts can be clear and concise, but at the same time, advanced, and our library is no different. Simply ordering a single SMS to be sent looks like this:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>from smsapi.client import SmsApiPlClient\n\ntoken = '%SMSAPI_ACCESS_TOKEN%'\nclient = SmsApiPlClient(access_token=token)\nresult = client.sms.send(to='+44111222333', message='Hello world!')<\/code><\/pre>\n\n\n\n<p>But what exactly is going on here? First, a <strong>client instance is created<\/strong>, which manages the server connection, which is why you need the token. Formulating server queries belongs to API communication implementation, which the library takes care of for you.<\/p>\n\n\n\n<p class=\"language-python\">The client will be used every time because it contains objects responsible for all available functions. The following line simply refers to the&nbsp;<code>sms<\/code>&nbsp;object and&nbsp;<strong>orders the message to be sent<\/strong>&nbsp;to the given phone number. That\u2019s it!<\/p>\n\n\n\n<p class=\"language-python\">The&nbsp;<code>send<\/code>&nbsp;function can accept multiple parameters, which modify the sending differently. They are described in the&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/www.smsapi.com\/docs\/#2-single-sms\" target=\"_blank\">Documentation section on single SMS<\/a>, and you can read about a few of the most useful ones later herein.<\/p>\n\n\n\n<p class=\"language-python\">The returned value \u2018result\u2019 is an iterable object of the class&nbsp;<code>SmsSendResult<\/code>, which contains the&nbsp;<strong>parameters of the messages sent<\/strong>. Specifically, these are&nbsp;<code>count<\/code>, or the number of messages ordered for sending, and&nbsp;<code>results<\/code>&nbsp;\u2013 a&nbsp;list of dictionaries that correspond to the individual ones. A dictionary contains, for example:<\/p>\n\n\n\n<ul class=\"wp-block-list language-python\">\n<li><code>id<\/code>&nbsp;\u2013 a unique identifier of a message in our system;<\/li>\n\n\n\n<li><code>points<\/code>&nbsp;\u2013 cost, or the number of points deducted from your account;<\/li>\n\n\n\n<li><code>number<\/code>&nbsp;\u2013 recipient\u2019s number;<\/li>\n\n\n\n<li><code>status<\/code>&nbsp;\u2013 delivery status (<a rel=\"noreferrer noopener\" href=\"https:\/\/www.smsapi.com\/docs\/#17-delivery-statuses\" target=\"_blank\">list of statuses in the Documentation<\/a>)<\/li>\n\n\n\n<li><code>date_sent<\/code>&nbsp;\u2013 a time when the text was sent.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Optional parameters<\/h3>\n\n\n\n<p class=\"language-python\">The other fields of the&nbsp;<code>SmsSendResult<\/code>&nbsp;are optional and are present if the&nbsp;<code>details=1<\/code> parameter is used:<\/p>\n\n\n\n<ul class=\"wp-block-list language-python\">\n<li><code>message<\/code>&nbsp;\u2013 the content of the text message;<\/li>\n\n\n\n<li><code>length<\/code>&nbsp;\u2013 message length;<\/li>\n\n\n\n<li><code>parts<\/code>&nbsp;\u2013 how many parts it is divided into.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Tutorial: How to send SMS messages using SMS API<\/h2>\n\n\n\n<p>Your bulk SMS campaigns wouldn&#8217;t be possible without the ability to send messages to <strong>multiple recipients simultaneously<\/strong>. Nothing could be simpler! Instead of a single phone number, you need to provide a list:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>from smsapi.client import SmsApiPlClient\n\ntoken = '%SMSAPI_ACCESS_TOKEN%'\nclient = SmsApiPlClient(access_token=token)\nreceivers = &#91;'+44111222333', '+44444555666']\n\nresults = client.sms.send(to=receivers, message='Hello receivers!')\nfor result in results:\n\tprint(result)<\/code><\/pre>\n\n\n\n<p>As I\u2019ve mentioned, the object returned by the sending order is iterable, so the above for loop will write the details of individual messages on the console.&nbsp;<\/p>\n\n\n\n<p>In the Customer Portal, you\u2019ll find the ability to create <strong>recipient groups<\/strong>, which streamlines organising and sending SMS in a Python script. With another method, you can use this facility in the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>from smsapi.client import SmsApiPlClient\n\ntoken = '%SMSAPI_ACCESS_TOKEN%'\nclient = SmsApiPlClient(access_token=token)\n\n\nclient.sms.send_to_group(\n\tgroup='Group_name', \n\tmessage='Hello group!')<\/code><\/pre>\n\n\n\n<p class=\"language-python\">The&nbsp;<code>to<\/code>&nbsp;and&nbsp;<code>group<\/code>&nbsp;parameters are directed to the same server query. However, they are mutually exclusive, and using them simultaneously will cause an API error. This is why the Python library has separate functions for them.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Additional features of the Python SMS gateway<\/h2>\n\n\n\n<p>As I\u2019ve mentioned, messaging using Python can be modified by setting the delivery time, custom SMS sender name field, and custom content.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Schedule and send an SMS using Python<\/h3>\n\n\n\n<p>The right moment to deliver a text message is critical to drawing the recipient\u2019s attention, <span style=\"box-sizing: border-box; margin: 0px; padding: 0px;\">as<\/span> the article on\u00a0<strong>planning SMS sending<\/strong> explains.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-smsapi-blog wp-block-embed-smsapi-blog\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"p9g1IVKmDv\"><a href=\"https:\/\/www.smsapi.com\/blog\/how-to-schedule-sms-campaign\/\">How to Schedule an SMS Campaign Online?<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; visibility: hidden;\" title=\"&#8220;How to Schedule an SMS Campaign Online?&#8221; &#8212; SMSAPI Blog\" src=\"https:\/\/www.smsapi.com\/blog\/how-to-schedule-sms-campaign\/embed\/#?secret=4ieanzHkgw#?secret=p9g1IVKmDv\" data-secret=\"p9g1IVKmDv\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p class=\"language-python\">To make your campaign schedule happen, use the&nbsp;<code>date<\/code> parameter. <a href=\"https:\/\/www.smsapi.com\/blog\/how-to-schedule-sms-campaign\/\" target=\"_blank\" rel=\"noreferrer noopener\">Scheduling messages<\/a> to be sent on 1 December 2023 at 12:30 p.m. using a standard&nbsp;<code>datetime<\/code>&nbsp;object looks like this:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>from smsapi.client import SmsApiPlClient\nfrom datetime import datetime\n\ntoken = '%SMSAPI_ACCESS_TOKEN%'\nclient = SmsApiPlClient(access_token=token)\nsend_time = datetime(2023,12,1,12,30).isoformat()\n\nresponse = client.sms.send(\n\tto='+44111222333', \n\tmessage='Hello in the future!',\n\tdate=send_time)<\/code><\/pre>\n\n\n\n<p><strong>Accepted date formats<\/strong>&nbsp;are either Unix time (the number of seconds since 1 January 1970) or ISO 8601 (here: 2021-12-01T12:30:00+01:00).<\/p>\n\n\n\n<p class=\"language-python\">The example below uses the latter option, which is the\u00a0<code>isoformat()<\/code> method used. If your plans change, you can cancel a scheduled message. To do this, you need its unique ID, which you should note in advance. Continuing from the previous listing:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>smsIdToCancel = response.results&#91;0].id\nclient.sms.remove_scheduled(smsIdToCancel)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. SMS sender name instead of the phone number<\/h3>\n\n\n\n<p>Another vital feature is the <strong>SMS sender name<\/strong>. It is a short text that will be displayed instead of the actual phone numbers. It\u2019s the name of a company, brand, or product \u2013 something to let the recipient know who\u2019s texting instantly.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-smsapi-blog wp-block-embed-smsapi-blog\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"OQN3iVDZ09\"><a href=\"https:\/\/www.smsapi.com\/blog\/sender-id-how-to-send-branded-sms-company-name\/\">Sender ID \u2013 How to Send Branded SMS Messages with the Company Name Instead of a Number?<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; visibility: hidden;\" title=\"&#8220;Sender ID \u2013 How to Send Branded SMS Messages with the Company Name Instead of a Number?&#8221; &#8212; SMSAPI Blog\" src=\"https:\/\/www.smsapi.com\/blog\/sender-id-how-to-send-branded-sms-company-name\/embed\/#?secret=3AyFfnXBZD#?secret=OQN3iVDZ09\" data-secret=\"OQN3iVDZ09\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>This sort of \u201cpresentation\u201d can effectively <strong>draw the prospective customer&#8217;s attention<\/strong>. Especially since many people receive several notifications on their phones every day. You can manage SMS senders in the relevant&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/ssl.smsapi.com\/sms_settings\/sendernames\" target=\"_blank\">Sender names section in the Customer Panel<\/a>. To use one of them in Python code, use the&nbsp;from parameter:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>from smsapi.client import SmsApiPlClient\n\ntoken = '%SMSAPI_ACCESS_TOKEN%'\nclient = SmsApiPlClient(access_token=token)\n\nclient.sms.send(\n\tto='+44111222333', \n\tmessage='Hello world!',\n\tfrom='sender_name')<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Handling SMS sender names using Python<\/h4>\n\n\n\n<p class=\"language-python\">As mentioned, <strong>SMS sender names displayed instead of phone numbers<\/strong> allow you to present yourself to the recipient. You can manage them manually in the Customer Portal or use software code in a script. This is made possible by the&nbsp;<code>Sender<\/code> class, which provides the following methods:<\/p>\n\n\n\n<ul class=\"wp-block-list language-python\">\n<li><code>add(name)<\/code>&nbsp;\u2013 adds a new sender name;<\/li>\n\n\n\n<li><code>check(name)<\/code>\u00a0\u2013 returns an object with the status, name, and flag that says if the name is the default;<\/li>\n\n\n\n<li><code>remove(name)<\/code>&nbsp;\u2013 deletes a name;<\/li>\n\n\n\n<li><code>default(name)<\/code>&nbsp;\u2013 sets a name as default;<\/li>\n\n\n\n<li><code>list()<\/code>&nbsp;\u2013 returns a list of all names.<\/li>\n<\/ul>\n\n\n\n<p>This class is used in the same way as for sending text notifications. Here is what listing, adding a new name, setting it as default, and verifying it looks like:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>senderList = client.sender.list()\nfor senderName in senderList\n\tprint(senderName)\n\nclient.sender.add('NewSenderName')\nclient.sender.default('NewSenderName')\nclient.sender.check('NewSenderName')<\/code><\/pre>\n\n\n\n<p>Our employee verifies each new sender name to ensure that the name is not legally restricted. Add yours in the Customer Portal, sign the statement, and wait for us to authorize it. You&#8217;ll get a notification when it&#8217;s ready to use.<\/p>\n\n\n\n<p>Keep in mind that some countries limit the usage of this feature, so it&#8217;s best to contact your consultant to check all requirements. Other than that, there are technical limitations: maximum length and permitted characters. For example, the GSM standard allows up to eleven alphanumeric characters. You can read more on that in the technical FAQ below.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-smsapi-blog wp-block-embed-smsapi-blog\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"XJL2OXdLN6\"><a href=\"https:\/\/www.smsapi.com\/blog\/tech-support-sms-communication-faq\/\">Tech Support \u2013 Frequently Asked Questions (FAQ)<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; visibility: hidden;\" title=\"&#8220;Tech Support \u2013 Frequently Asked Questions (FAQ)&#8221; &#8212; SMSAPI Blog\" src=\"https:\/\/www.smsapi.com\/blog\/tech-support-sms-communication-faq\/embed\/#?secret=GaC3IuxbxE#?secret=XJL2OXdLN6\" data-secret=\"XJL2OXdLN6\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">3. Customising your SMS text messages<\/h3>\n\n\n\n<p>A third meaningful way to polish your SMS campaign, other than scheduling and sender IDs, is <strong>SMS customisation<\/strong>. It allows you to modify each text sent in bulk in a certain way. Individual recipients will then feel treated more personally.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-smsapi-blog wp-block-embed-smsapi-blog\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"NYwgW1GqJV\"><a href=\"https:\/\/www.smsapi.com\/blog\/basics-of-content-marketing-in-your-sms-campaign\/\">Basics of Content Marketing in SMS Campaigns<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; visibility: hidden;\" title=\"&#8220;Basics of Content Marketing in SMS Campaigns&#8221; &#8212; SMSAPI Blog\" src=\"https:\/\/www.smsapi.com\/blog\/basics-of-content-marketing-in-your-sms-campaign\/embed\/#?secret=V4gsOM1ZXw#?secret=NYwgW1GqJV\" data-secret=\"NYwgW1GqJV\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>In practice, it works using special character strings within a message. In individual SMS sent, these strings are replaced with the target text. This text, in turn, is drawn either from the contact database or from one of four custom parameters. See the latter case in the following simplified example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>receivers = &#91;'+44111222333', '+44444555666']\nclient.sms.send(\n\tto=receivers,\n\tmessage='Hello &#91;%1%], your number is &#91;%2%]!',\n\tparam1=&#91;'Adam', 'Bert']\n\tparam2=&#91;'11', '22'])<\/code><\/pre>\n\n\n\n<p>In consecutive messages, field [%1%] will be replaced with successive elements of the param1 list, and similarly for [%2%] and the param2 list. In the end, the messages will look like this:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Hello Adam, your number is 11!<\/p>\n\n\n\n<p>Hello Bert, your number is 22!<\/p>\n<\/blockquote>\n\n\n\n<p>You can write it more elegantly using the contact database. Use the following commands:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>receivers = &#91;'+44111222333', '+44444555666']\nclient.sms.send(\n\tto=receivers,\n\tmessage='Hello &#91;%imie%], your number is &#91;%1%]!',\n\tparam1=&#91;'11', '22'])<\/code><\/pre>\n\n\n\n<p>In this case, the [%imie%] parameter will be replaced with the name associated with the specific recipient in the contact database. Of course, there are more standard fields: family name, e-mail, city, and others, all described in the&nbsp;<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/www.smsapi.com\/docs\/#3-bulk-sms\">Documentation<\/a>.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. SMS content templates for the Python app<\/h3>\n\n\n\n<p><strong>SMS content templates<\/strong> make things somewhat easier when bulk messaging. They are message formulas that you can create in the\u00a0<a href=\"https:\/\/ssl.smsapi.com\/react\/sms-templates\" target=\"_blank\" rel=\"noreferrer noopener\">templates tab<\/a>. With them, you save time on writing identical notifications all over again.<\/p>\n\n\n\n<p>If you also use parameters in them, what you get is modifiable content. You receive greater convenience at work and customised messages to boot. Using a sample template could look like this:&nbsp;<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>PackageSentTemplate: \u201eHi [%imie%], your order has been sent! It will arrive within [%1%] business days.\u201d&nbsp;<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>receivers = &#91;'+44111222333', '+44444555666']\nclient.sms.send(\n\tto=receivers,\n\ttemplate='PackageSentTemplate',\n\tparam1='4')<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Handling SMS API errors<\/h2>\n\n\n\n<p>Expecting the best is worth being ready for the worst, which is why exception handling is essential. When a server error occurs in processing a particular query, information about this will be sent instead of the typical response. The library will process it and raise one of the exceptions.<\/p>\n\n\n\n<p class=\"language-python\">Five classes, all inheriting after&nbsp;<code>Exception<\/code>, have been defined. They have the following hierarchy:<\/p>\n\n\n\n<ul class=\"wp-block-list language-python\">\n<li><code>SmsApiException<\/code>&nbsp;\u2013 master class, allows handling all exceptions from the SMSAPI library. Contains the message and, optionally, a code.\n<ul class=\"wp-block-list\">\n<li><code>ClientException<\/code>&nbsp;\u2013 occurs if no authorisation data is provided.<\/li>\n\n\n\n<li><code>EndpointException<\/code>&nbsp;\u2013 exceptions reported from the server side \u2013 see the list below.\n<ul class=\"wp-block-list\">\n<li><code>SendException<\/code>&nbsp;\u2013 class assigned to the message send action. If incorrect phone numbers are used, contain a list of their details.<\/li>\n\n\n\n<li><code>ContactsException<\/code>&nbsp;\u2013 exceptions when handling the contact database using software code.&nbsp;<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Common error codes<\/h3>\n\n\n\n<p>You will find a detailed list of codes and their explanations in the documentation. The most commons are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>11<\/strong>&nbsp;\u2013 message too long or no message;<\/li>\n\n\n\n<li><strong>12<\/strong>&nbsp;\u2013 the message is divided into more parts than the limit;<\/li>\n\n\n\n<li><strong>13<\/strong>&nbsp;\u2013 incorrect recipient number;<\/li>\n\n\n\n<li><strong>14<\/strong>&nbsp;\u2013 incorrect sender name;<\/li>\n\n\n\n<li><strong>26<\/strong>&nbsp;\u2013 message subject is too long;<\/li>\n\n\n\n<li><strong>40<\/strong>&nbsp;\u2013 no group of this name exists;<\/li>\n\n\n\n<li><strong>101<\/strong>&nbsp;\u2013 incorrect authorisation token;<\/li>\n\n\n\n<li><strong>103<\/strong>&nbsp;\u2013 insufficient funds on the account to send the message;<\/li>\n\n\n\n<li><strong>8<\/strong>,&nbsp;<strong>201<\/strong>,&nbsp;<strong>999<\/strong>&nbsp;\u2013 internal server errors.&nbsp;<\/li>\n<\/ul>\n\n\n\n<p class=\"language-python\">The\u00a0<code>EndpointException<\/code>\u00a0exception occurs in two more situations. One is server communication errors. The code comes from the HTTP response, such as the familiar 404 error\u2014no resource found.<\/p>\n\n\n\n<p>The other possibility is using an unrecognised parameter name, most likely a typo. For example, write <a rel=\"noreferrer noopener nofollow\" href=\"https:\/\/en.wikipedia.org\/wiki\/The_Medium_Is_the_Massage\" target=\"_blank\">\u201emassage\u201d instead of \u201emessage\u201d<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">More options for handling mobile communication in Python<\/h2>\n\n\n\n<p>The capabilities of our library reach farther, though! Now that we\u2019re near the end, it\u2019s worth giving some of them the spotlight, such as SMS campaigns or contact database handling.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Sending MMS with Python<\/h3>\n\n\n\n<p><strong>Sending MMS messages<\/strong> is important. Handling them is very simple:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>response = client.mms.send(\n\tto='+44111222333', \n\tsmil=SMIL_contents,\n\tsubject='Hello multimedia')<\/code><\/pre>\n\n\n\n<p class=\"language-python\">The mandatory smil parameter is a correct multimedia message in the XML format SMIL 1.0, which you must prepare beforehand. An object of the same type as SMS is returned:&nbsp;<code>SmsSendResult<\/code>. Additionally, you can provide a&nbsp;text-based message subject<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Managing contact databases<\/h3>\n\n\n\n<p>I&#8217;ve already mentioned the contact database in this article. Having a list of prospective customers is crucial when running marketing actions.<\/p>\n\n\n\n<p>SMSAPI enables you to use a contact database to run an SMS campaign practically. You can manage such a database in the Customer Panel, but the Python library also allows you to do that.<\/p>\n\n\n\n<p class=\"language-python\">The&nbsp;<code>client<\/code>&nbsp;also has, in addition to the&nbsp;<code>sms<\/code>,&nbsp;<code>sender<\/code>&nbsp;and&nbsp;<code>mms<\/code>&nbsp;objects, the&nbsp;<code>contacts<\/code> instance. Therefore, the way you use the latter is similar to what I&#8217;ve shown you in the examples before. Creating a new contact, for example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>response = client.contacts.create_contact(\n\tfirst_name='Dwayne',\n\tlast_name='Hicks',\n\tphone_number='+44111222333',\n\temail='dhicks@example.com',\n\tgender='male'\n\tbirthday_date='2000-01-31')<\/code><\/pre>\n\n\n\n<p>The individual functions allow you to read, list, create, and delete individual contacts and whole groups. It is also possible to assign and remove a contact from a group.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The SMS API for Python: summary<\/h2>\n\n\n\n<p>And that\u2019s it for the essential operation of SMS sending in Python! However, this article still hasn\u2019t exhausted all the library&#8217;s capabilities. Feel free to test text and other features of SMSAPI: automated notification, receive messages, HLR lookup, and more!<\/p>\n\n\n\n<p>You can find more samples of how the SMS API gateway functions and features are used in the <a rel=\"noreferrer noopener nofollow\" href=\"https:\/\/github.com\/smsapi\/smsapi-python-client\/tree\/master\/examples\" target=\"_blank\">example folder<\/a> on GitHub. The repository also includes a <a rel=\"noreferrer noopener nofollow\" href=\"https:\/\/github.com\/smsapi\/smsapi-python-client\/tree\/master\/tests\" target=\"_blank\">folder of unit tests<\/a> written in the unittest framework. They can serve as a source of sample applications for different methods too.<\/p>\n\n\n\n<div class=\"wp-block-smsapi-blog-2021-infobox infobox infobox--info post-content--full-width\"><div class=\"infobox__icon\"><img decoding=\"async\" src=\"data:image\/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+CjxzdmcKICAgd2lkdGg9IjEwMCUiCiAgIGhlaWdodD0iMTAwJSIKICAgdmlld0JveD0iMCAwIDEyIDE2IgogICBjbGlwLXJ1bGU9ImV2ZW5vZGQiCiAgIHhtbDpzcGFjZT0icHJlc2VydmUiCiAgIHZlcnNpb249IjEuMSIKICAgaWQ9InN2ZzYzIgogICB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciCiAgIHhtbG5zOnN2Zz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPgogIDxwYXRoCiAgICBzdHlsZT0iZmlsbDogcmdiKDE3LCAxOTIsIDEyOCk7IGZpbGwtcnVsZTogbm9uemVybzsiCiAgIGQ9Im03LjUxNDA1LDExbC0zLDBjMCwtMyAxLjYsLTQgMi43LC00LjZjMC40LC0wLjIgMC43LC0wLjQgMC45LC0wLjZjMC41LC0wLjUgMC4zLC0xLjIgMC4yLC0xLjRjLTAuMywtMC43IC0xLC0xLjQgLTIuMywtMS40Yy0yLjEsMCAtMi41LDEuOSAtMi41LDIuM2wtMywtMC40YzAuMiwtMS43IDEuNywtNC45IDUuNSwtNC45YzIuMywwIDQuMywxLjMgNS4xLDMuMmMwLjcsMS43IDAuNCwzLjUgLTAuOCw0LjdjLTAuNSwwLjUgLTEuMSwwLjggLTEuNiwxLjFjLTAuOSwwLjUgLTEuMiwxIC0xLjIsMnoiCiAgIGlkPSJwYXRoNTkiIC8+CiAgPHBhdGgKICAgIHN0eWxlPSJmaWxsOiByZ2IoMTcsIDE5MiwgMTI4KTsgZmlsbC1ydWxlOiBub256ZXJvOyIKICAgZD0ibTgsMTRjMCwxLjEwNSAtMC44OTUsMiAtMiwyYy0xLjEwNSwwIC0yLC0wLjg5NSAtMiwtMmMwLC0xLjEwNSAwLjg5NSwtMiAyLC0yYzEuMTA1LDAgMiwwLjg5NSAyLDJ6IgogICBpZD0icGF0aDYxIiAvPgo8L3N2Zz4K\"\/><\/div><div class=\"infobox__title\"><h4>How to handle incoming messages?<\/h4><\/div><div class=\"infobox__content\">\n<p>Outbound messaging (sending SMS) is nice and all, but what about inbound (receiving messages)? Nothing is easier than using SMS API to receive text messages using Python or any other language.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-smsapi-blog wp-block-embed-smsapi-blog\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"Z1IvVmXU5F\"><a href=\"https:\/\/www.smsapi.com\/blog\/receiving-sms-online-developer-guide\/\">Receiving SMS Online \u2013 Developer Guide #02<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; visibility: hidden;\" title=\"&#8220;Receiving SMS Online \u2013 Developer Guide #02&#8221; &#8212; SMSAPI Blog\" src=\"https:\/\/www.smsapi.com\/blog\/receiving-sms-online-developer-guide\/embed\/#?secret=LLsECzECRT#?secret=Z1IvVmXU5F\" data-secret=\"Z1IvVmXU5F\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n<\/div><\/div>\n\n\n\n<p>And the&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/www.smsapi.com\/docs\/\" target=\"_blank\">API Documentation<\/a>, frequently referenced already, provides a source of knowledge on the meaning of individual parameters, statuses and codes. Now you have all the knowledge you need to integrate Python with SMS text messages. Off you go!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>And now for something completely different! The time has come to discuss another programming library for SMS campaigns. It is written in a peculiar but well-liked language. It\u2019s something for those who enjoy British humour and fast scripts but detest braces. Meet our Python library!<\/p>\n","protected":false},"author":21,"featured_media":5521,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[20,39],"class_list":["post-5519","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorial","tag-english","tag-api"],"_links":{"self":[{"href":"https:\/\/www.smsapi.com\/blog\/wp-json\/wp\/v2\/posts\/5519","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.smsapi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.smsapi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.smsapi.com\/blog\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.smsapi.com\/blog\/wp-json\/wp\/v2\/comments?post=5519"}],"version-history":[{"count":13,"href":"https:\/\/www.smsapi.com\/blog\/wp-json\/wp\/v2\/posts\/5519\/revisions"}],"predecessor-version":[{"id":6078,"href":"https:\/\/www.smsapi.com\/blog\/wp-json\/wp\/v2\/posts\/5519\/revisions\/6078"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.smsapi.com\/blog\/wp-json\/wp\/v2\/media\/5521"}],"wp:attachment":[{"href":"https:\/\/www.smsapi.com\/blog\/wp-json\/wp\/v2\/media?parent=5519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.smsapi.com\/blog\/wp-json\/wp\/v2\/categories?post=5519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.smsapi.com\/blog\/wp-json\/wp\/v2\/tags?post=5519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}