JavaScript API Integration – ByByBG Documentation
⚡ JavaScript SDK

JavaScript Integration

Learn how to upload images, process background removal requests, and display transparent PNG results using JavaScript and Fetch API.

INTRODUCTION

JavaScript Upload Workflow

JavaScript can upload images, send requests to backend endpoints, and display AI processed images instantly inside web applications.

Important: Never place private API keys directly inside frontend JavaScript files.
📤

Select File

Users upload images directly from their browser using file input fields.

Send Request

JavaScript sends image files securely to backend endpoints using Fetch API.

🤖

AI Processing

The ByByBG API removes image backgrounds automatically using advanced AI models.

🖼️

Show Result

Transparent PNG images are displayed instantly after processing completes.

STEP 1

HTML Structure

Create a simple image upload field and result container for users.

<input type="file"
id="imageInput">

<button onclick="removeBg()">
    Remove Background
</button>

<div id="result"></div>
STEP 2

JavaScript Fetch API Example

Upload images using async JavaScript requests and display processed PNG results instantly.

async function removeBg(){

    const fileInput =
    document.getElementById("imageInput");

    const result =
    document.getElementById("result");

    if(!fileInput.files.length){

        alert("Select image first");

        return;
    }

    const formData = new FormData();

    formData.append(
        "file",
        fileInput.files[0]
    );

    result.innerHTML =
    "Processing...";

    try{

        const response = await fetch(
            "/ajax/remove-bg.php",
            {
                method:"POST",
                body:formData
            }
        );

        if(!response.ok){
            throw new Error();
        }

        const blob =
        await response.blob();

        const url =
        URL.createObjectURL(blob);

        result.innerHTML = `
            <img src="${url}"
            style="max-width:100%;">

            <br><br>

            <a href="${url}"
            download="no-bg.png">
                Download PNG
            </a>
        `;

    }catch{

        result.innerHTML =
        "Something went wrong";

    }
}
FEATURES

Recommended UI Features

  • Drag and drop uploads.
  • Progress indicators and loaders.
  • Image previews before upload.
  • Animated processing states.
  • Download buttons for PNG images.
  • Friendly error handling messages.
SECURITY

Security Recommendations

Frontend JavaScript should never expose private API keys publicly.
  • Use backend proxy endpoints for API requests.
  • Validate uploaded images server-side.
  • Apply upload size limits.
  • Restrict unsupported image formats.
  • Use rate limiting and request throttling.
ERRORS

Common JavaScript Errors

Error
Description
401
Invalid or missing API key.
413
Uploaded image file is too large.
429
Too many requests. Rate limit exceeded.
500
Internal server processing error.

Continue to Python Integration

Learn backend automation workflows and server-side integrations using Python and the requests library.