Go SDK

View as Markdown

Go SDK

A fast, zero-dependency Go client for the MeshAPI AI model gateway.

Requirements

  • Go ≥ 1.21

Installation

$go get github.com/yourorg/meshapi-go-sdk@v0.1.0

Quick Start

Basic Chat Completion

1import "meshapi-go-sdk"
2
3client := meshapi.New(meshapi.Config{
4 BaseURL: "https://api.meshapi.ai",
5 Token: "rsk_...",
6})
7
8model := "openai/gpt-4o-mini"
9resp, err := client.Chat.Completions.Create(ctx, meshapi.ChatCompletionParams{
10 Model: &model,
11 Messages: []meshapi.ChatMessage{{Role: "user", Content: "What is 2+2?"}},
12})

Streaming Chat Completion

Streaming is handled elegantly using Go channels. The client returns both a data channel and an error channel to process chunks asynchronously.

1chunkCh, errCh := client.Chat.Completions.Stream(ctx, params)
2
3for chunk := range chunkCh {
4 if len(chunk.Choices) > 0 && chunk.Choices[0].Delta != nil {
5 fmt.Print(*chunk.Choices[0].Delta.Content)
6 }
7}
8
9// Check for errors once the stream closes
10if err := <-errCh; err != nil {
11 log.Fatal(err)
12}

Error Handling & Resiliency

Retries occur automatically on 429, 502, 503, and 504 errors using exponential backoff (defaulting to 3 retries, maximum 30 seconds wait, adhering to the Retry-After header).

You can catch structured MeshAPIError wrappers:

1resp, err := client.Chat.Completions.Create(ctx, params)
2if err != nil {
3 var svcErr *meshapi.MeshAPIError
4 if errors.As(err, &svcErr) {
5 fmt.Println(svcErr.Status) // HTTP status
6 fmt.Println(svcErr.Code) // "rate_limit_exceeded", "spend_limit_exceeded", etc.
7 fmt.Println(svcErr.RequestID) // Trackable request ID
8 }
9}

[!WARNING] Streams do not automatically retry! On a network connection failure mid-stream, the error channel receives a MeshAPIError with Code="stream_interrupted". Restarting the Stream call is required to resume.