Go Backend Integration
How to call Comet Engine from a Go service (e.g. app-core-backend).
HTTP Client
go
import (
"encoding/json"
"net/http"
"net/url"
"strconv"
)
type CometClient struct {
BaseURL string
APIKey string
APISecret string
HTTPClient *http.Client
}
func NewCometClient(baseURL, apiKey, apiSecret string) *CometClient {
return &CometClient{
BaseURL: baseURL,
APIKey: apiKey,
APISecret: apiSecret,
HTTPClient: &http.Client{Timeout: 30 * time.Second},
}
}Request Helpers
go
func (c *CometClient) doRequest(method, path string, body interface{}) (map[string]interface{}, error) {
var reqBody *bytes.Buffer
if body != nil {
b, _ := json.Marshal(body)
reqBody = bytes.NewBuffer(b)
} else {
reqBody = bytes.NewBuffer(nil)
}
req, _ := http.NewRequest(method, c.BaseURL+path, reqBody)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-API-Key", c.APIKey)
req.Header.Set("X-API-Secret", c.APISecret)
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var result map[string]interface{}
json.NewDecoder(resp.Body).Decode(&result)
return result, nil
}Example: Wallet Operations
go
// Create wallet (tenant is inferred from API key)
resp, _ := c.doRequest("POST", "/api/v1/wallets/create", map[string]interface{}{
"userId": 12345,
})
// Get balance
params := url.Values{}
params.Set("userId", "12345")
resp, _ := c.doRequest("GET", "/api/v1/wallets/balance?"+params.Encode(), nil)
// Mint tokens
resp, _ = c.doRequest("POST", "/api/v1/assets/usdt/mint", map[string]interface{}{
"userId": 12345,
"amountBase": "10000000",
"externalId": "mint-001",
})Example: AMM Swap
go
// Get quote
params := url.Values{}
params.Set("from", "USDT")
params.Set("to", "IMC")
params.Set("amountIn", "1000000")
quote, _ := c.doRequest("GET", "/api/v1/swap/quote?"+params.Encode(), nil)
// Execute swap
swapResp, _ := c.doRequest("POST", "/api/v1/swap/tokens", map[string]interface{}{
"userId": 12345,
"from": "USDT",
"to": "IMC",
"amountIn": "1000000",
})Error Handling
go
// All responses follow this shape:
// { "status": "success", "data": {...} }
// { "error": "description" }
type CometResponse struct {
Status string `json:"status"`
Data map[string]interface{} `json:"data,omitempty"`
Error string `json:"error,omitempty"`
}