Published
What you are building
Fileboy has no Go package, and does not need one: an upload is one multipart request. This handler takes a file from your own form and sends it on, with only the standard library.
You need a public key. Create a free app and copy it from the Keys page. Keep keys in environment variables, and never put a private key in code that reaches the browser. An upload needs only the public key; add the private key only if your app uses signed requests.
1. The upload request
Send a POST to /files/upload with the key in an Authorization header, the file in a file field and, optionally, a data field of JSON with the file's name and metadata. The answer is JSON: { "data": { ... } } with the file's id, or an error.
curl https://api.fileboy.io/files/upload \
-H "Authorization: public YOUR_PUBLIC_KEY" \
-F "file=@./photo.jpg" \
-F 'data={"name":"photo.jpg"}'2. The same request in Go
The request to Fileboy is written through a pipe, so it streams out as it is read and is never built up in memory. The incoming form is a different matter: r.FormFile parses it first, holding up to 32 MB in memory (more goes to a temporary file), which the 20 MB limit below keeps it under. To stream the incoming file too, read it with r.MultipartReader().
package main
import (
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
)
const fileboyURL = "https://api.fileboy.io"
type fileboyFile struct {
ID string `json:"id"`
Name string `json:"name"`
Mime string `json:"mime"`
}
// uploadToFileboy sends a file to Fileboy and returns its details. The id is what to store.
func uploadToFileboy(name string, body io.Reader) (*fileboyFile, error) {
pr, pw := io.Pipe()
form := multipart.NewWriter(pw)
go func() {
part, err := form.CreateFormFile("file", name)
if err == nil {
_, err = io.Copy(part, body)
}
if err == nil {
meta, _ := json.Marshal(map[string]string{"name": name})
err = form.WriteField("data", string(meta))
}
if err == nil {
err = form.Close()
}
pw.CloseWithError(err)
}()
req, err := http.NewRequest("POST", fileboyURL+"/files/upload", pr)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "public "+os.Getenv("FILEBOY_PUBLIC_KEY"))
req.Header.Set("Content-Type", form.FormDataContentType())
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
raw, _ := io.ReadAll(res.Body)
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("fileboy: %s: %s", res.Status, raw)
}
var out struct {
Data fileboyFile `json:"data"`
}
if err := json.Unmarshal(raw, &out); err != nil {
return nil, err
}
return &out.Data, nil
}
func avatarHandler(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, 20<<20) // refuse bodies over 20 MB
file, header, err := r.FormFile("avatar")
if err != nil {
http.Error(w, "Choose a file", http.StatusBadRequest)
return
}
defer file.Close()
uploaded, err := uploadToFileboy(header.Filename, file)
if err != nil {
http.Error(w, "The upload failed", http.StatusBadGateway)
return
}
// save uploaded.ID against the user here
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"id": uploaded.ID})
}
func main() {
http.HandleFunc("POST /avatar", avatarHandler)
http.ListenAndServe(":8080", nil)
}"POST /avatar" as a route pattern needs Go 1.22 or later. On an older version, register "/avatar" and check r.Method yourself.
3. Show it at the size you need
Build the URL from the stored ID. You are only editing a query string, so there is nothing to call:
func avatarURL(id string, size int) string {
return fmt.Sprintf(
"https://api.fileboy.io/images/get/%s?transform=resize:%dx%d~fit:cover;format:webp",
id, size*2, size*2,
)
}The transform syntax lists every step. Reading a file or image needs no key, so these URLs work in <img> tags, emails and anywhere else, unless the app requires signed URLs.
Checked against the current libraries in November 2026. Found something that no longer works? Tell us.