Remove image backgrounds in Go
The standard library's mime/multipart package is all you need — no third-party dependency, which fits Go's minimal-dependency conventions well.
Remove a background
package main
import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
)
func main() {
file, _ := os.Open("photo.jpg")
defer file.Close()
var body bytes.Buffer
writer := multipart.NewWriter(&body)
part, _ := writer.CreateFormFile("input", "photo.jpg")
io.Copy(part, file)
writer.WriteField("format", "png")
writer.Close()
req, _ := http.NewRequest("POST", "https://api.bgbuster.com/images/remove-background", &body)
req.Header.Set("Content-Type", writer.FormDataContentType())
req.Header.Set("X-Api-Key", "your-api-key")
resp, _ := http.DefaultClient.Do(req)
defer resp.Body.Close()
respBody, _ := io.ReadAll(resp.Body)
fmt.Println(string(respBody))
}The request is a multipart POST to https://api.bgbuster.com/images/remove-background, authenticated with your API key in the X-Api-Key header. Pass the image as a file (input) or as a public URL string.
By default the response is JSON with a temporary download url. That URL stays valid for 24 hours — download and store the image yourself if you need it longer.
Need more than a plain removal? POST /images/edit takes the same kind of request and chains background removal together with resize, rotate, flip, blur, brightness/contrast/saturation, and trim — in whatever order you list them.
| Field | Type | Required | Description |
|---|---|---|---|
| input | File or string | Yes | Image file or a public URL to fetch it from. |
| output | "url" or "raw" | No | Return a temporary download URL, or the transformed image bytes directly. Defaults to "url". |
| format | "png" or "webp" | No | Output image format. Defaults to "png". |
| trim | boolean | No | Trim empty transparent space around the result. |
Default (output=url)
{
"id": "image-id",
"url": "https://..."
}With output=raw, save the image bytes directly instead of following a URL
package main
import (
"bytes"
"io"
"mime/multipart"
"net/http"
"os"
)
func main() {
file, _ := os.Open("photo.jpg")
defer file.Close()
var body bytes.Buffer
writer := multipart.NewWriter(&body)
part, _ := writer.CreateFormFile("input", "photo.jpg")
io.Copy(part, file)
writer.WriteField("format", "png")
writer.WriteField("output", "raw")
writer.Close()
req, _ := http.NewRequest("POST", "https://api.bgbuster.com/images/remove-background", &body)
req.Header.Set("Content-Type", writer.FormDataContentType())
req.Header.Set("X-Api-Key", "your-api-key")
resp, _ := http.DefaultClient.Do(req)
defer resp.Body.Close()
out, _ := os.Create("result.png")
defer out.Close()
io.Copy(out, resp.Body)
}| Status | Code | Meaning |
|---|---|---|
| 400 | IMAGE_TOO_LARGE | The input image exceeds the size limit. |
| 400 | IMAGE_DECODE_ERROR | The input file couldn't be decoded as an image. |
| 400 | INVALID_IMAGE_URL | The URL passed as input didn't return image content. |
| 400 | UPSTREAM_ERROR | The input URL couldn't be fetched. |
| 401 | UNAUTHORIZED | The API key is missing or invalid. |
| 402 | INSUFFICIENT_BALANCE | Your account is out of credits. |
| 403 | IMAGE_FORBIDDEN | The source CDN blocked the request for the input URL. |
| 429 | TOO_MANY_REQUESTS | Rate limit exceeded — retry after a short delay. |
| 500 | IMAGE_TRANSFORMATION_ERROR | The background removal step failed unexpectedly. |
- 1
Sign up with an email or a social login — no credit card required.
- 2
Buy a credit pack. Credits never expire, and you'll need a balance before the API accepts requests.
- 3
From the dashboard, go to API Keys and click Create API key.
- 4
Authenticate your requests
Send the key in the X-Api-Key header on every request to api.bgbuster.com.
