How to Remove Image Backgrounds in Go

A complete guide to integrating FAPIhub's background removal API into your Go project. Takes less than 10 minutes.

This guide shows how to call the FAPIhub background removal API from Go using the standard library. No external dependencies required for basic uploads.

Prerequisites

  • A FAPIhub API key (get one free at fapihub.com/dashboard)
  • Go 1.20 or higher installed
  • Basic understanding of HTTP requests

Step-by-Step Integration

1

Install dependencies

Install any libraries needed for HTTP requests.

Install Go 1.20+ and set up your GOPATH. No external packages required for basic usage.
2

Get your API key

Create a free account and copy your API key from the dashboard.

3

Basic background removal

Upload an image and receive transparent PNG output.

package main

import (
  "bytes"
  "fmt"
  "io"
  "mime/multipart"
  "net/http"
  "os"
)

func main() {
  f, err := os.Open("photo.jpg")
  if err != nil { panic(err) }
  defer f.Close()

  var b bytes.Buffer
  w := multipart.NewWriter(&b)
  fw, _ := w.CreateFormFile("image", "photo.jpg")
  if _, err := io.Copy(fw, f); err != nil { panic(err) }
  w.Close()

  req, _ := http.NewRequest("POST", "https://fapihub.com/v2/rembg/", &b)
  req.Header.Set("token", "YOUR_API_KEY")
  req.Header.Set("Content-Type", w.FormDataContentType())

  client := &http.Client{}
  resp, err := client.Do(req)
  if err != nil { panic(err) }
  defer resp.Body.Close()

  out, _ := os.Create("result.png")
  defer out.Close()
  io.Copy(out, resp.Body)
  fmt.Println("Saved result.png")
}
4

Save and verify output

Write API response bytes to a PNG file and verify output.

// The basic example saves the API response to result.png. 
// You can also check the file size: fmt.Println("Saved result.png size:", fileInfo.Size())
5

Handle errors

Check HTTP status codes and catch network errors.

// Check HTTP status and read response body on error
if resp.StatusCode != 200 {
  body, _ := io.ReadAll(resp.Body)
  fmt.Printf("API error: %d - %s
", resp.StatusCode, string(body))
}
6

Batch processing

Loop through image paths for high-volume jobs.

package main

import (
  "io"
  "mime/multipart"
  "net/http"
  "os"
  "path/filepath"
)

func process(path string) {
  f, _ := os.Open(path)
  defer f.Close()
  var b bytes.Buffer
  w := multipart.NewWriter(&b)
  fw, _ := w.CreateFormFile("image", filepath.Base(path))
  io.Copy(fw, f)
  w.Close()
  req, _ := http.NewRequest("POST", "https://fapihub.com/v2/rembg/", &b)
  req.Header.Set("token", "YOUR_API_KEY")
  req.Header.Set("Content-Type", w.FormDataContentType())
  client := &http.Client{}
  resp, _ := client.Do(req)
  defer resp.Body.Close()
  out, _ := os.Create(path + "_nobg.png")
  defer out.Close()
  io.Copy(out, resp.Body)
}

func main() {
  // iterate over images in folder and call process(path)
}

Original vs Result

See what the API returns on real examples. Each pair shows the untouched source image and the cleaned FAPIhub result.

Ready to Start Removing Backgrounds in Go?

Free tier includes 100 full-resolution requests/month. No credit card. Your API key is ready in 30 seconds.

Get Your Free API Key