Back to Browse

How to Create a Flask RESTful API with GET and POST Requests | Flask REST API Tutorial

2.6K views
Feb 3, 2025
19:24

Want to build a **REST API** using Flask? 🚀 In this tutorial, we’ll walk you through creating a **Flask RESTful API** with **GET and POST** requests step by step. Flask makes it easy to build lightweight, scalable, and efficient APIs for web and mobile applications. By the end of this tutorial, you'll have a fully functional **Flask API** that can **send and receive JSON data** using HTTP requests. --- ## **🔹 What You’ll Learn in This Video:** ✅ Setting up Flask for API Development ✅ Creating a Flask RESTful API with `Flask` and `Flask-RESTful` ✅ Handling **GET & POST Requests** ✅ Sending & Receiving **JSON Data** ✅ Running & Testing the API with **Postman or cURL** --- ## **🔹 Prerequisites** Before starting, ensure you have: ✔️ **Python 3.x** installed → [Download Python](https://www.python.org/downloads/) ✔️ **Flask & Flask-RESTful Installed** ✔️ **VS Code or Any Code Editor** --- ## **🔹 Step-by-Step Guide** ### **1️⃣ Install Required Packages** Open the terminal and run the following command: ```bash # Create a project directory mkdir flask-rest-api && cd flask-rest-api # Create a virtual environment python -m venv venv source venv/bin/activate # Windows: venv\Scripts\activate # Install Flask and Flask-RESTful pip install flask flask-restful ``` --- ### **2️⃣ Create a Flask API Project** Create a new file called **`app.py`** and add the following code: ```python from flask import Flask, request, jsonify from flask_restful import Resource, Api app = Flask(__name__) api = Api(app) # Sample in-memory data data = [] # Define a Resource for GET and POST class ItemResource(Resource): def get(self): return jsonify({"items": data}) def post(self): new_item = request.get_json() # Get JSON data from request data.append(new_item) return jsonify({"message": "Item added!", "item": new_item}) # Add the resource to the API api.add_resource(ItemResource, "/items") if __name__ == "__main__": app.run(debug=True) ``` --- ### **3️⃣ Run the Flask API Server** Save the file and run: ```bash python app.py ``` You should see Flask running on **http://127.0.0.1:5000** --- ### **4️⃣ Test the API with GET & POST Requests** #### **✅ GET Request (Fetch Items)** Open a browser or Postman and visit: 👉 **http://127.0.0.1:5000/items** It should return: ```json {"items": []} ``` #### **✅ POST Request (Add an Item)** Use **Postman** or cURL to send a **POST request** with JSON data: ```json { "name": "Laptop", "price": 1200 } ``` **Postman Setup:** - Select **POST** - Enter **http://127.0.0.1:5000/items** - Go to **Body** → **raw** → **JSON** - Paste the JSON data and hit **Send** 💡 The API will respond: ```json { "message": "Item added!", "item": { "name": "Laptop", "price": 1200 } } ``` --- ### **5️⃣ Fetch Updated Items** Re-run the **GET request** to **http://127.0.0.1:5000/items** Now it should return the updated list: ```json { "items": [ { "name": "Laptop", "price": 1200 } ] } ``` --- ## **🔹 Who Is This Tutorial For?** ✅ Web Developers Building APIs ✅ Backend Engineers Working with REST APIs ✅ Anyone Looking to Learn API Development with Flask --- ## **🔹 Resources Mentioned in This Video:** 📌 Flask Documentation → [https://flask.palletsprojects.com/](https://flask.palletsprojects.com/) 📌 Flask-RESTful Docs → [https://flask-restful.readthedocs.io/](https://flask-restful.readthedocs.io/) --- ## **💡 Pro Tips for API Development** ✔ Use **Postman** to test API endpoints. ✔ Add **error handling** for better responses. ✔ Use a **database (MySQL, PostgreSQL, SQLite)** instead of in-memory storage. ✔ Deploy your API using **Docker, AWS, or GCP**. --- ## **👍 Like, Share & Subscribe!** If this tutorial helped you, please **LIKE, SHARE, and SUBSCRIBE** for more **Flask, Python, and Web Development** tutorials! 🚀 💬 Got questions? Drop them in the **comments** below! --- ### **🔹 Hashtags:** #Flask #API #RESTAPI #Python #WebDevelopment #Backend #FlaskTutorial #PythonAPI #APIDevelopment #FlaskRestful Start building your Flask REST API today! 🐍🚀

Download

0 formats

No download links available.

How to Create a Flask RESTful API with GET and POST Requests | Flask REST API Tutorial | NatokHD