Home / Docs / Go

Go

Call the gateway from Go with the standard library — no third-party client needed.

Both endpoints are plain JSON over HTTP, so net/http is sufficient.

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "net/http"
    "os"
    "time"
)

type msg struct {
    Role    string `json:"role"`
    Content string `json:"content"`
}

type req struct {
    Model     string `json:"model"`
    MaxTokens int    `json:"max_tokens"`
    Messages  []msg  `json:"messages"`
}

type resp struct {
    Content []struct {
        Text string `json:"text"`
    } `json:"content"`
    Usage struct {
        InputTokens  int `json:"input_tokens"`
        OutputTokens int `json:"output_tokens"`
    } `json:"usage"`
}

func main() {
    body, _ := json.Marshal(req{
        Model:     "claude-sonnet-4-6",
        MaxTokens: 1024,
        Messages:  []msg{{Role: "user", Content: "Hello"}},
    })

    r, _ := http.NewRequest("POST", "https:">//aiprimetech.io/v1/messages", bytes.NewReader(body))
    r.Header.Set("x-api-key", os.Getenv("CLAUDEAPIKEY"))
    r.Header.Set("anthropic-version", "2023-06-01")
    r.Header.Set("content-type", "application/json")

    client := &http.Client{Timeout: 120 * time.Second}
    res, err := client.Do(r)
    if err != nil {
        panic(err)
    }
    defer res.Body.Close()

    var out resp
    json.NewDecoder(res.Body).Decode(&out)
    fmt.Println(out.Content[0].Text)
    fmt.Println(out.Usage.InputTokens, out.Usage.OutputTokens)
}
Always set an explicit Timeout on the client. Go's default http.Client has none, so a stalled connection blocks a goroutine indefinitely.