Go SDK
Use the Go OpenAI client library with Glitch by configuring the base URL and adding your Glitch API key.
Basic Setup
Section titled “Basic Setup”package main
import ( "context" "fmt" "os" "github.com/sashabaranov/go-openai")
func main() { config := openai.DefaultConfig(os.Getenv("GLITCH_API_KEY")) config.BaseURL = "https://api.golabrat.ai/v1"
client := openai.NewClientWithConfig(config)
resp, err := client.CreateChatCompletion( context.Background(), openai.ChatCompletionRequest{ Model: openai.GPT4, Messages: []openai.ChatCompletionMessage{ {Role: "user", Content: "Hello!"}, }, }, )
if err != nil { panic(err) }
fmt.Println(resp.Choices[0].Message.Content)}Error Handling
Section titled “Error Handling”Handle Glitch security blocks gracefully:
import ( "context" "errors" "net/http" "os" "github.com/sashabaranov/go-openai")
func chat(client *openai.Client, userInput string) (string, error) { resp, err := client.CreateChatCompletion( context.Background(), openai.ChatCompletionRequest{ Model: openai.GPT4, Messages: []openai.ChatCompletionMessage{ {Role: "user", Content: userInput}, }, }, )
if err != nil { var apiErr *openai.APIError if errors.As(err, &apiErr) && apiErr.HTTPStatusCode == 403 { // Security block - handle gracefully return "I can't process that request.", nil } return "", err }
return resp.Choices[0].Message.Content, nil}Accessing Security Headers
Section titled “Accessing Security Headers”To access security headers, use a custom HTTP client:
import ( "bytes" "encoding/json" "log" "net/http" "os")
func makeRequest() { body, _ := json.Marshal(map[string]interface{}{ "model": "gpt-4", "messages": []map[string]string{ {"role": "user", "content": "Hello"}, }, })
req, _ := http.NewRequest("POST", "https://api.golabrat.ai/v1/chat/completions", bytes.NewBuffer(body)) req.Header.Set("Authorization", "Bearer "+os.Getenv("GLITCH_API_KEY")) req.Header.Set("Content-Type", "application/json")
client := &http.Client{} resp, err := client.Do(req) if err == nil { blocked := resp.Header.Get("X-Risk-Blocked") == "true" categories := resp.Header.Get("X-Risk-Categories") confidence := resp.Header.Get("X-Risk-Confidence")
// Process security metadata if blocked { log.Printf("Blocked: %s (confidence: %s)", categories, confidence) } }}Next Steps
Section titled “Next Steps”- SDKs Overview — Other SDK integrations
- Frameworks — Framework-specific guides
- Quick Start — Get running in 5 minutes