Building a Go Bitcoin Balance Client for AI Agents
Research question
A small Go HTTP client can isolate the wallet credential from model-generated tool arguments. The fixed balance route uses the documented net and type fields.
This is an independent educational reference example for WAB Engine API v5, not a required SDK or a live-tested integration. The wallet must already be restored by its owner. All wallet requests in this study use Testnet.
Reference implementation
1package main
2import (
3 "bytes"; "encoding/json"; "errors"; "fmt"; "io"; "net/http"; "os"; "time"
4)
5func balance(key string) (json.RawMessage,error) {
6 if key=="" {return nil,errors.New("missing BTC key")}
7 c:= &http.Client{Timeout:10*time.Second, Transport:&http.Transport{}, CheckRedirect:func(*http.Request,[]*http.Request)error{return http.ErrUseLastResponse}}
8 req,err:=http.NewRequest("POST","http://127.0.0.1:7070/api/v5/btc/wallet/balance",bytes.NewBufferString(`{"net":"test","type":"segwit"}`)); if err!=nil{return nil,err}
9 req.Header.Set("Content-Type","application/json"); req.Header.Set("X-BTC-KEY",key)
10 resp,err:=c.Do(req); if err!=nil{return nil,errors.New("engine unavailable")}; defer resp.Body.Close()
11 b,err:=io.ReadAll(io.LimitReader(resp.Body,1048577)); if err!=nil||len(b)>1048576{return nil,errors.New("invalid response size")}
12 var e struct{OK bool `json:"ok"`; Result json.RawMessage `json:"result"`}
13 if resp.StatusCode!=200||json.Unmarshal(b,&e)!=nil||!e.OK||len(e.Result)==0||bytes.Equal(e.Result,[]byte("null")){return nil,errors.New("balance unavailable")}; return e.Result,nil
14}
15func main(){_,err:=balance(os.Getenv("WAB_BTC_KEY")); if err!=nil{fmt.Fprintln(os.Stderr,err);os.Exit(1)};fmt.Println("Balance response received; inspect locally in your application.")}How to apply the example
Save as main.go and review the code. With Go installed and a restored Testnet wallet on the local engine, set WAB_BTC_KEY privately and run go run main.go. This makes a real balance query only if you choose to run it.
Engineering trade-offs and failure modes
HTTP success alone is not wallet success: inspect the JSON envelope and reject missing results. The example deliberately does not expose raw balances to a model. A timeout is not a zero balance.
The engine remains authoritative for wallet state and transaction execution. Do not expose wallet credentials, raw private wallet data or recovery material to the model. No SEND is performed by this study. Review the deployed API contract before adapting the example; no live engine tests have been performed.
