<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Timothy Kenno Handojo</title>
    <description>The latest articles on DEV Community by Timothy Kenno Handojo (@timkenhan).</description>
    <link>https://dev.to/timkenhan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F490046%2F12e844fc-a710-452a-bbb2-dbffdd2dada6.jpg</url>
      <title>DEV Community: Timothy Kenno Handojo</title>
      <link>https://dev.to/timkenhan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/timkenhan"/>
    <language>en</language>
    <item>
      <title>Building a RESTful API with Flask - part 0: getting started</title>
      <dc:creator>Timothy Kenno Handojo</dc:creator>
      <pubDate>Thu, 15 Oct 2020 06:58:37 +0000</pubDate>
      <link>https://dev.to/timkenhan/building-a-restful-api-with-flask-part-0-getting-started-1m07</link>
      <guid>https://dev.to/timkenhan/building-a-restful-api-with-flask-part-0-getting-started-1m07</guid>
      <description>&lt;p&gt;There can be some mystique aura surrounding APIs, and REST API is no exception. When I first got started with my current job position, I had no idea what the heck it was, yet had build one myself (with my team, of course). With this article, I hope to demistify this mystery and show that you can build one too!&lt;/p&gt;

&lt;p&gt;For the technology stack, we'll have Python 3.7 or above. We'll be building our stuff on top of Flask micro-framework. To test, we'll be using cURL (Windows users, you should have it by now). We'll also need a Linux box running as a production server. There's actually quite a bit more to that, but we'll get to that. As for now, this should be enough.&lt;/p&gt;

&lt;p&gt;Let's get started!&lt;/p&gt;

&lt;h1&gt;
  
  
  Setting up the environment
&lt;/h1&gt;

&lt;p&gt;If you haven't made a directory for your project, do it now. If you've never done it before, better start doing so now!&lt;/p&gt;

&lt;p&gt;For this example, I'll be naming my directory &lt;code&gt;restful-api&lt;/code&gt;. Let's point our terminal to that and run &lt;code&gt;python3 -m venv venv&lt;/code&gt; to install the virtual environment. Windows user would wanna run &lt;code&gt;py -3 -m venv venv&lt;/code&gt; instead. You will now see a new directory called &lt;code&gt;venv&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With this virtual environment, you get to install any python packages without touching any of your actual system files. When you're done with it (or somehow mess it up), you can just delete it and create new one when you're ready to use it again. Avoid the temptation to move it around as it won't work (I've learned it the hard way).&lt;/p&gt;

&lt;p&gt;Before you do anything, the virtual environment needs to be activated. Unlike what you did above, this one needs to be done &lt;strong&gt;everytime&lt;/strong&gt; you open a new terminal to run anything (e.g. when you're getting back from a break). To do so, run &lt;code&gt;. venv/bin/activate&lt;/code&gt;. Windows user would instead run &lt;code&gt;venv\Scripts\activate&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, assuming you're already in the virtual environment, you will install the necessary python packages. Run &lt;code&gt;pip install flask&lt;/code&gt;. Windows user would do the same.&lt;/p&gt;

&lt;h1&gt;
  
  
  Creating the App
&lt;/h1&gt;

&lt;p&gt;After the installation finish (or while waiting for it), create a new directory called &lt;code&gt;app&lt;/code&gt; where you will do most of your work. Create a file called &lt;code&gt;__init__.py&lt;/code&gt; and fill it with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from flask import Flask

def create_app(config_object=None):
    app = Flask(__name__)

    @app.route('/')
    def hello():
        return 'Hello World!\n'

    return app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it! You've created a working application.&lt;/p&gt;

&lt;p&gt;Check out the &lt;a href="https://flask.palletsprojects.com/"&gt;Flask documentation&lt;/a&gt; for more information on how it works.&lt;/p&gt;

&lt;p&gt;You actually could've put everything in a single file and make everything much simpler like in many other tutorials. However, this method would make it more manageable when scaling bigger. You can thank me later.&lt;/p&gt;

&lt;h1&gt;
  
  
  Testing the App
&lt;/h1&gt;

&lt;p&gt;To test it (after the installation finish), run &lt;code&gt;flask run&lt;/code&gt;. You can see it in your terminal that the app is now accessible thru &lt;code&gt;http://127.0.0.1:5000&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To access this, you can use any HTTP client. Web browser would do for this step (go on, try it!), but not for the next ones as we'd need to manipulate our request data with relative ease.&lt;/p&gt;

&lt;p&gt;So we'll use cURL, a command line HTTP client. Now open a new terminal and type &lt;code&gt;curl http://127.0.0.1:5000&lt;/code&gt; (alternatively, can type &lt;code&gt;curl localhost:5000&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;You may have been using Postman. If you're already familiar with it, you can use it to test this app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kenno-ideapad:[kenno]:~$ curl localhost:5000
Hello World!kenno-ideapad:[kenno]:~$ curl http://127.0.0.1:5000/
Hello World!kenno-ideapad:[kenno]:~$ 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Touching up
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;But wait, shouldn't RESTful API be doing stuff in JSON?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That is true... and I was just showing you how to get things working.&lt;/p&gt;

&lt;p&gt;Now to get it working in JSON, we're going to revisit &lt;code&gt;app/__init__.py&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Go to the line with &lt;code&gt;from flask import Flask&lt;/code&gt; and add &lt;code&gt;, jsonify&lt;/code&gt; at the end, turning it into &lt;code&gt;from flask import Flask, jsonify&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now go to the function &lt;code&gt;hello()&lt;/code&gt; and change its return statement from &lt;code&gt;return 'Hello World!\n'&lt;/code&gt; to &lt;code&gt;return jsonify({'message': 'Hello World!'})&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You will now have on your &lt;code&gt;app/__init__.py&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from flask import Flask, jsonify

def create_app(config_object=None):
    app = Flask(__name__)

    @app.route('/')
    def hello():
        return jsonify({'message': 'Hello World!'})

    return app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Restart the application by pressing &lt;code&gt;Ctrl-C&lt;/code&gt; on the console and running &lt;code&gt;flask run&lt;/code&gt; again.&lt;/p&gt;

&lt;p&gt;Check again with your cURL.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kenno-ideapad:[kenno]:~$ curl http://127.0.0.1:5000/
{"message":"Hello World!"}
kenno-ideapad:[kenno]:~$ curl localhost:5000
{"message":"Hello World!"}
kenno-ideapad:[kenno]:~$ 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Better?&lt;/p&gt;

&lt;p&gt;Stay tuned for the next part! While at it, get your Linux box ready and get it on your home network.&lt;/p&gt;

</description>
      <category>python</category>
      <category>webdev</category>
      <category>flask</category>
      <category>restful</category>
    </item>
  </channel>
</rss>
