Skip to content

Writing a Simple API with Flask (Python)

Introduction

In this tutorial, we'll walk through the process of creating a simple RESTful API using Python and the Flask web framework. Flask is a lightweight and easy-to-use framework for building web applications, including APIs.

Prerequisites

Before you begin, make sure you have the following installed:

Step 1: Setting Up the Project

Create a new directory for your project and navigate into it.

mkdir flask-api-tutorial
cd flask-api-tutorial

Step 2: Creating a Virtual Environment

It's good practice to use a virtual environment to isolate your project's dependencies. Create a virtual environment using the following commands:

1
2
3
python -m venv venv
# On Windows: python -m venv venv
Activate the virtual environment:
1
2
3
4
5
# On macOS/Linux
source venv/bin/activate
# On Windows
venv\Scripts\activate
This step ensures that your project has a dedicated environment for its dependencies, minimizing conflicts and ensuring consistency across different projects.

Step 3: Installing Flask

Install Flask within the virtual environment:

pip install Flask

Step 4: Writing the API Code

Create a file named app.py in your project directory and open it in a text editor. Add the following code:

from flask import Flask, jsonify

app = Flask(__name__)

@app.route('/api', methods=['GET'])
def get_data():
    data = {'message': 'Hello, API!'}
    return jsonify(data)

if __name__ == '__main__':
    app.run(debug=True)

This code sets up a basic Flask application with a single endpoint (/api) that returns a JSON response.

Step 5: Running the API

In the terminal, run the Flask application:

python app.py
Visit http://127.0.0.1:5000/api in your browser or use a tool like curl or Postman to make a GET request.
curl http://127.0.0.1:5000/api
You should receive a JSON response: {"message": "Hello, API!"}

Conclusion

Congratulations! You've successfully created a simple API using Flask. This is just a starting point, and you can expand and enhance your API by adding more routes, handling different HTTP methods, and integrating with databases.

Explore Flask's documentation (Flask Documentation) for more advanced features and best practices.

Feel free to adapt this tutorial to other frameworks or languages as needed.