{"id":844,"date":"2018-10-18T16:09:16","date_gmt":"2018-10-18T16:09:16","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=844"},"modified":"2018-10-21T00:29:48","modified_gmt":"2018-10-21T00:29:48","slug":"creating-rest-api-in-python","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/18\/creating-rest-api-in-python\/","title":{"rendered":"Creating REST API in Python"},"content":{"rendered":"<p>REST or Representational State Transfer is a software development style used mainly in API or Application Programming Interface design to build interactive and modern web services. It is also known as RESTful web service.<\/p>\n<p>Python is a powerful programming language. It has many libraries for building REST or RESTful APIs. One of the popular library for building web apps and writing REST APIs is Flask.<\/p>\n<p>In this article, I will show you how to create REST API in Python using Flask. Let\u2019s get started.<\/p>\n<p>You should have<\/p>\n<ul>\n<li>Python 2 or Python 3 installed on your computer.<\/li>\n<li>PIP or PIP3 installed on your computer.<\/li>\n<li>The basic understanding of Python programming language.<\/li>\n<li>The basic understanding of executing commands in the shell.<\/li>\n<\/ul>\n<p>You should be able to find articles and tutorials on all these topics on <a href=\"https:\/\/linuxhint.com\/\">LinuxHint.com<\/a><\/p>\n<p>I will be using Python 3 on Debian 9 Stretch in this article. If you\u2019re using Python 2, you will have to adjust a little bit. You should be able to figure it out yourself as it will be simple as writing python instead of python3 and pip instead of pip3.<\/p>\n<h2>Setting Up Virtual Environment:<\/h2>\n<p>To put it simply, virtual environment is used to isolate one Python app from another. The Python package used to do that is virtualenv.<\/p>\n<p>You can easily install virtualenv using PIP on your computer with the following command:<\/p>\n<p>$ sudo -H pip3 install virtualenv<\/p>\n<p>Now create a project directory (let\u2019s call it pyrest\/) with the following command:<\/p>\n<p>Now create a Python virtual environment on the pyrest\/ project directory with the following command:<\/p>\n<p>Now navigate into the project directory with the following command:<\/p>\n<p>Then, activate the Python virtual environment with the following command:<\/p>\n<p>Finally, run the following command to install the Flask Python library:<\/p>\n<h3>Writing Your First Flask Script:<\/h3>\n<p>In this section, I will write a hello world program in Python Flask.<\/p>\n<p>First, create a file hello.py in your project directory:<\/p>\n<p>Now add the following lines to hello.py file and save it.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/10\/1-6.png\" alt=\"\" width=\"1140\" height=\"490\" \/><\/p>\n<p>In the next section, I will show you how to run Flask scripts.<\/p>\n<h3>Running Flask Script:<\/h3>\n<p>Now to start the hello.py Flask server, run the following command:<\/p>\n<p>As you can see, the server has started on http:\/\/127.0.0.1:8080.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/10\/2-6.png\" alt=\"\" width=\"1037\" height=\"215\" \/><\/p>\n<p>Now, you can access the Flask server http:\/\/127.0.0.1:8080 from the web browser or API testing softwares such as Postman. I am going to use CURL.<\/p>\n<p>$ curl http:\/\/127.0.0.1:8080<\/p>\n<p>As you can see, the correct output is printed on the screen.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/10\/3-6.png\" alt=\"\" width=\"1040\" height=\"113\" \/><\/p>\n<p>Congrats! Flask is working.<\/p>\n<h3>Accessing Data Using GET in REST API:<\/h3>\n<p>GET request on REST API is used to fetch information from the API server. You set some API endpoints and do a GET request on that end point. It\u2019s simple.<\/p>\n<p>First, create a new file get.py in your project directory with the following command:<\/p>\n<p>Now add the following lines in your get.py file and save it.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/10\/4-5.png\" alt=\"\" width=\"939\" height=\"451\" \/><\/p>\n<p>Here, on line 1, the Flask constructor function and jsonify function is imported from the flask module.<\/p>\n<p>On line 3, a Flask object is created and stored on app variable.<\/p>\n<p>On line 5, I created a Python array of dictionaries of some dummy data and stored it in the accounts variable.<\/p>\n<p>On line 10, I defined the API endpoint \/accounts and the request method, which is GET.<\/p>\n<p>On line 11, I defined the function getAccounts(). getAccounts() function will execute when a GET request to \/accounts endpoint is made.<\/p>\n<p>Line 12, which is a part of getAccounts() function, I converted the accounts array of dictionaries to JSON using jsonify() function and returned it.<\/p>\n<p>On line 14-15, I called the app.run() to tell Flask to run the API server on port 8080.<\/p>\n<p>Now run the Flask API server with the following command:<\/p>\n<p>The server has started on port 8080.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/10\/5-5.png\" alt=\"\" width=\"1023\" height=\"205\" \/><\/p>\n<p>Now make a GET request to the \/accounts endpoint with CURL as follows:<\/p>\n<p>$ curl http:\/\/127.0.0.1:8080\/accounts<\/p>\n<p>As you can see, the accounts data is displayed as JSON format on GET request on \/accounts endpoint.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/10\/6-4.png\" alt=\"\" width=\"1035\" height=\"296\" \/><\/p>\n<p>You can also get specific account data as well. To do that, I am going to create another API endpoint \/account\/&lt;id&gt;. Here, &lt;id&gt; will be the ID the account holder. The ID here is the index of the array.<\/p>\n<p>Edit the get.py script and add the marked lines to it.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/10\/7-4.png\" alt=\"\" width=\"930\" height=\"577\" \/><\/p>\n<p>Here, on line 14, I defined the API endpoint \/account\/&lt;id&gt; and the method to be used, which is GET.<\/p>\n<p>On line 15-17, the function getAccount() for the API endpoint \/account\/&lt;id&gt; is defined. The getAccount() function accepts a id as an argument. The value of &lt;id&gt; from the API endpoint is set to the id variable of getAccount() function.<\/p>\n<p>On line 16, the id variable is converted to an integer. I also deduced 1 from the id variable. Because the array index starts from 0. I want to start the account ID from 1. So if I put 1 as the account &lt;id&gt;, 1 \u2013 1 = 0, I will get the element at index 0 from the array accounts.<\/p>\n<p>On line 17, the array at index &lt;id&gt; is returned as JSON.<\/p>\n<p>The rest of the codes are the same.<\/p>\n<p>Now run the API server again.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/10\/8-3.png\" alt=\"\" width=\"1038\" height=\"230\" \/><\/p>\n<p>I requested data for account 1 and 2 separately and I got the expected output as you can see from the screenshot below.<\/p>\n<p>$ curl http:\/\/127.0.0.1:8080\/account\/1<br \/>\n$ curl http:\/\/127.0.0.1:8080\/account\/2<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/10\/9-2.png\" alt=\"\" width=\"1080\" height=\"313\" \/><\/p>\n<h3>Adding Data Using POST in REST API:<\/h3>\n<p>Now I am going to rename get.py to api.py and add an API endpoint \/account for adding new data.<\/p>\n<p>Rename get.py to api.py:<\/p>\n<p>First, add the lines (19-26) as marked in the screenshot below to the api.py file.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/10\/10-3.png\" alt=\"\" width=\"936\" height=\"731\" \/><\/p>\n<p>Now run the api.py server:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/10\/11-2.png\" alt=\"\" width=\"1032\" height=\"216\" \/><\/p>\n<p>To insert new data into the \/account endpoint, run the following command:<\/p>\n<p>$ curl -X POST -H &#8220;Content-Type: application\/json&#8221; -d &#8216;{&#8220;name&#8221;: &#8220;Shovon&#8221;, &#8220;balance&#8221;: 100}&#8217;<br \/>\nhttp:\/\/127.0.0.1:8080\/account<\/p>\n<p><u>NOTE:<\/u> Here, \u2018{\u201cname\u201d: \u201cShovon\u201d, \u201cbalance\u201d: 100}\u2019 is the JSON input data.<\/p>\n<p>The data should be inserted.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/10\/12-2.png\" alt=\"\" width=\"1064\" height=\"187\" \/><\/p>\n<p>As you can see, the new data is added.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/10\/13-2.png\" alt=\"\" width=\"1073\" height=\"361\" \/><\/p>\n<p>So that\u2019s it for this article. Thanks for reading this article.<\/p>\n<p><a href=\"https:\/\/linuxhint.com\/rest_api_python\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>REST or Representational State Transfer is a software development style used mainly in API or Application Programming Interface design to build interactive and modern web services. It is also known as RESTful web service. Python is a powerful programming language. It has many libraries for building REST or RESTful APIs. One of the popular library &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/18\/creating-rest-api-in-python\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Creating REST API in Python&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-844","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/844","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=844"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/844\/revisions"}],"predecessor-version":[{"id":1024,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/844\/revisions\/1024"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}