How to write a simple Flask API for hello world?


Flask is a lightweight web framework written in Python that allows developers to quickly build web applications and APIs. It provides a simple and elegant way to create RESTful APIs with minimal code. In this article, we will walk through the process of creating a simple Flask API that returns a "Hello, World!" message.

Prerequisites

Before we start, make sure you have the following installed on your machine:

  • Python (version 3.6 or above)

  • Flask (can be installed using pip)

Algorithm

A generic algorithm for writing any simple Flask API is as follows:

  • Set up the Flask environment:

    • Create a new directory for your project.

    • Navigate to the project directory in your terminal or command prompt.

  • Create a virtual environment:

    • Use the command python3 -m venv venv to create a virtual environment.

    • Activate the virtual environment using the appropriate command based on your operating system.

  • Install Flask:

    • Use the command pip install flask to install Flask within the virtual environment.

  • Write the Flask API code:

    • Create a Python file, such as app.py, in your project directory.

    • Import the Flask module.

    • Create a Flask application instance using app = Flask(__name__).

    • Define a route using the @app.route decorator. For the "Hello, World!" API, use @app.route('/').

    • Create a function, such as hello_world(), and include the desired response.

    • Ensure that the if __name__ == '__main__': condition is present, which allows the server to run when executing the script directly.

  • Run the Flask API:

    • Save the app.py file.

    • Make sure you are in the project directory with the virtual environment activated.

    • Use the command python app.py to start the Flask development server.

  • Test the Flask API:

    • Open a web browser or a tool like Postman.

    • Access http://127.0.0.1:5000/ to see the "Hello, World!" message.

  • Further development:

    • Explore additional Flask features and extensions to enhance your API's functionality.

    • Consider security measures, best practices, and scalability as you build more advanced APIs.

Steps for Creating a Simple Flask API

Step 1: Setting up the Flask Environment

To begin, let's create a new directory for our project. Open your terminal or command prompt and navigate to the desired location. Use the following command to create a new directory:

mkdir flask-api-hello-world

After creating the directory move to the project directory using the command below

cd flask-api-hello-world

Step 2: Creating a virtual environment

It is considered good practice to work within a virtual environment to avoid conflicts between different project dependencies. Create a virtual environment using the following command:

python3 -m venv venv

Activate the virtual environment :

On macOS/Linux

source venv/bin/activate

On Windows:

venv\Scripts\activate

Step 3: Installing flask

Once inside the virtual environment, we can install Flask using pip:

Pip install flask

Step 4: Writing the Flask API

Now, let's create a Python file named app.py and open it in your preferred text editor. Add the following code to create a basic Flask API:

Example

In the below code example, we import the Flask module, create a Flask web application instance, and define a route using the @app.route decorator. The route / corresponds to the root URL of our API. When a request is made to this URL, the hello_world function is executed, returning the string 'Hello, World!'.

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

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

Step 5: Running the flask API

Save the app.py file and return to your terminal or command prompt. Ensure that you are still inside the project directory and that your virtual environment is active. Use the following command to start the Flask development server:

python app.py

Output

If everything is set up correctly, you should see output similar to the following:

 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Step 6: Testing the FLask API

Open a web browser or a tool like Postman and navigate to http://127.0.0.1:5000/. You should see the "Hello, World!" message displayed in your browser or as the response in Postman.

Conclusion

In this article, we discussed the process of building a simple Flask API that returns a "Hello, World!" message. We covered the necessary steps, from setting up the Flask environment and creating a virtual environment to writing the code and running the Flask development server. By following these steps, you should now have a basic understanding of how to create a simple Flask API.

Updated on: 16-Oct-2023

69 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements