From function to API in minutes:

Transforming a Python function into an API endpoint and deploying it to Heroku using Asymmetric!

Transform and deploy in minutes using Asymmetric, no previous API knowledge required.

Daniel Leal
3 min readOct 29, 2020

--

It is only normal to want to put in production that project that you worked so hard for. Maybe, you are thinking of exposing that awesome ML model you built as a web API for other developers to use it. Maybe you just want to use it yourself in your own web application or script. You have the predict function… And now what? Well, lucky you, with asymmetric, you can add one line on top of your function and that’s it! An API endpoint! Let’s see how that would work…

Creating the API endpoint

Imagine that you have your amazing predictor function named predictor that lives inside a file named predict.py. In the example, the predict method receives two numbers and returns the sum of those numbers, with pure machine learning thechniques:

To transform that into an API, install Asymmetric using pip install asymmetric and then decorate your predict function!

Notice that we imported the asymmetric object from the asymmetric library and then we decorated our function with it! Now, you can start your API with the following command:

That will freeze your terminal and start the API, which can be located at http://127.0.0.1:8000/predict. Notice that we “targeted” our predict.py with the asymmetric run command, but didn’t write the .py part of the file name. You can test your API by using the following snippet (you should see an 8 printed):

Uploading the API to Heroku

Now that your function has been transformed into an API, all that’s left is to use Heroku to expose that API to the rest of the world! Make sure that your predict.py file is commited to git before continuing.

Now, all that’s left is to create some files that Heroku needs (in order to start the app) and to run some commands. Let’s start! You need to create a file named requirements.txt that includes all the libraries that you will need (asymmetric included). In the case of the example, the file would look like this:

You also need a file named Procfile to tell Heroku how to start the app! The Procfile should look like this:

Here, we told Heroku to start asymmetric targeting our file predict.py and using the host 0.0.0.0 and the port in the environmental variable named $PORT (Heroku needs these to be passed to asymmetric, don’t worry too much about them). Finally, commit the new files to git and create a new Heroku app using heroku new. This will create a new application, and it should show you the URL, something like the following:

Now, all that’s left to do is to run git push heroku master. This will push your API to Heroku. After that, you can use the following snippet to use your function from Heroku:

And that’s it! Now anyone can access your fantastic function! Asymmetric has a ton of other nice features, so go check it out!

--

--