Photoroom API Documentation
Ask or search…

Python Integration

In this tutorial, I will show you how you can integrate the PhotoRoom API into a Python codebase in just a few minutes.

Step 1: Get Your API Key

The first thing you need to do is get your API key. Here are the steps you'll need to follow:

Step 2: Use Our Sample Code

Visit PhotoRoom’s GitHub to find a sample code that's ready to use.
Simply copy and paste the code into your project, and update the placeholder with your own API key.
import http.client
import mimetypes
import os
import uuid
# Please replace with your own apiKey
def remove_background(input_image_path, output_image_path):
# Define multipart boundary
boundary = '----------{}'.format(uuid.uuid4().hex)
# Get mimetype of image
content_type, _ = mimetypes.guess_type(input_image_path)
if content_type is None:
content_type = 'application/octet-stream' # Default type if guessing fails
# Prepare the POST data
with open(input_image_path, 'rb') as f:
image_data =
filename = os.path.basename(input_image_path)
body = (
f"Content-Disposition: form-data; name=\"image_file\"; filename=\"{filename}\"\r\n"
f"Content-Type: {content_type}\r\n\r\n"
).encode('utf-8') + image_data + f"\r\n--{boundary}--\r\n".encode('utf-8')
# Set up the HTTP connection and headers
conn = http.client.HTTPSConnection('')
headers = {
'Content-Type': f'multipart/form-data; boundary={boundary}',
'x-api-key': apiKey
# Make the POST request
conn.request('POST', '/v1/segment', body=body, headers=headers)
response = conn.getresponse()
# Handle the response
if response.status == 200:
response_data =
with open(output_image_path, 'wb') as out_f:
print("Image saved to", output_image_path)
print(f"Error: {response.status} - {response.reason}")
# Close the connection

Step 3: Call the API

Now it's time to call the API in your code! Just use the function remove_background() and pass as arguments both the path of the original image and the path where the result image should be saved.
from remove_background import remove_background
# Example usage
input_path = "test.jpg"
output_path = "result.png"
remove_background(input_path, output_path)

You're done!

That's it! With just these three easy steps, you can integrate the PhotoRoom Background Removal API into your Python project and provide your users with high-quality images with clean backgrounds.
According to resellers who use the PhotoRoom app, this feature can increase sales by 20 up to 100%.
If you want to learn more about the PhotoRoom API and get your API key, visit