TUTORIAL

Send SMS with Python - 3 Simple Steps to Get Started with 46elks

If you know how to program in Python you can send text message with 46elks. Just follow these three simple steps to get started and in a few minutes you will receive an SMS that you have sent from Python.

Swedish: Är du i Sverige så rekommenderar vi att du läser den svenska versionen av denna guide.

1. Create a user

To send an SMS with 46elks you first have to create an account. Go to the registration page and fill in your details. You will then receive an automatic phone call to verify your new account.

To start using 46elks to send a text messages you will need to purchase some credits. The minimum purchase is 10 EUR which is more than enough to get started.

2. Username and password

Once you have an account it’s time to login to the 46elks dashboard. This is where you can see all your important account information - like the username and password you need to send an SMS. The username is called API username and the password API password.

Tip: Click on the blurred text under API password to see your own password.

Every time you do something with 46elks, like sending an SMS, you have to provide your username and password via Basic HTTP Authentication. When you are using Python you need to set Basic Authentication username and password in the Authorization HTTP Header (see the code examples below).

3. Send an SMS

Finally, it’s time to write some Python. Your code needs to be able to do the following:

There are many ways to implement this in Python. Here are examples of how it can be done with the Requests library (for Python 2 & 3), with Python 2.x or Python 3.x (both without any extra libraries).

Requests Python 2 Python 3

import requests

API_USERNAME = "YOUR USERNAME" # Specify your API username
API_PASSWORD = "YOUR PASSWORD" # Specify your API password

# Make a request to the 46elks API
response = requests.post(
    "https://api.46elks.com/a1/sms",
    auth = (API_USERNAME, API_PASSWORD),
    data = {
        "from": "Python",  # The sender of the SMS, up to 11 characters
        "to": "YOUR PHONE NUMBER", # The number that will receive the text message
        "message": "It is easy to send SMS with 46elks" # The content of the text message
    }
)

# Check that the request was succesful
if response.status_code == 200:
    print("Sent! The API responded:")
    print(response.text)
else:
    # Error handling code...
    print("Error: %s" % response.text)
from urllib import urlencode
from urllib2 import urlopen, Request, HTTPError
from base64 import b64encode
import json

API_USERNAME = "YOUR USERNAME" # Specify your API username
API_PASSWORD = "YOUR PASSWORD" # Specify your API password

headers = {}
headers['Authorization'] = "Basic " + b64encode(API_USERNAME + ':' + API_PASSWORD)

data = urlencode({
    "from": "Python",  # The sender of the SMS, up to 11 characters
    "to": "YOUR PHONE NUMBER", # The number that will receive the text message
    "message": "It is easy to send SMS with 46elks" # The content of the text message
})

try: 
    req = Request("https://api.46elks.com/a1/sms", data, headers)
    http_res = urlopen(req)
    json_res = json.load(http_res)
    print "SMS sent! Here's what the API responded:"
    print json.dumps(json_res, indent=2)
except HTTPError, e:
    print "Error %d. The API responded with: " % e.code
    print e.read()
from urllib.parse import urlencode
from urllib.request import urlopen, Request, HTTPError
from base64 import b64encode
import json

API_USERNAME = "YOUR USERNAME" # Specify your API username
API_PASSWORD = "YOUR PASSWORD" # Specify your API password
API_AUTH = API_USERNAME + ":" + API_PASSWORD

params = {
    "from": "Python",  # The sender of the SMS, up to 11 characters
    "to": "YOUR PHONE NUMBER", # The number that will receive the text message
    "message": "It is easy to send SMS with 46elks" # The content of the text message
}

# Encode the parameters
data = urlencode(params).encode("utf-8")

# Set the headers so the request can be authenticated
headers = {}
headers["Authorization"] = b"Basic " + b64encode(API_AUTH.encode())
headers["User-Agent"] = b"46elks.se/tutorials/send-sms-with-python-3-x"

# Make the request
try: 
    req = Request("https://api.46elks.com/a1/sms", data, headers)
    http_res = urlopen(req)
    json_res = json.load(http_res)
    print("SMS sent! Here's what the API responded:")
    print(json.dumps(json_res, indent=2))
except HTTPError as e:
    error = e.read().decode("utf-8")
    print("Error %d. The API responded with: " % e.code)
    print(error)

Tip: The phone number to send to should be formatted with country code and without spaces, dashes or parentheses. For example +468123123 and not 08-123123 or +46 (8) 123 123.

Use the Python example by creating a send.py file, copy/paste the example code above to the file, add your username, password and phone number to the code. Then execute it by using the command python send.py and your phone will buzz. Congratulations, you’ve just sent an SMS from Python.

Next step

You’ve just sent a text message with a text sender (i.e. not from a phone number). By doing this you can use SMS to remind customers of reservations, send confirmations of new orders, etc.

To send an SMS is the easiest way to get started with 46elks, but you can do so much more. For example you can receive SMS, automatically respond to an incoming SMS or handle voice calls. For this and more there are a load of Python code examples at GitHub.

If you have any questions then get in touch, we love talking to our users!