DEV Community 👩‍💻👨‍💻

Cover image for How to create a basic rest API step by step and line by line!
Ezpie
Ezpie

Posted on

How to create a basic rest API step by step and line by line!

For Totally no reason I created this post. So here's how to create a rest API using light and simple python framework flask!

YOU NEED THIS!

Before starting make sure you have a folder with a python venv. If not then here(You all are so lucky to have me!)

  1. Create a venv by typing python3 -m venv env. env is the name of the venv.
  2. Activate the venv, env/Scripts/activate
  3. The venv is now activate

install every little thing!

Now to installing, we need the flask framework, the flask_restful, and postmen

  1. pip install flask
  2. pip install flask_restful. Quick thing, flask_restful needs to be install for me, but maybe gets install for you with flask.

Lets create a rest api!

First create a file called any thing you want, I will call it app.py. And here's how it looks

from flask import Flask
from flask_restful import Api, Resource

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

class Hello(Resource):
    def get(self):
        return {'data': 'this is json!'}

api.add_resource(Hello, '/hello')

app.run(debug=True)
Enter fullscreen mode Exit fullscreen mode

Here we have a class which is just an api as a fact.
If you know about how web work then get is simple for you. it's just a method to get info from servers.
Run this file in a CLI and your flask server is now ready to work!

test api using postman

After installing postman open it.
postman
This is how postman looks
click on the create new link and now this thing opens
after clicking create new
Now click on the http request
and now where it asks for a url there enter the url of your api(When you run your flask server it gets displayed)
Now hit send and you get a response from the server!
Image description
And that's that! But if you want more do comment below and sure will share!

Top comments (0)

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.