Kotlin API Integration – ByByBG Documentation
🤖 Kotlin SDK

Kotlin Integration

Integrate AI background removal into native Android applications using Kotlin, OkHttp, and multipart image uploads.

INTRODUCTION

Kotlin Android Integration

Use Kotlin and OkHttp to upload images and remove backgrounds directly inside Android applications using AI-powered processing.

Recommended: Use secure backend proxy APIs in production applications to protect private API credentials.
STEP 1

Add OkHttp Dependency

Add the OkHttp library inside your Gradle dependencies before making API requests.

implementation(
"com.squareup.okhttp3:okhttp:4.12.0"
)
STEP 2

Import Required Classes

Import OkHttp and File classes before creating multipart upload requests.

import okhttp3.*
import java.io.File
STEP 3

Kotlin Upload Example

Upload images and receive transparent PNG results using Kotlin and OkHttp multipart requests.

val client = OkHttpClient()

val file =
File("/storage/image.jpg")

val requestBody =
MultipartBody.Builder()
.setType(MultipartBody.FORM)

.addFormDataPart(

    "file",

    file.name,

    file.asRequestBody(
        "image/jpeg"
        .toMediaType()
    )

)

.build()

val request = Request.Builder()

.url(
"https://api.bybybg.com/v1/remove-bg"
)

.addHeader(
"X-API-Key",
"YOUR_API_KEY"
)

.post(requestBody)

.build()

client.newCall(request)
.enqueue(object : Callback {

    override fun onFailure(
        call: Call,
        e: IOException
    ) {

        println("Failed")

    }

    override fun onResponse(
        call: Call,
        response: Response
    ) {

        if(response.isSuccessful){

            println(
            "Background removed"
            )

        }else{

            println(
            "Error: ${response.code}"
            )

        }
    }
})
FEATURES

Recommended Android Features

📷

Camera Support

Capture images directly using Android camera APIs.

🖼️

Gallery Picker

Allow users to select images from their device gallery.

Progress Loader

Display animated loading indicators during AI image processing.

💾

Save PNG

Store transparent PNG images inside local device storage.

BEST PRACTICES

Recommended Practices

  • Compress images before uploading.
  • Validate supported image formats.
  • Handle connection failures and retries.
  • Never expose private API keys publicly.
  • Use backend APIs in production environments.
ERRORS

Common API Errors

Status
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 Error Handling

Learn about API limits, status codes, troubleshooting, and error handling best practices.