API Key Authentication & Setup Guide – ByByBG

API Key Authentication & Setup

This guide explains step by step how to securely store and use your ByByBG API key. All examples below are safe to copy and will not cause server errors.

Step 1: Get Your API Key

  • Login to your ByByBG dashboard
  • Open the API Keys section
  • Copy your API key (example: bg_live_xxxxx)

Step 2: Create API Config File (PHP)

Create this file in your backend project:


config/bybybg.php
        

Paste the following code into config/bybybg.php and replace YOUR_API_KEY_HERE with your real API key.


<?php

return [
    "api_key" => "YOUR_API_KEY_HERE",
    "api_url" => "https://bybybg.com/v1/remove-bg"
];

?>
        

Step 3: Create Backend Endpoint

This backend file will receive the image from frontend, attach your API key securely, and call the ByByBG API.

Create this file:


web/remove-bg.php
        

Paste the following code inside web/remove-bg.php:


<?php

$config = require dirname(__DIR__) . "/config/bybybg.php";

if (!isset($_FILES["file"])) {
    http_response_code(400);
    exit("No image uploaded");
}

$file = new CURLFile(
    $_FILES["file"]["tmp_name"],
    mime_content_type($_FILES["file"]["tmp_name"]),
    $_FILES["file"]["name"]
);

$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => $config["api_url"],
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => [
        "X-API-Key: " . $config["api_key"]
    ],
    CURLOPT_POSTFIELDS => [
        "file" => $file
    ],
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 60
]);

$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($status !== 200) {
    http_response_code($status);
    exit("API Error");
}

header("Content-Type: image/png");
echo $response;

?>
        

Step 4: Call Backend from Frontend

Your frontend (HTML / JavaScript) should send images to the backend endpoint below:


/web/remove-bg.php
        

👉 Important:
The API key is used only in backend files. Never expose it in frontend JavaScript or HTML.

Common Mistakes (Avoid These)

  • ❌ Writing raw <?php inside HTML
  • ❌ Putting API key in JavaScript
  • ❌ Uploading config files to public repositories

Best Practices

  • Store API keys in config files or environment variables
  • Restrict API key usage from dashboard
  • Rotate API keys if compromised