If you’re new to programming and want to learn how to build an API in Python, you’re in the right place!
In this guide, I’ll walk you through creating a simple REST API using Python’s Flask framework. By the end, you’ll know how to:
- Install Flask
- Create a basic API from scratch
- Add GET and POST operations
- Test your API using Postman
This tutorial is designed for absolute beginners, so don’t worry if you’ve never built an API before. Let’s get started!
The tutorial assumes that you have some basic familiarity with concepts of coding and basics of Python.
What is an API?
API stands for Application Programming Interface. In simple words, it’s a way for two applications to talk to each other.
For example: when you use a weather app, it fetches live weather data from a weather API.
In this tutorial, we’ll build a small API that lets us store and fetch messages.
Step 1: Install Flask
Flask is a lightweight web framework in Python, perfect for beginners.
Open your terminal (Command Prompt or PowerShell on Windows, Terminal on Mac/Linux) and type:
pip install flask
That’s it! Flask is now installed.
Step 2: Create a Python File
Create a file named app.py
and add this basic Flask setup:
from flask import Flask, request, jsonify
app = Flask(__name__)
# Temporary storage
messages = []
@app.route('/')
def home():
return "Welcome to My First API!"
Here, we created a simple Flask app with a home route /
. If you open this route in your browser, it will display “Welcome to My First API!”.
Step 3: Add a GET Endpoint
A GET request is used to fetch data. Let’s allow users to fetch all stored messages.
Add this code inside app.py
:
@app.route('/messages', methods=['GET'])
def get_messages():
return jsonify({"messages": messages})
Now, when someone visits /messages
, they’ll see all stored messages in JSON format.
✍️ Step 4: Add a POST Endpoint
A POST request lets us send data to the server. Let’s allow users to add new messages.
@app.route('/messages', methods=['POST'])
def add_message():
data = request.get_json()
message = data.get("message")
if not message:
return jsonify({"error": "Message is required"}), 400
messages.append(message)
return jsonify({"message": "Message added successfully!"}), 201
Now, users can send a message to the API, and it will be stored in memory.
Step 5: Run the API
At the bottom of app.py
, add:
if __name__ == '__main__':
app.run(debug=True)
Save the file, then run:
python app.py
If everything works, you’ll see:
* Running on http://127.0.0.1:5000/
That means your API is running locally at http://127.0.0.1:5000
Step 6: Test the API with Postman
Now let’s test it out using Postman (a free tool for API testing).
1. Test the GET request
- Open Postman
- Choose GET method
- Enter:
http://127.0.0.1:5000/messages
- Click Send
- You should see:
{ "messages": [] }
2. Test the POST request
- Change the method to POST
- Enter:
http://127.0.0.1:5000/messages
- Go to the Body tab → Select raw → Choose JSON
- Enter:
{ "message": "Hello World!" }
- Click Send
- You’ll see:
{ "message": "Message added successfully!" }
Now, if you run the GET request again, you’ll see:
{
"messages": ["Hello World!"]
}
Voila, you just built and tested your first Python API!
Final Thoughts
In this beginner’s tutorial, we covered:
- What an API is
- Installing Flask
- Creating an API with GET and POST operations
- Running it locally
- Testing it with Postman
This is just the beginning — you can extend your API by adding more routes, connecting it to a database (like SQLite or MongoDB), and even deploying it to the cloud.
Further Reading: AI’s Impact on Job Markets: Transformations in 2025
One Comment