0
0
FastAPIframework~30 mins

Background tasks in FastAPI - Mini Project: Build & Apply

Choose your learning style9 modes available
Background Tasks with FastAPI
📖 Scenario: You are building a simple web API that accepts user requests to send welcome emails. Sending emails can take time, so you want to handle this task in the background without making the user wait.
🎯 Goal: Create a FastAPI app that accepts a POST request with a user's email. The app should start a background task to simulate sending a welcome email, then immediately respond to the user.
📋 What You'll Learn
Create a FastAPI app instance named app
Define a POST endpoint at /send-email/ that accepts a JSON body with an email field
Use FastAPI's BackgroundTasks to run a function called send_welcome_email in the background
The send_welcome_email function should accept an email parameter and simulate sending an email by printing a message
The endpoint should return a JSON response immediately confirming the email is being sent
💡 Why This Matters
🌍 Real World
Background tasks are useful in web apps to handle slow operations like sending emails, processing files, or calling external APIs without making users wait.
💼 Career
Understanding background tasks is important for backend developers to build responsive and scalable APIs.
Progress0 / 4 steps
1
Create FastAPI app and email data model
Import FastAPI and BaseModel from fastapi and pydantic respectively. Create a FastAPI app instance called app. Define a Pydantic model called EmailRequest with one field email of type str.
FastAPI
Need a hint?

Use app = FastAPI() to create the app. Define EmailRequest as a class inheriting from BaseModel with an email string field.

2
Add background task function
Define a function called send_welcome_email that takes one parameter email. Inside the function, add a print statement that says Sending welcome email to {email} using an f-string.
FastAPI
Need a hint?

Define a simple function that prints the welcome email message using the email parameter.

3
Create POST endpoint with background task
Import BackgroundTasks from fastapi. Create a POST endpoint at /send-email/ using @app.post. The endpoint function should be called send_email and accept two parameters: request of type EmailRequest and background_tasks of type BackgroundTasks. Inside the function, add the background task send_welcome_email with the email from request.email. Return a dictionary with the key message and value "Email is being sent to {request.email}".
FastAPI
Need a hint?

Use background_tasks.add_task() to schedule the email sending function. Return a confirmation message immediately.

4
Add type hints and finalize code
Ensure the send_welcome_email function has a return type hint of None. Also, add a type hint request: EmailRequest and background_tasks: BackgroundTasks in the send_email function signature. Confirm the full code includes all imports, the app instance, the data model, the background task function, and the POST endpoint.
FastAPI
Need a hint?

Add -> None to the background task function and -> dict to the endpoint function for clarity.