Imagine that you want to create your first web application or make a program interface (API) that can receive requests and return responses. Sounds complicated? In fact, with the help of Flask, a simple but powerful web framework for Python, it's very easy to do. Today I will show you how to quickly write a simple API that can accept requests and respond to them.

What is Flask?
Flask is a lightweight web framework for Python that makes it easy to create web applications and APIs. It is easy to use and learn, and it is also ideal for small projects or for quick prototyping of ideas. Flask is a great choice for those who want to quickly understand how web development works and how to write an API.
Getting Started: Installing Flask
Before we start writing code, we need to install Flask. To do this, open the terminal and run the following command:
pip install FlaskAfter that, you will have Flask installed, and you can start writing our first API.
Creating the first Flask API
Now let's create a file named app.py. In this file, we will write the code for our API. Open the code editor and paste the following code:
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/')
def home():
return "Welcome to our simple API!"
@app.route('/api/greet', methods=['GET'])
def greet():
response = {
"message": "Hello! This is a response from our API."
}
return jsonify(response)
if __name__ == '__main__':
app.run(debug=True)Let's figure out what's going on here:
Import Flask and json:
Flaskallows us to create a web application, andjsonifyis used to return data in JSON format.Create a Flask instance:
app = Flask(__name__)— this is what our application creates.Routes (
@app.route()): A route is a way to associate a URL with a function. For example,@app.route('/')links the root page to thehomefunction, which returns a welcome message.Function
greet(): It processes the request at/api/greetand returns a JSON response.
Launching the API
To run our API, go back to the terminal and run the following command:
python app.pyAfter that, Flask will start the server, and you will see a message like:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)Now you can open your browser and go to http://127.0.0.1:5000/ to see the message "Welcome to our simple API!". And if you go to http://127.0.0.1:5000/api/greet, you will see JSON with a greeting.
Adding new features
Creating an API on Flask is like playing a constructor: you can add new routes and features so that the API can do more. Let's add a route that will accept the user's name and greet them:
from flask import request
@app.route('/api/greet/<name>', methods=['GET'])
def greet_user(name):
response = {
"message": f"Hi {name}! This is a personal greeting from our API."
}
return jsonify(response)Now, if you open http:// 127.0.0.1:5000/api/greet/John, the API will return a personalized greeting "Hello, Vasya!"
Why Flask is simple and powerful
Flask is good because it can be used to quickly create prototypes and learn web development without spending a lot of time studying complex frameworks. It is lightweight, easy to understand, and yet powerful enough to handle most tasks.
Unlike other frameworks, such as Django, Flask does not require many settings and extra files. It gives you flexibility and control over the project, which is especially important if you are just starting your programming journey.
Conclusion
Now you know how to create a simple Python API using Flask. This is a great way to start your journey in the world of web development and understand how servers and client requests work. Flask will help you easily and quickly create your first project, and then you can complicate it, add new features and learn in practice.
