Go HTTP Integration
Go projects can directly call CoreRouter using the standard library without depending on specific SDKs. This is suitable for server-side, command-line tools, and scenarios requiring full control over retries, timeouts, and logging.
Basic Request
go
package main
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"os"
"time"
)
func main() {
apiKey := os.Getenv("COREROUTER_API_KEY")
if apiKey == "" {
panic("missing COREROUTER_API_KEY")
}
body := []byte(`{
"model": "claude-sonnet-4-5",
"messages": [
{"role": "user", "content": "Hello, please introduce yourself."}
]
}`)
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(
ctx,
http.MethodPost,
"https://api.corerouter.cloud/v1/chat/completions",
bytes.NewReader(body),
)
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+apiKey)
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
panic(fmt.Sprintf("request failed: %s\n%s", resp.Status, data))
}
fmt.Println(string(data))
}Production Environment Recommendations
- Must set timeout; don't directly use requests without timeout control.
- Log
status_codeand error summary; don't log complete API Key. - Implement limited retries with exponential backoff for
429,502,503,504. - Make
model,base_url, and API Key configurable for easy model and environment switching. - Streaming output requires reading Server-Sent Events line by line; don't wait for entire response to finish.
Environment Variables
bash
export COREROUTER_API_KEY="sk-xxxxxxxxxxxxxxxx"
export COREROUTER_BASE_URL="https://api.corerouter.cloud/v1"
export COREROUTER_MODEL="claude-sonnet-4-5"When deploying to servers, containers, or CI, use the platform's Secret management feature to save API Keys.
