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.
bg_live_xxxxx)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"
];
?>
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;
?>
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.
<?php inside HTML