Anita Achu

Vonage Team Member

Anita Achu is a software engineer, passionate about technical writing. She loves backend technologies and using these technologies to solve problems. Also, she is an ardent believer that rap is the...

[Read more]
< Tutorial />

Deploying PythonAnywhere With the Messages API

Last updated on May 21, 2021

To make an application available for end-users to view and use, the application has to be deployed. Application or software deployment is one of the most important processes; the application becomes available to the intending users by this process.

Deployment involves the process of moving an application, updates, modules, and so on from the development server to production, where users can access this application via the internet or intranet.

Most software companies and developers deploy applications through a manual or automated process or, most times, both. The processes involved include software and package installation, testing, software release, and deployment for the deployment.

In this tutorial, we will be focusing on deploying Vonage Message API to production on PythonAnywhere. Vonage Message API integrates with SMS, MMS, and popular social chat apps—so users can communicate with your customers on whichever channel they choose. To access the API, you must create an account on the Vonage Developer Dashboard, where you are provided with credit to start testing the API.

First, we will be creating a simple application on PythonAnywhere that sends WhatsApp messages to users, which we will then deploy subsequently.


Vonage API Account

To complete this tutorial, you will need a Vonage API account. If you don’t have one already, you can sign up today and start building with free credit. Once you have an account, you can find your API Key and API Secret at the top of the Vonage API Dashboard.

This tutorial also uses a virtual phone number. To purchase one, go to Numbers > Buy Numbers and search for one that meets your needs.

Screenshot of new Meetings API session in progress
Start developing in minutes with free credits on us. No credit card required!

  • A PythonAnywhere account either free or paid account.
  • Python application installed on your system.
  • A basic knowledge of Python.

What Is PythonAnywhere?

PythonAnywhere is a cloud-based development and hosting environment for python applications.

This platform is used for hosting both python web applications and scripts. PythonAnywhere serves as a hosting environment; developers can code, run and host python applications and scripts; these python applications include Django, Flask, web2py, and Bottle applications.

It is free, beginner-friendly, and provides a good number of python extensions installed by default. Another unique feature of PythonAnywhere that it is explicitly built for python projects; therefore, it does not provide empty Linux and Ubuntu servers, unlike other cloud service providers.

Installing PythonAnywhere

To begin with, if you do not have a PythonAnywhere account, head over to and create a "Beginner account", this allows you to create a free account and will be sufficient to carry out this project. Fill the form and create your account.

You must choose a username wisely because the username will serve as your website domain name, i.e. Agree to the "Terms and Conditions" and then check your mail for a verification mail.

Creating a Bash Console

Once you log in to your account, you will have a dashboard with different sections. Click on the "consoles" section and choose your preferred console, between python and bash console; this tutorial will be using the bash console. Click on the "$ bash", and a bash console will open up in your browser window.

An example of the PythonAnywhere Bash Console
An example of the PythonAnywhere Bash Console

For this tutorial, we will be deploying a Whatsapp bot using the Vonage API. We will be creating a simple bot to send WhatsApp messages using the Flask framework. This bot will take the user's WhatsApp number and the Vonage number provided. We will be creating a flask file for this and making some imports but before we begin, I will briefly show you how to set up your Vonage account and add your number to the sandbox.

  • Flask is a lightweight python web application framework. Which we could be using to create our application
  • Vonage is an API library which offers flexible voice, messaging, video and Communication APIs

Vonage Messages API - WhatsApp Channel

Vonage Messages API allows users to send messages to a WhatsApp number via the WhatsApp channel. Firstly, to access Vonage API, create a Vonage API account on the Vonage Developer dashboard. Afterwards, you'll get free credits to use the API and an API Key and API Secret at the top of the Vonage Dashboard. Lastly, to use the WhatsApp Channel, you need to set up a sandbox.

Setting up Vonage Sandbox

To set up Vonage Message API sandbox, click on Messages and Dispatch, followed by Sandbox. The image below shows an example of the screen you'll see:

A screenshot of the Vonage dashboard showing various social channels available for the Messages API
A screenshot of the Vonage dashboard showing various social channels available for the Messages API

Under the WhatsApp section, add WhatsApp to the sandbox. Afterwards, this will be displayed on the screen.

An example of the sandbox dashboard page with WhatsApp enabled
An example of the sandbox dashboard page with WhatsApp enabled

You can either scan the QR code or click for an invite email. The link navigates to the WhatsApp application, containing a default message in the input field required by the Vonage API.

The image below shows an example of how the page is displayed:

A screenshot of a WhatsApp chat
A screenshot of a WhatsApp chat

In this tutorial, our default message is, "Join wing tacky". When you send this message, your number gets added to the whitelist for the Sandbox. You can add more numbers by sending the same text to the recipient using the WhatsApp number you wish to add.

Once this the process is complete, we'll then create our Flask application in a file called

import requests
import os
from os.path import join, dirname
from dotenv import load_dotenv
import vonage
from vonage import Client
from dotenv import load_dotenv

app = Flask(__name__)

dotenv_path = join(dirname(__file__), "../myapp.env")

client = vonage.Client(
    key=os.getenv("VONAGE_API_KEY"), secret=os.getenv("VONAGE_API_SECRET")

data= {
    "from": { 
        "type": "whatsapp",
        "number": "VONAGE NUMBER",    
    "to": { 
        "type": "whatsapp",
        "number": "YOUR NUMBER" },
    "message": {
      "content": {
        "type": "text",
        "text": "Thank you for registering with us. Welcome to Pharm services!"

url = ""
resp =, json=data, auth=(os.getenv("VONAGE_API_KEY"), os.getenv("VONAGE_API_SECRET")))

print (resp)

if __name__ == '__main__':

In the code above, we imported Vonage, our API library. It has already been installed in our terminal; we also imported a request to send a request to our POST request to call our environment variable and Client to send messages through our Message API.

N.B: Add Vonage number and your number in the required section of the above and do not add '+' or '000' to the number. Just your country code, for example, "234817542367".

Once this is done successfully, run python on your terminal. You will receive a 202 response.

For the purpose of this tutorial, we are running this project using python3. To ensure your using this version of python run the following command to check the version of python you are running on:

python --version

If your version output is Python 2. Please update any commands you run with python3.

Adding a Requirements.txt File

A requirements.txt which contains all the python packages is required to install the application on PythonAnywhere. Here is our requirements.txt


In your PythonAnywhere bash console, we will be using the nano text editor. The next step is adding the and requirements.txt to our PythonAnywhere account; the best step is to copy and paste the code to the clipboard. Then open the bash console using your text editor:


This line of code takes you to the nano editor, paste the code from []( file. Afterwards, exit the editor by entering Ctrl-X and 'Yes' to save changes.

Repeat the same process for your requirements.txt

nano requirements.txt

With the above steps, you have moved the code from your local machine to PythonAnywhere for it to be hosted.

Creating a virtual environment

Firstly, we begin by creating a virtual environment. Ensure the virtual environment is installed on your system; to do this, run the python package manager - **pip** *command.*

python -m pip install --user virtualenv

Once virtual env is installed, proceed to creating and activating a virtual environment.

virtualenv env

Activate the virtual environment by running the following commands:

source env/bin/activate

Once the virtual environment is running, we proceed to install the required packages for this project as seen in our requirements.txt file:

pip install -r requirements.txt

Now let's create a web application on PythonAnywhere.

To begin, click on "web" in the navigation bar. Then "Add a new web app".

Except you have a paid PythonAnywhere account, where you can use a custom domain name, else PythonAnywhere will create a domain for you, using this format "your".

Click the "Next" button to proceed.

An example of the webapp running
An example of the webapp running

Again, click "Next". On the next page, select your preferred python web framework for this project. Though our application is written in Flask, we will be using the Manual configuration because it works for all WSGI compatible framework.

A screenshot showing the creation of a new Python web app.
A screenshot showing the creation of a new Python web app.

Click on 'Manual configuration' then 'Next'. On the next page, select a python version. I'll be using "Python 3.8", then click "**Next".**

The following page contains information on Manual Configuration. Click "Next" to create a WSGI file with a simple "Hello World" application.

Afterwards, the next page is the last configuration page containing details of your web application. Your simple "Hello World" application has been created.

We can now view this application, open a new tab on your web browser. Add the following URL: "". This URL will allow us to check whether the application deployment was successful. The page you're presented with will look similar to the example below:

A screenshot showing a page served by PythonAnywhere
A screenshot showing a page served by PythonAnywhere

Adding Virtualenv Configuration

In the PythonAnywhere page, go to the "Virtualenv" section, where we will configure the location of the virtual environment. Click on "Enter path to a virtualenv" and add /home/yourusername/env i.e., if your username is MessageAPI. You should enter /home/messageapi/env

Configuration: WSGI configuration

On the configuration page, there is a section, "Code". Under Code, the WSGI configuration file has the path of the WSGI configuration file added by PythonAnywhere.

A screenshot of the WSGI configuration
A screenshot of the WSGI configuration

Take note of the WSGI configuration file, or copy it out the path to the clipboard if you prefer. Click on your bash console under the "Your consoles" section after clicking "Consoles" in the navigation bar. The bash session is what you'll see next.

Open the WSGI configuration file in a text editor and add the copied path :

nano /var/www/

PythonAnywhere will generate a long template file that contains some instructions. Still, for this tutorial, you'll need to remove the contents of this file and replace them with the following Python code:

from whatsapp import app as application

At the beginning of each line press Ctrl+k to delete it.

This line renames the "app*"* variable specified in to "application" and imports it. The WSGI application exported by the Flask system stores this variable. PythonAnywhere will use the full name "application" to access it.

Reload application

Once you've made these changes, save the WSGI configuration file. Then, return to "Web" on the PythonAnywhere dashboard section and click on the "Reload" button to reload the web application with the final configuration.

Connecting URL

On the Vonage "Message API sandbox" page, scroll down to "Webhooks". In this section, enter the webhook's URL. This URL consists of the PythonAnywhere URL followed by the /whatsapp route to the webhook. The URL will be in the format in general. As shown below:

Creating the webhooks to handle WhatsApp messages
Creating the webhooks to handle WhatsApp messages

Save the webhooks. Now lets run our application

Chatting with BOT

With this step, you can create an application that sends customers a message; once they register on your site, it sends a request to the bot's endpoint running on PythonAnywhere.

Here's an example:

A screenshot of the activation of a WhatsApp Bot
A screenshot of the activation of a WhatsApp Bot


In this tutorial, we learned how to deploy a Vonage Message API to production on PythonAnywhere. I hope you find these guidelines helpful. Asides from the Message API, Vonage has several products you could use in your application.

If you wish to keep using PythonAnywhere, you can upgrade to a paid account with no limitations. PythonAnywhere, in particular, allows free applications to be manually confirmed every three months.

You can also deploy Vonage application on Heroku or any other deployment platform. Happy coding!