Writing Python Api with Flask RESTful
Tags: python, flask, restfulapi
Published: 2020-01-09 22:20:15

I had been working on a python application, Now it came to the section where I had to write an API for my application. it was such a headache because I was a newbie on Api's and I had little understanding of APIs. So I spend so much time online reading about APIs and how to integrate one to my application. it all looked like rocket science from most of the articles I went through. that is why I decided to come up with this article. it will take you through implementing a simple API, Later I will come up with a more detailed article though this code will take you through most of the basics of flask RESTful API and how to create one successfully. 
I have put comments on my code for clarification and detailed walkthrough.

everything that starts with # is a comment, I believe if you are reading the article you have an understanding of python coding. hope you will like this article.
The concept of using classes with python has been covered in this article to help you have full understanding. I will write a more advanced article for now this 
article is for absolute beginners 
## what the application below does

1. Receives json file and extracts the json values that are then used for computation 
2. After computation, the application creates a JSON file. this request can be done through postman which enable to make api calls and request.

Before i begin this article it would be nice if share how the file structure looks on my pycharm IDE.
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/vrebue0mef8b4w2mbiau.png)

1. This is the main file for your flask


```
from flask import Flask, jsonify, request
#importing flask restful extenstion
from flask_restful import Resource, Api

#importing a class from a package called ntuity in the application

from ntuity.ntuityprac import  #importing a class from a package called ntuity

app = Flask(__name__)
*api=Api(app)


##class Kamau(Resource):

    def post(self):
        jsonval=request.get_json()
        content=jsonval.get("number")
        sum=10*content
        bestie=["welcome", 525, "kiza"]
        return jsonify({"sum":sum,"kenya":bestie})

class Kithome(Resource):
    p=NtuityPrac() #class object
    getval=p.get_square
    getsum=p.get_sum

    
    def post(self):
        jsonval = request.get_json()
        content = jsonval.get("number")
        get_add=self.getsum(content)
        get_sum=self.getval(content)
        return jsonify({"square":get_sum, "sum":get_add})


api.add_resource(Kamau,'/kama')
api.add_resource(Kithome,'/kithome')
if __name__ == '__main__':
    app.run()

below is the imported class from a package has two methods 

1. get_square()
2. get_sum()


class NtuityPrac:
#this class has two methods get square and get sum
    def get_square(self,num1):
        return num1*num1

    def get_sum(self, num1):
        return num1+num1
```

This a post man json request to kithome endpoint
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/ipor9i038s0mdrbvuwyn.png)

The output of this request is json file with sum and square
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/30q2ngadzu7leqljsqjj.png)

I hope you have liked this article.

I am Kali Kimanzi a Masters student Energy Informatics at University of Applied Sciences Upper Austria, I am also a software developer at NTUITY a brand of neoom group gmbh Wien | Freistadt. I take interest in Data sciences using python and i use python libraries like pandas, numpy and Matplotlib. I specialize in Python, PHP/Laravel, Java. 

**Social media links**

Kali kimanzi @ Facebook: [Kali Kimanzi  ](https://www.facebook.com/Kalikimcel)
Kali  Kimanzi @  Linkedin: [Kali Kimanzi](http://linkedin.com/in/kalikimanzi)
Kali  Kimanzi @ instagram : [Kali Kimanzi ](https://www.instagram.com/kalikimanzi/)
Kali Kimanzi  GitHub: [Kali Kimanzi  ](https://github.com/kalikim)
kali kimanzi Twitter : [ Kali Kimanzi ](https://twitter.com/karliekimcel)