Go · net/http

Use Backfire from Go

Go already has a battle-tested HTTP client, so the relay integration is a struct, a request and a decode. Nothing is added to your module graph.

Notes for this client

  • Use a context with a deadline so the call cannot outlive your handler.
  • Decode into a struct that only lists the fields you need — the envelope carries more.
  • Encode binary bodies with body_base64 rather than trying to send raw bytes as JSON.
  • Always check Ok before reading Upstream.Status.

Working example

go
payload := []byte(`{"url":"https://api.github.com/zen","method":"GET","timeout_ms":8000}`)

req, _ := http.NewRequestWithContext(ctx, "POST", "https://backfireapi.base44.app/functions/forward", bytes.NewReader(payload))
req.Header.Set("Authorization", "Bearer "+os.Getenv("BACKFIRE_KEY"))
req.Header.Set("Content-Type", "application/json")

res, err := http.DefaultClient.Do(req)
if err != nil {
    return err
}
defer res.Body.Close()

var envelope struct {
    Ok       bool `json:"ok"`
    Body     struct{ Text string `json:"text"` } `json:"body"`
    Upstream struct{ Status int `json:"status"` } `json:"upstream"`
    Meta     struct{ TripsCharged int `json:"trips_charged"` } `json:"meta"`
}
if err := json.NewDecoder(res.Body).Decode(&envelope); err != nil {
    return err
}
fmt.Println(envelope.Upstream.Status, envelope.Meta.TripsCharged)

Questions about this

Other languages

Try it with 5,000 free trips.

No card, no subscription, and the balance never expires.