Install
openclaw skills install @psyb0t/go-xaiUnofficial Go client for the xAI (Grok) REST API at https://api.x.ai. xai.NewClientWithResponses from github.com/psyb0t/go-xai gives one typed method per endpoint, generated with oapi-codegen from xAI's OpenAPI document - chat completions, the Responses API, Anthropic style messages, embeddings, tokenization, image and video generation, files, skills, document search, models and API key info - with JSON 200 bodies decoded into Go structs. Auth is a bearer API key set through a request editor. Use when Go code needs to call the xAI API or already imports go-xai.
openclaw skills install @psyb0t/go-xaiTyped Go client for the xAI API. Every endpoint in xAI's OpenAPI document is a method on ClientWithResponses.
For install, the method list, union types and error handling, see references/setup.md.
XAI_API_KEY. Never hardcode it, commit it or log it, and never print request headers.NewClientWithResponses. Use https://api.x.ai.github.com/psyb0t/go-xai.Client method, which returns the *http.Response with the body unread, and parse the stream yourself.import (
"context"
"fmt"
"net/http"
"os"
"github.com/psyb0t/go-xai"
)
auth := xai.WithRequestEditorFn(func(_ context.Context, req *http.Request) error {
req.Header.Set("Authorization", "Bearer "+os.Getenv("XAI_API_KEY"))
return nil
})
client, err := xai.NewClientWithResponses("https://api.x.ai", auth)
if err != nil {
return err
}
models, err := client.HandleModelsListRequestWithResponse(ctx)
if err != nil {
return err // transport error
}
if models.JSON200 == nil {
return fmt.Errorf("list models: status %d: %s", models.StatusCode(), models.Body)
}
for _, model := range models.JSON200.Data {
fmt.Println(model.Id)
}
A chat completion. Messages and message content are unions, built with From... helpers (Message2 is the user message, Content0 is plain text):
var content xai.Content
if err := content.FromContent0("Say hello"); err != nil {
return err
}
var msg xai.Message
if err := msg.FromMessage2(xai.Message2{Content: content, Role: xai.Message2RoleUser}); err != nil {
return err
}
model := "<model-id>" // pick one from the models list
messages := []xai.Message{msg}
res, err := client.HandleGenericCompletionRequestWithResponse(ctx, xai.ChatRequest{
Model: &model,
Messages: &messages,
})
if err != nil {
return err
}
if res.JSON200 == nil {
return fmt.Errorf("chat: status %d: %s", res.StatusCode(), res.Body)
}
if len(res.JSON200.Choices) > 0 && res.JSON200.Choices[0].Message.Content != nil {
fmt.Println(*res.JSON200.Choices[0].Message.Content)
}