This sample project demonstrates how to use Twilio APIs in a Python web application. Once the app is up and running, check out the home page to see which demos you can run. You'll find examples for Chat, Video, Sync, and more.
Let's get started!
To run the application, you'll need to gather your Twilio account credentials and configure them
in a file named .env
. To create this file from an example template, do the following in your
Terminal.
cp .env.example .env
Open .env
in your favorite text editor and configure the following values.
Every sample in the demo requires some basic credentials from your Twilio account. Configure these first.
Config Value | Description |
---|---|
TWILIO_ACCOUNT_SID |
Your primary Twilio account identifier - find this in the console here. |
TWILIO_API_KEY |
Used to authenticate - generate one here. |
TWILIO_API_SECRET |
Used to authenticate - just like the above, you'll get one here. |
When you generate an API key pair at the URLs above, your API Secret will only be shown once -
make sure to save this information in a secure location, or possibly your ~/.bash_profile
.
Depending on which demos you'd like to run, you'll need to configure a few more values in your
.env
file.
Config Value | Product Demo | Description |
---|---|---|
TWILIO_CHAT_SERVICE_SID |
Chat | Like a database for your Chat data - generate one in the console here |
TWILIO_SYNC_SERVICE_SID |
Sync (Preview) | Like a database for your Sync data - generate one with the curl command below. |
TWILIO_NOTIFICATION_SERVICE_SID |
Notify (Preview) | You will need to create a Notify service - generate one here |
During the Sync developer preview, you will need to generate Sync service instances via API until the Console GUI is available. Using the API key pair you generated above, generate a service instance via REST API with this curl command:
curl -X POST https://preview.twilio.com/Sync/Services \
-d 'FriendlyName=MySyncServiceInstance' \
-u 'SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:your_api_secret'
You will need to create a Notify Service through the Twilio Console, and add at least one credential on the Mobile Push Credential screen (such as Apple Push Notification Service or Firebase Cloud Messaging for Android) to send notifications using Notify.
This application uses the lightweight Flask Framework.
We need to set up your Python environment. Install virtualenv
via pip
:
pip install virtualenv
Next, we need to install our dependencies:
virtualenv venv
source venv/bin/activate
pip install -r requirements.txt
Now we should be all set! Run the application using the python
command.
python app.py
Your application should now be running at http://localhost:5000. When you're finished, deactivate your virtual environment using deactivate
.
If you are going to connect to this SDK Starter Kit with a mobile app (and you should try it out!), your phone won't be able to access localhost directly. You'll need to create a publicly accessible URL using a tool like ngrok to send HTTP/HTTPS traffic to a server running on your localhost. Use HTTPS to make web connections that retrieve a Twilio access token.
ngrok http 5000
MIT