The unofficial Go SDK for xAI provides a first-class, idiomatic Go interface to xAI's powerful AI capabilities. This SDK enables Go developers to integrate chat completions, streaming responses, and upcoming features like file operations, image generation, and more.
Production Ready: v0.2.1 - Hotfix release with 100% proto alignment to xAI Python SDK v1.4.0. Verified and running in production on Zaguán. All 14 proto files aligned (108 messages, 18 enums). Chat API fully functional and tested.
xAI's Grok models offer powerful AI capabilities, but integrating them into Go applications requires careful handling of HTTP requests, streaming responses, and error management.
xAI SDK for Go provides a clean, idiomatic Go interface that handles all the complexity for you. Focus on building your application while the SDK manages authentication, connection pooling, retries, and streaming.
✓ Production Verified: This SDK is actively used in production on Zaguán, handling real-world workloads with proven reliability.
Synchronous and streaming chat with message builders. Clean API for single-shot and conversational interactions.
Define and use tools in your chat completions. Enable AI to call functions and interact with external systems.
Control reasoning effort levels and enable search capabilities for enhanced AI responses.
Get structured JSON and JSON schema outputs. Perfect for parsing AI responses into typed data structures.
API key and Bearer token support with TLS. Environment variable integration for safe credential management.
Environment variables and programmatic config. Customize timeouts, retries, and connection settings.
Health checks, automatic retries, and keepalive. Robust connection handling for production workloads.
Comprehensive error types with gRPC integration. Clear error messages for debugging and monitoring.
Comprehensive test coverage for all components. Reliable and production-ready code.
go get github.com/ZaguanLabs/xai-sdk-gopackage main
import (
"context"
"fmt"
"log"
"github.com/ZaguanLabs/xai-sdk-go/pkg/xai"
)
func main() {
// Create client with API key from environment
client := xai.NewClient()
// Create a chat completion
resp, err := client.Chat.Create(context.Background(), &xai.ChatRequest{
Model: "grok-beta",
Messages: []xai.Message{
{Role: "user", Content: "What is the meaning of life?"},
},
})
if err != nil {
log.Fatal(err)
}
fmt.Println(resp.Choices[0].Message.Content)
}package main
import (
"context"
"fmt"
"log"
"github.com/ZaguanLabs/xai-sdk-go/pkg/xai"
)
func main() {
client := xai.NewClient()
stream, err := client.Chat.CreateStream(context.Background(), &xai.ChatRequest{
Model: "grok-beta",
Messages: []xai.Message{
{Role: "user", Content: "Tell me a story"},
},
Stream: true,
})
if err != nil {
log.Fatal(err)
}
defer stream.Close()
// Process streaming chunks
for {
chunk, err := stream.Recv()
if err != nil {
break
}
fmt.Print(chunk.Choices[0].Delta.Content)
}
}Set your xAI API key as an environment variable:
export XAI_API_KEY=your-api-key-herexai-sdk-go/ ├── pkg/ │ └── xai/ │ ├── client.go # Main client implementation │ ├── chat.go # Chat completions API │ ├── stream.go # Streaming support │ ├── config.go # Configuration management │ ├── errors.go # Error types and handling │ └── types.go # Request/response types ├── examples/ │ ├── basic/ # Basic usage examples │ ├── streaming/ # Streaming examples │ └── advanced/ # Advanced features ├── go.mod └── README.md
The SDK supports both environment variables and programmatic configuration:
XAI_API_KEY - Your xAI API keyXAI_BASE_URL - Custom API base URL (optional)XAI_TIMEOUT - Request timeout in seconds (optional)client := xai.NewClient(
xai.WithAPIKey("your-api-key"),
xai.WithBaseURL("https://api.x.ai/v1"),
xai.WithTimeout(30 * time.Second),
xai.WithRetries(3),
)Foundation through v0.1.6 - Core infrastructure, proto definitions, configuration, client, authentication, chat completions with function calling and structured outputs. Production-verified on Zaguán.
v0.2.0-v0.2.1 - 100% proto alignment with xAI Python SDK v1.4.0. All 14 proto files aligned (108 messages, 18 enums). New proto files: deferred, documents, embed, sample, types, shared, usage. Complete chat proto with all 37 messages. Embed API wrapper added.
SDK wrapper implementations: Deferred completions API, Documents search API, Updated files and collections wrappers
Image generation API, Tokenization API, Enhanced error handling, Integration tests
# Clone the repository
git clone https://github.com/ZaguanLabs/xai-sdk-go.git
cd xai-sdk-go
# Build
go build ./...
# Run tests
go test ./...# Set your API key
export XAI_API_KEY=your-api-key-here
# Run basic example
go run examples/basic/main.go
# Run streaming example
go run examples/streaming/main.goContributions are welcome! Whether it's bug reports, feature requests, or pull requests, we appreciate all forms of contribution.
Apache 2.0 Licensed - The xAI SDK for Go is free and open source software. You're welcome to use, modify, and distribute it for any purpose.