Header Ads

How to Deploy Python Swagger API on Google Colab

Python Swagger API is a quick way of building server-less RESTful web APIs. However, deployment or execution of these APIs is usually complex even for testing or staging purpose. Normally, docker solution is recommended for the deployment of the Swagger APIs and in some cases locally deployed windows solutions. Sometimes it becomes difficult to follow through all the complex docker based or locally deployed solutions even just for testing purpose. There is however another solution, a little simpler, but, only recommended for staging or testing purpose.

Today, I shall be demonstrating a much simpler solution for deploying python swagger API on google colab for testing or staging purpose only. You don't need to install any tool on your machine, just a gmail account and access to google colab product.

 


Prerequisites:

Before proceeding any further in this article, following are some of the many prerequisites for this article:
  1. Gmail Account.
  2. Access to Google Colab Product.
  3. Python Code Implementation for Swagger API  .
  4. Knowledge of Python.

The running working google colab solution file is provided with a sample swagger API code. 

Download Now!


Let's begin now.

1) First you need to have gmail account (if not already have), then access google colab product and create a new notebook.



 
2) Add your code for swagger API into a new notebook cell and make sure that your code is saved in an external python code file upon execution.

 
3) Now at the top most within the notebook create a cell and add following library installation line of code i.e.

!npm install localtunnel

I am going to use network tunneling technique to allow public access to my custom created RESTful web APIs. Know that normally custom created APIs deployed or execute on local server machine for testing purpose and they can easily be exposed to public access temporarily through network tunneling technique for testing purpose only. Google colab itself is a remote server, so, anything that is created on it that involves URL sharing or exposure cannot be access without network tunneling technique because the default  behavior is deployment over local server. So, when google colab act as local server, the local hosted URL needs to temporary expose as public for testing purpose. So, here I am using python "localtunnel"  library to serve this purpose.


4) Next step is to get public IP of google colab notebook because it is required in some cases by "localtunnel" library to bypass the access password requirement. For this purpose use following line of code in a new notebook cell after your swagger API code i.e.

!wget -q -O - ipv4.icanhazip.com

5) Then add following line of code in a new cell at the end of the notebook i.e. 

!python my_api.py & npx localtunnel

The above line of code not only just starts the server for your swagger API, but also expose the localhost URL to a randomly generated public URL, that you can use to test your RESTful web APIs. "my_api.py" is the file name where I have saved my swagger API code.
 

 
6) Time to execute the deployed Swagger RESTful APIs. Use any RESTED client and make sure to add the necessary headers, especially to bypass the localtunnel library password requirement as shown below. I have created a hello endpoint that simply returns JSON based "Hello World" message. Here's how the execution looks like in the RESTED client. i.e.



Conclusion

In this article, you will learn to deploy python swagger API on google colab for testing or staging purpose only. You don't need to install any tool on your machine, just a gmail account and access to google colab product. You will learn to generate public IP of your google colab notebook. You will also learn about the network tunneling technique to temporary expose localhost server publicly for testing purpose only. Finally, you will learn to deploy, execute and test out your swagger RESTful web API on any RESTED client with the help of google colab notebook as server without the need to create any complex docker or local deployments.

Related Articles