Receive an SMS Message with Python SDK
Note:
Before you can get started, you need the following already set up:
- Set all SMS API configuration settings.
- Python and a familiarity with how to create a new app.
- PIP (package installer for Python) and a familiarity with how to install Python modules.
- Flask and a familiarity with how to set up a Flask environment and app.
- ngrok. You'll use ngrok to open a tunnel to your local server.
Learn how to handle incoming SMS messages in a Python application with the Sinch Python SDK.
Steps:
Install the SDK
The easiest way to install the SDK is using pip
:
- Open a command prompt or terminal to the local repository folder.
- Execute the following command:
pip install sinch
Set up your environment
Now you can start setting up your environment.
Create a new folder where you want your app project and open a command prompt to that location. Create a new environment with the following command:
py -3 -m venv venv
Activate the environment using the following command:
venv/Scripts/activate
Install your dependencies
We will be using Flask to create a lightweight webserver that will listen for requests from the Sinch servers to handle incoming calls. Additionally, we'll be using therequests
module to make HTTP requests.Use the following commands to install the Flask
and requests
modules:pip install Flask
pip install requests
Set up your Python application
Create a new file namedapp.py
and paste the provided "handle-incoming.py" code into the file.Modify your application
The code provided includes placeholder parameters. You'll need to update the parameters detailed in the following subsections with your values.
Initialize the client
Before initializing a client using this SDK, you'll need three pieces of information:
- Your Project ID
- An access key ID
- An access key Secret
Note:
Ensure that you save the file.
Start your web server and set up a tunnel
- Start the server by executing the following command:
flask run
By default, your web server is started on port 5000.
- Now you need to open a tunnel to the server you just set up. We are using ngrok for this. If you don't have ngrok installed already you can install it with the following command:
npm install ngrok -g
- In the terminal or command prompt, enter:
ngrok http 5000
You will see a screen like the following.
- On the highlighed "Forwarding" line, copy the address ending in
.ngrok.io
.
Configure your Callback URL
Next, configure a Callback URL for your Sinch account.
- On your Sinch Customer Dashboard-> Service APIs click on the service plan ID link.
- In “Callback URL” click Add Callback URL and paste in the HTTPS address referred to in the previous section.
Test the application
Now that your server is running and your webhook is configured, you can test the application.
From your messaging platform, send your SMS app a message. You will receive a message back in response on your mobile handset.
Additional resources
- Visit our API specification to test more endpoints.