Setting Up a Simple API Using Python and Flask
In this article, I’ll show you how to create a simple API using Python and the Flask framework. This is the first part of a two-part series. Here, I’ll cover the basic concepts and setup, while in the next article, I’ll dive into advanced topics like authentication, security, and usability. Let’s get started!
What Is an API?
First, let’s clarify what an API is. An API (Application Programming Interface) allows developers to interact with your application indirectly. Instead of requiring access to your source code, they can use your API to perform actions like creating accounts, sending messages, or retrieving data.
Typically, a Web API consists of one or more URLs, each with specific conditions for executing commands in your application. Remember that API development requires careful attention—any vulnerabilities could affect your entire application.
API calls can return various types of data, such as plain text, JSON objects, image files, and more. For example, the URL below is an API that returns a JSON object containing comments from an application:
Requirements
-
Python 3.8 or above: Python is a high-level programming language capable of building almost anything. In this article, we’ll use it as the backend language for our application. Since Python is an interpreted language, you’ll need to install the Python interpreter on your system. You can download it from the official website: python.org.
-
Flask: Flask is a lightweight Python web framework that simplifies web application development. It’s a standalone module with minimal dependencies, making it easy to install using PIP (Python’s package manager).
Installation
Once Python and PIP (which installs automatically with Python) are set up, open your system’s command-line interface (Terminal on macOS/Linux or CMD on Windows) and run the following command to install Flask:
pip3 install flaskAfter Flask is installed, you’re ready to proceed.
Setting Up a Simple Flask Application
To create a Flask application, start by importing the Flask class:
from flask import FlaskNext, create an instance of the Flask class. This instance represents your application:
app = Flask(__name__)The __name__ variable passed to the constructor is a built-in Python variable that holds the name of your application.
Routing
Routing allows you to define accessible URLs or resources in your application. Frameworks like Flask handle routing programmatically, saving time and improving security compared to manual routing. Here’s how to define a route:
@app.route('/', methods=['GET'])def home_page(): return "Tada! My simple Flask app is running!"In this example, when a client accesses the root URL (/) with a GET request, the home_page function is called, returning the specified text. The root URL can be written with or without a trailing slash (e.g., floppydisk.vercel.app is equal to floppydisk.vercel.app/).
The methods argument in the route decorator is a list, meaning you can specify multiple HTTP methods (or HTTP verbs) for a single route.
Running the Application
Finally, start your application by calling the run method:
app.run(host='localhost', port=5000)This code serves your application on localhost (IP address 127.0.0.1) at port 5000, making it accessible at http://127.0.0.1:5000. To run the script, execute the following command in your terminal:
python3 [your-file-name].pyIf everything works correctly, you’ll see a message like this in your console:
* Serving Flask app 'app' * Debug mode: offWARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on http://localhost:5000Press CTRL+C to quitNow, open your browser and navigate to http://127.0.0.1:5000. You should see the message: Tada! My simple Flask app is running!
Here’s the complete source code:
from flask import Flask
app = Flask(__name__)
@app.route('/', methods=['GET'])def home_page(): return "Tada! My simple Flask app is running!"
app.run(host='localhost', port=5000)Conclusion
In this article, I’ve demonstrated how to create a simple API using Python and Flask. In the next part, I’ll cover the advanced concepts mentioned earlier.