# Receive an SMS Message with Python SDK Note: Before you can get started, you need the following already set up: - - [Python](https://www.python.org/) and a familiarity with how to create a new app. - [PIP (package installer for Python)](https://pypi.org/project/pip/) and a familiarity with how to install Python modules. - [Flask](https://flask.palletsprojects.com/en/2.3.x/installation/) and a familiarity with how to set up a Flask environment and app. - [ngrok](https://ngrok.com/). 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: 1. [Install](#install-the-sdk) the Python SDK 2. [Set up](#set-up-your-environment) your environment 3. [Set up](#set-up-your-python-application) your Python application 4. [Configure](#configure-your-callback-url) your callback URL 5. [Test](#test-the-application) your application ## Install the SDK The easiest way to install the SDK is using [`pip`](https://pypi.org/project/pip/): 1. Open a command prompt or terminal to the local repository folder. 2. Execute the following command: ```shell 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: ```shell py -3 -m venv venv ``` Activate the environment using the following command: ```shell 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 the `requests` module to make HTTP requests. Use the following commands to install the `Flask` and `requests` modules: ```shell pip install Flask pip install requests ``` ## Set up your Python application Create a new file named `app.py` and paste the provided "handle-incoming.py" code into the file. handle-incoming.py import os import sinch from flask import Flask, request app = Flask(__name__) sinch_client = sinch.SinchClient( key_id="YOUR_KEY_ID", key_secret="YOUR_KEY_SECRET", project_id="YOUR_PROJECT_ID" ) @app.route('/', methods=['POST']) def result(): inbound_message = request.get_json() print(inbound_message) if all(key in inbound_message for key in ["body", "to", "from"]): sinch_client.sms.batches.send( body="Thank you for using the Sinch SDK. You sent: " + inbound_message["body"], delivery_report="none", to=[inbound_message["from"]], from_=inbound_message["to"] ) return "Inbound message received", 200 else: return "Invalid data", 400 ### 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 These values can be found on the [Access Keys](https://dashboard.sinch.com/settings/access-keys) page of the Sinch Build Dashboard. You can also [create new access key IDs and Secrets](https://community.sinch.com/t5/Conversation-API/How-to-get-your-access-key-for-Conversation-API/ta-p/8120), if required. Note If you have trouble accessing the above link, ensure that you have gained access to the [Conversation API](https://dashboard.sinch.com/convapi/overview) by accepting the corresponding terms and conditions. Ensure that you save the file. ### Start your web server and set up a tunnel 1. Start the server by executing the following command: ```shell flask run ``` By default, your web server is started on port 5000. 2. Now you need to open a tunnel to the server you just set up. We are using [ngrok](https://ngrok.com/) for this. If you don't have ngrok installed already you can install it with the following command: ```shell npm install ngrok -g ``` 3. In the terminal or command prompt, enter: ```shell ngrok http 5000 ``` You will see a screen like the following. ![ngrok screenshot](/assets/ngrok.135751fc8aa7c936230a2a88bff2263644a1d1737263acc00294006a34b0f60b.c6cba068.png) 4. 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. 1. On your [Sinch Build Dashboard](https://dashboard.sinch.com/sms/api/rest)-> Service APIs click on the service plan ID link. 2. 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](/docs/sms/api-reference/) to test more endpoints.