Go API Reference

Import

1import joytoken "github.com/joytoken/client-sdk-golang"

NewClient

1client := joytoken.NewClient(opts ...joytoken.Option)
OptionDescription
WithAPIKey(apiKey string)Sets the JoyToken API key
WithAPIBaseURL(apiBaseURL string)Sets the base URL for non-OpenAI endpoints
WithOpenAIBaseURL(openAIBaseURL string)Sets the Chat Completions base URL
WithHTTPClient(httpClient HTTPClient)Uses a custom HTTP client
WithHeader(key, value string)Adds a default header to every request

Environment defaults:

VariableUsed for
JOY_TOKEN_API_KEYAPI key
JOY_TOKEN_API_BASE_URLAPI base URL
JOY_TOKEN_OPENAI_BASE_URLChat Completions base URL

CreateChatCompletion

1response, err := client.CreateChatCompletion(ctx, joytoken.ChatCompletionRequest{
2 Model: "auto",
3 Messages: []joytoken.ChatMessage{
4 {Role: "user", Content: "Hello"},
5 },
6})

Calls POST /openai/v1/chat/completions with stream: false.

StreamChatCompletion

1stream, err := client.StreamChatCompletion(ctx, request)
2defer stream.Close()
3
4for {
5 chunk, err := stream.Recv()
6 if errors.Is(err, io.EOF) {
7 break
8 }
9}

Calls POST /openai/v1/chat/completions with stream: true and returns a ChatCompletionStream.

ListModels

1models, err := client.ListModels(ctx)

Calls GET /api/v1/models.

APIError

1var apiErr *joytoken.APIError
2if errors.As(err, &apiErr) {
3 fmt.Println(apiErr.StatusCode, apiErr.RequestID, apiErr.Body)
4}
FieldType
StatusCodeint
RequestIDstring
Bodystring

Core types

1type ChatCompletionRequest struct {
2 Model string
3 Messages []ChatMessage
4 Stream bool
5 Temperature *float64
6 MaxTokens *int
7 TopP *float64
8 Stop any
9 Tools []ChatTool
10 ToolChoice any
11 Tier string
12 Metadata map[string]any
13}
1type ChatMessage struct {
2 Role string
3 Content any
4 Name string
5 ToolCallID string
6 ToolCalls []ToolCall
7}
1type ModelListResponse struct {
2 Object string
3 Data []ModelInfo
4}