Simple project FastAPI Kafka PubSub

2 min read 7 months ago
Published on May 04, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Tutorial: Creating a REST API with FastAPI and Kafka PubSub

Step 1: Introduction

  • Welcome to this tutorial where we will learn how to create a REST API using FastAPI and Kafka PubSub.
  • This tutorial is based on the video titled "Simple project FastAPI Kafka PubSub" from the channel lemoncode21.

Step 2: Setting up the Environment

  • Ensure you have Python installed on your system.
  • Install FastAPI and Kafka libraries by running the following commands:
    pip install fastapi
    pip install kafka-python
    

Step 3: Importing Required Libraries

  • In your Python script, import the necessary libraries:
    from fastapi import FastAPI
    from kafka import KafkaProducer
    

Step 4: Creating a FastAPI Instance

  • Initialize a FastAPI instance by creating an object of the FastAPI class:
    app = FastAPI()
    

Step 5: Defining a Route to Receive Requests

  • Define a route using a decorator to specify the endpoint where requests will be sent:
    @app.post("/send_message/{message}")
    async def send_message(message: str):
        # Code for sending the message to Kafka
    

Step 6: Implementing Kafka Producer

  • Create a Kafka Producer instance to publish messages to a Kafka topic:
    producer = KafkaProducer(bootstrap_servers='localhost:9092')
    

Step 7: Sending Messages to Kafka

  • Inside the send_message function, use the Kafka Producer to send the message to a Kafka topic:
    producer.send('my_topic', message.encode())
    

Step 8: Running the FastAPI Server

  • Start the FastAPI server by running the following command in your terminal:
    uvicorn your_script_name:app --reload
    
    Replace your_script_name with the name of your Python script.

Step 9: Testing the API

  • Use a tool like Postman or curl to send POST requests to http://localhost:8000/send_message/your_message.
  • Replace your_message with the message you want to send to Kafka.

Step 10: Conclusion

  • Congratulations! You have successfully created a REST API using FastAPI and integrated it with Kafka PubSub.
  • Feel free to explore more features and functionalities of FastAPI and Kafka for your projects.

By following these steps, you can create a simple project that leverages FastAPI for building REST APIs and Kafka for real-time message processing.