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.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.
Install any libraries needed for HTTP requests.
Install Go 1.20+ and set up your GOPATH. No external packages required for basic usage.Create a free account and copy your API key from the dashboard.
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")
}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())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))
}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)
}See what the API returns on real examples. Each pair shows the untouched source image and the cleaned FAPIhub result.
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")
}
// Send to /v2/rembg/blur/ with form field blur_radius=15
var b bytes.Buffer
w := multipart.NewWriter(&b)
fw, _ := w.CreateFormFile("image", "photo.jpg")
io.Copy(fw, f)
w.WriteField("blur_radius", "15")
w.Close()
// make request as shown in basic example, use URL /v2/rembg/blur/
// Send to /v2/rembg/color/ with form field background_color=255,255,255
w.WriteField("background_color", "255,255,255")
// make request as shown in basic example, use URL /v2/rembg/color/
// Send to /v2/rembg/gradient/ with top_color and bottom_color fields
w.WriteField("top_color", "0,0,255")
w.WriteField("bottom_color", "255,255,255")
// make request as shown in basic example, use URL /v2/rembg/gradient/
// Send to /v2/rembg/shadow/ with shadow params like offset and blur
w.WriteField("offset", "10")
w.WriteField("blur", "15")
// make request as shown in basic example, use URL /v2/rembg/shadow/Free tier includes 100 full-resolution requests/month. No credit card. Your API key is ready in 30 seconds.
Get Your Free API Key