Can I use the URL of an image?

The Remove Background API requires you to upload the content of your image file when making the API call.

However, if your images are already hosted online, it's possible that you don't have the image file itself, but rather a URL that points to the file.

If that's the case, there are two solutions for you to process your image through the Photoroom API.

Option 1: Download the image locally

In most situations, you should be able to use the image URL to download the content of the image, store it in a local variable, and then use it when calling the Remove Background API.

Here's an example code in JavaScript of how to implement this workflow:

const axios = require('axios');
const FormData = require('form-data');
const fs = require('fs');
const path = require('path');

async function downloadImageAndCallAPI(imageUrl) {
    try {
        // Step 1: Download the image in the local variable `imageBytes`
        const response = await axios.get(imageUrl, {
            responseType: 'arraybuffer'
        });
        
        const imageBytes = response.data;

        // Step 2: Call to the Remove Background API
        const form = new FormData();
        form.append('image_file', imageBytes, 'image.jpg');

        // Step 3: Make an API call with the image bytes in multipart form
        const uploadResponse = await axios.post("https://sdk.photoroom.com/v1/segment", form, {
            headers: {
                "x-api-key": "REPLACE_WITH_YOUR_API_KEY",
                ...form.getHeaders(),
            },
            responseType: 'arraybuffer'
        });

        // Step 3: Save the result image locally
        const localPath = path.join(__dirname, 'photoroom-result.png');
        fs.writeFileSync(localPath, uploadResponse.data);
    } catch (error) {
        console.error('An error occurred:', error.message);
    }
}

Option 2: Use the Image Editing API

If downloading the content of the image in a local variable is absolutely not possible for you, then you can use our Image Editing API to process your images.

The Image Editing API does offer a GET endpoint, thanks to which you can use the argument imageUrl to pass the URL of your image.

The Image Editing API is part of our Plus plan and calling this API is more expensive than calling the Remove Backhround API.

Make sure to review our API pricing.

Last updated