Build REST API with Flask

Python 12 min min read Updated: Mar 09, 2026 Intermediate
Build REST API with Flask
Intermediate Topic 3 of 10

Build REST API with Flask

REST APIs are one of the most common ways for applications to communicate with each other. Flask provides a simple and powerful way to build RESTful APIs using Python. A REST API allows clients such as web applications, mobile apps, or other servers to interact with your application through HTTP requests.

Flask is widely used for creating APIs because it is lightweight, flexible, and easy to extend with additional libraries.

What is a REST API?

A REST API (Representational State Transfer API) is an interface that allows communication between systems using HTTP protocols. REST APIs use standard HTTP methods to perform operations on resources.

The most common HTTP methods used in REST APIs are:

Method Purpose Interview Key Points Example API
GET Retrieve data from the server Used to fetch resources without modifying server data. GET requests are idempotent, meaning multiple calls return the same result without changing the state. /api/users
POST Create new data Used to create a new resource on the server. POST requests are not idempotent, meaning repeated requests may create multiple records. /api/users
PUT Update existing data Used to completely replace an existing resource with new data. PUT requests are idempotent, meaning multiple requests produce the same result. /api/users/1
PATCH Partially update data Used to update only specific fields of a resource instead of replacing the entire object. /api/users/1
DELETE Remove data Used to delete a resource from the server. DELETE is usually idempotent, meaning deleting the same resource multiple times has the same effect. /api/users/1
HEAD Retrieve headers only Similar to GET but returns only response headers without the body. Often used for checking resource metadata. /api/users
OPTIONS Check supported methods Used by browsers for CORS preflight requests to determine allowed HTTP methods. /api/users

Installing Flask

First, install Flask using the pip package manager.

bash pip install flask

Creating a Simple REST API

Below is a basic Flask REST API that returns JSON data.

python from flask import Flask, jsonify app = Flask(__name__) @app.route("/api") def home(): return jsonify({ "message": "Welcome to Flask REST API" }) if __name__ == "__main__": app.run(debug=True)

The jsonify() function converts Python dictionaries into JSON responses.

Creating an API Endpoint

API endpoints represent resources that can be accessed through specific URLs.

python @app.route("/api/users") def get_users(): users = [ {"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"} ] return jsonify(users)

This endpoint returns a list of users in JSON format.

Handling POST Requests

The POST method allows clients to send data to the server.

python from flask import request @app.route("/api/users", methods=["POST"]) def add_user(): data = request.get_json() return jsonify({ "message": "User created", "user": data })

The request.get_json() function retrieves JSON data from the request body.

Updating Data with PUT

python @app.route("/api/users/<int:id>", methods=["PUT"]) def update_user(id): data = request.get_json() return jsonify({ "message": f"User {id} updated", "data": data })

This endpoint updates user information.

Deleting Data

python @app.route("/api/users/<int:id>", methods=["DELETE"]) def delete_user(id): return jsonify({ "message": f"User {id} deleted" })

This endpoint removes a user from the system.

Testing APIs with Postman

API endpoints can be tested using tools such as:

  • Postman
  • cURL
  • Insomnia
  • Browser developer tools

Example using cURL:

bash curl http://127.0.0.1:5000/api/users

Returning JSON Responses

APIs usually return data in JSON format because it is lightweight and widely supported.

python return jsonify({ "status": "success", "data": users })

Project Structure for Flask API

flask_api_project/
│
├── app.py
├── models/
├── routes/
├── services/
└── requirements.txt

This structure helps organize large API projects.

Real-World Applications

Flask REST APIs are commonly used in:

  • Mobile applications
  • Single-page web applications
  • Machine learning APIs
  • Microservices architectures
  • Backend services

Best Practices for Flask APIs

  • Use proper HTTP status codes.
  • Validate user input.
  • Structure APIs using blueprints.
  • Secure APIs with authentication.
  • Document APIs using tools like Swagger.

Conclusion

Flask makes it easy to build powerful REST APIs using Python. By defining routes, handling HTTP methods, and returning JSON responses, developers can create scalable backend services for modern applications.

REST APIs built with Flask are widely used in microservices, web platforms, and machine learning systems.

In the next tutorial, we will explore Flask Authentication & Security and learn how to protect APIs and user data.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators