Go API Reference

Import

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

NewClient

1client := joytoken.NewClient(
2 joytoken.WithAPIKey(os.Getenv("JOY_TOKEN_API_KEY")),
3 joytoken.WithAPIBaseURL(os.Getenv("JOY_TOKEN_API_BASE_URL")),
4 joytoken.WithOpenAIBaseURL(os.Getenv("JOY_TOKEN_OPENAI_BASE_URL")),
5)
OptionUse
WithAPIKeyRequest auth
WithAPIBaseURLBase URL for JoyToken APIs such as model list
WithOpenAIBaseURLBase URL for OpenAI-compatible APIs
WithHTTPClientCustom HTTP client
WithHeaderAdd a default request header

CreateChatCompletion

1completion, 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 and returns a non-streaming Chat Completions response.

StreamChatCompletion

1stream, err := client.StreamChatCompletion(ctx, request)
2defer stream.Close()
3
4chunk, err := stream.Recv()

Reads SSE chunks. io.EOF means the stream is complete.

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}
FieldUse
StatusCodeHTTP status code
RequestIDRequest ID
BodyError response body