Summarize this article with:
In this comprehensive tutorial, we'll walk you through the step-by-step process of creating a fully functional background removal API using FastAPI, one of the fastest and most modern web frameworks for building APIs with Python.
To power the actual background removal, we’ll integrate with EdenAI, a platform that aggregates multiple AI services under a single API.
For a more in-depth explanation, make sure to watch our Youtube tutorial on background removal, where we provide a detailed breakdown of each step.
By following along with the video, you'll gain a complete understanding of the process, ensuring you don't miss any important details.
Whether you're new to Python APIs, background removal, or FastAPI, this hands-on guide is beginner-friendly and practical.
What You'll Build
By the end of this tutorial, you’ll have a working API that:
- Accepts an image via URL or file upload.
- Sends the image to EdenAI’s background removal endpoint.
- Returns a base64 version of the image with the background removed (suitable for displaying in a frontend)
Prerequisites
Before starting, make sure you have the following tools and knowledge:
- Python 3.7+: Required for compatibility with FastAPI and other libraries.
- pip: Python’s package manager for installing libraries.
- EdenAI API Key: You’ll need to create an account on edenai.co and grab your API key.
- Basic Python knowledge: Especially around functions and JSON.
What is Background Removal API?
.avif)
Eden AI’s Background Removal API uses AI to automatically remove the background from images, isolating the main subject.
It supports multiple providers (like PhotoRoom and Sentisight) through a single, unified API, making it easy to integrate and switch between services.
Common use cases include e-commerce, design, and social media, helping users create clean, professional visuals without manual editing.
Step-by-Step Guide
Step 1: Project Setup
Start by creating a dedicated folder and setting up a Python virtual environment (best practice for Python projects).
A virtual environment ensures your project’s dependencies don't interfere with system-wide packages.
- mkdir and cd create and move into your project folder.
- python -m venv venv initializes a virtual environment to isolate your project dependencies.
- source venv/bin/activate activates the virtual environment so that any installed packages are limited to this project.
Next, install the required dependencies:
Here’s what each library does:
- fastapi: The web framework to build your API.
- uvicorn: An ASGI server to run your FastAPI app.
- python-dotenv: Loads environment variables (like your API key) from a .env file.
- requests: Sends HTTP requests to EdenAI’s API.
- python-multipart: Enables FastAPI to handle file uploads.
Create a .env file in the root directory and store your EdenAI API key securely:
Never upload this file to version control (like GitHub)! It contains your private API credentials.
Step 2: Create the API File
Create a file named main.py and paste in the full FastAPI code.
Step 3: Add the Model for URL Input
This model is used to define what we expect when a user sends a URL-based background removal request:
Step 4: Root Route (Health Check)
This basic route just confirms the API is live:
This is a simple GET endpoint to confirm that your API is running. When you visit the root URL, it returns a friendly message.
Step 5: URL-Based Background Removal Endpoint
Now implement an endpoint that receives an image URL and removes the background via EdenAI:
What this does:
- Takes a JSON payload with an image URL.
- Sends it to EdenAI.
- Returns the base64 image data.
If the request fails (e.g. invalid image), it returns a 500 error.
Step 6: File Upload Background Removal Endpoint
This endpoint handles images uploaded directly as files:
What this does:
- Accepts an uploaded image file.
- Temporarily saves it as temp.png.
- Sends the file to EdenAI for processing using multipart/form-data.
- Deletes the temporary file afterward to clean up storage.
- Returns the processed image as a base64 string.
Step 7: Run Your API
Start your FastAPI app using Uvicorn:
Visit http://127.0.0.1:8000 to test the health check route.
Testing the Endpoints
1. URL Test
Send a POST request to /remove-bg/url with a JSON body like:
2. Upload Test (e.g., with Postman or a frontend)
Use Postman or a frontend client to POST to /remove-bg/file with form-data:
- file: upload an image file
- providers: remove_bg
Tips and Suggestions
EdenAI supports multiple background removal providers. You can specify fallback_providers to ensure reliability if the primary one fails.
CORS middleware is enabled by default for all origins — making it frontend-friendly.
Always secure your .env files by adding them to .gitignore.
Conclusion
By following this step-by-step tutorial, you've built a powerful and flexible background removal API using FastAPI and EdenAI.
Whether you're working on a web app, mobile app, or internal tool, this API can now handle both image URLs and direct uploads, returning clean, base64-encoded images ready for use.
You've also learned best practices around environment variables, API integration, and modular code design.
From here, you can extend the functionality with more AI services from EdenAI, add user authentication, or deploy your API to a cloud platform like Render, Vercel, or Railway.
Resources
Ready to dive deeper? We've got everything you need to get started.
.avif)
.jpg)

.avif)
.avif)