Messaging • Last Updated 10/22/2024

Receiving a Text Message with the Telnyx SMS Python SDK

Messaging engineer, Badal Moradia walks you through how to set up your Telnyx account so you can start receiving text messages.

Michael Bratschi - co-host of Telnyx SMS regulations webinar.

By Michael Bratschi

Python SDK feature image

Messaging engineer, Badal Moradia walks you through how to set up your Telnyx account so you can start receiving text messages using our SMS Python SDK. Watch the video below:

Learn More

Read the full guide here

Step 1: Make sure you have Flask and the Telnyx SDK installed, as well as the requests library. More information can be found on the full guide link above.

Step 2: Create a Flask application to accept webhooks. Create a new file called receive.py and copy the code below into the file. We'll go through the steps one by one.

import os
from flask import Flask, request

Make the code from the Flask library available in the receive.py file.

app = Flask(__name__)

Create a new instance of the Flask class, passing in the module attribute __name__. as the name of the Flask application.

@app.route('/webhooks', methods=['POST'])

Next, you’ll define a route using a decorator function. We define the path as /webhooks and specify that the path will respond to the HTTP POST method. This route will accept webhooks from Telnyx when your Telnyx number receives an SMS or MMS.

def webhooks():

Finally, the print function will print the body of the request so you can see it in your terminal.

print(body)

The last lines in the file check to see if the name of the module is __main__, and if it is, starts the application on the port 5000.

if __name__ == "__main__":
    app.run(port=5000)

All together, it should look like this:

import os
from flask import Flask, request

app = Flask(__name__)

@app.route('/webhooks', methods=['POST'])
def webhooks():
    body = request.json

    print(body)

    return '', 200

if __name__ == "__main__":
    app.run(port=5000)

Step 3: In your Telnyx Mission Control Portal, go to the “Auth” link in the left nav and create a new v2 API key and paste in for “YOUR_API_KEY”.

Step 4: In the “Numbers” tab in Mission Control, make sure you have a number available associated with a messaging profile.

How to purchase a phone number

How to create a messaging profile

Step 5: From your terminal, run the below:

python receive.py

You should see something like the following: ```bash

  • Serving Flask app "receive" (lazy loading)
  • Environment: production WARNING: Do not use the development server in a production environment. Use a production WSGI server instead.
  • Debug mode: off
  • Running on (Press CTRL+C to quit) ``` This means our server is listening on port 5000. You're almost ready to receive an SMS or MMS.

Step 6: Set up ngrok or a similar tunneling service that enables you to receive webhooks from inbound messages to your newly created application on your local webserver.

Step 7: Go to your terminal, and set up your secure tunnel by typing “./ngrok http 8000”. Copy the forwarding domain from ngrok.

Step 8: Head back to your Mission Control Portal, click on “Messaging” and go to your Messaging Profile. Click the edit [✎] icon and paste the forwarding domain you copied under “Send a webhook to this URL:” with one change: include “/webhooks” at the end of the domain to direct the request to the webhook endpoint in your Flask app.

Receiving Messages Webhook URL

Step 9: Head back to your code and change your Flask app “port” from 5000 to 8000.

Run your code, and you should be able to see your Flask app running on localhost:8000. Test by sending a message to the number you associated with your Messaging Profile from your phone, and you should be able to see the event printed on your terminal!

Questions about your set up? We’re here to help! Our support team is available in Mission Control 24/7.


Take advantage of insights from 1 billion+ messages with our '2023 Guide to SMS and MMS'. Download your copy to learn from Telnyx experts and shape your ideal SMS/MMS strategy.


Share on Social
Jump to:Learn More

Sign up for emails of our latest articles and news

Related articles

Sign up and start building.