Writing your first API with Python Flask.

Hari Lee
3 min readAug 23, 2019

--

Step by step guide to building a simple GET and POST method using python flask.

What is this article about

This article will take you through the steps in creating a simple flask application and write your own API for the application you are building.

I myself am a frontend developer, so I am familiar with the issues any front-end developer would face while trying to migrate to writing API’s or trying to get into the backend development. Teaching all the aspects of creating a strong backend code is beyond the scope of this article but this could serve as the right ignition to achieve just that.

Technical Requirements

  1. any code editor (I personally prefer visual studio code)
  2. the latest version of python installed (find the link here)

Let’s write some code

  1. Create a new project name it “Flask_program”.
  2. Create a file inside and name it “app.py”.
  3. Now open up your command prompt and direct to the folder where your project is placed (if you are using visual studio code press CTRL and ` button to open up the inbuilt command prompt).
  4. Write pip install flask to install flask in your project.
  5. Once the installation is completed open up the project in visual studio code.
  6. Open app.py and write the following code.

4. Now run the command “python app.py” in the command prompt you should be able to see a set of comments appearing in the command prompt like this.

5. Now simply open up your browser and search localhost and you will be able to see your program executing.

Creating a GET method

A GET method is used to carry data from the client to server in the HTTP protocol, a GET method carries request parameters appended in the URL.

let's see how it is done.

  1. Continue in the same folder and add the following code in the file app.py you created.

2. After writing the following code run the program like before.

3. This time in order to see the route in action you can either use postman or simply got to the browser and hit the URL

4. Open up your browser and type the URL “ http://localhost:5000/login/username/password”.

here in place of username and password give any string of your choice, remember we discussed earlier that a GET method is one that accepts parameters appended in the URL, that is what we are doing here.

Give yourself a nice pat you have successfully written a GET method in python flask.

Creating a POST method

A POST method is used to carry data from the client to the server in the body of the URL rather than embedded in the URL itself.

let’s see how it is done.

  1. Continue in the same folder and add the following code in the file app.py you created.

2. The above code guides you in writing a POST method and execute it the same way.

3. In order to test this route, we will make use of an application called postman( download postman here)

4. Make sure that you spell it correctly.

Conclusion

You can now play with the above code snippet and make wonders happen.

Thank you.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response