Simply copy and paste the code into your project, and update the placeholder with your own API key.
consturl='https://sdk.photoroom.com/v1/segment';// Please replace with your own apiKeyconstapiKey="YOUR_API_KEY_HERE";exportasyncfunctionremoveBackground(imageFile:File):Promise<Blob>{constformData=newFormData();formData.append('image_file', imageFile);constresponse=awaitfetch(url,{ method:'POST', headers:{'X-Api-Key': apiKey}, body: formData});if (!response.ok) {console.error(response.json())thrownewError('Network response was not ok');}constimageBlob:Blob=awaitresponse.blob();return imageBlob;}
Warning: Please be aware that this will make your API key accessible to anyone browsing your website. If your website is public, we recommend calling our API through a proxy that you control (Firebase / Google functions, AWS lambda).
Step 3: Call the API
Now it's time to call the API in your code! Just use the function removeBackground() and pass in the file your user has selected.
The function will return a Blob containing a new image with the background removed.
You're done!
That's it! With just these three easy steps, you can integrate the Photoroom Background Removal API into your web app 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%.