Guides & Tutorials6 min read

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
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

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 and copy the code below into the file. We'll go through the steps one by one.
1import os
2from flask import Flask, request
Make the code from the Flask library available in the file.
1app = Flask(__name__)
Create a new instance of the Flask class, passing in the module attribute _name_. as the name of the Flask application.
1@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.
1def webhooks():
Finally, the print function will print the body of the request so you can see it in your terminal.
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.
1if __name__ == "__main__":
All together, it should look like this:
1import os
2from flask import Flask, request
4app = Flask(__name__)
6@app.route('/webhooks', methods=['POST'])
7def webhooks():
8    body = request.json
10    print(body)
12    return '', 200
14if __name__ == "__main__":
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.
Step 5: From your terminal, run the below:
You should see something like the following:
2* Serving Flask app "receive" (lazy loading)
3* Environment: production
4  WARNING: Do not use the development server in a production environment.
5  Use a production WSGI server instead.
6* Debug mode: off
7* 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.
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.
Share on Social

Get Started for Free!

Create a free account to set up voice, messaging, IoT, video & more.