The Coze API SDK for Go is a powerful tool designed to seamlessly integrate Coze's open APIs into your Go projects.
Key Features:
- Full support for Coze open APIs and authentication APIs
- Both synchronous and streaming API calls
- Optimized streaming APIs with io.Reader interface
- Optimized list APIs with Iterator interface
- Simple and idiomatic Go API design
go get github.com/coze-dev/coze-go
To get started, visit https://www.coze.com/open/oauth/pats (or https://www.coze.cn/open/oauth/pats for the CN environment).
Create a new token by clicking "Add Token". Configure the token name, expiration time, and required permissions. Click OK to generate your personal access token.
Important: Store your personal access token securely to prevent unauthorized access.
func main() {
// Get an access_token through personal access token or oauth.
token := os.Getenv("COZE_API_TOKEN")
authCli := coze.NewTokenAuth(token)
/*
* The default access is api.coze.com, but if you need to access api.coze.cn
* please use baseUrl to configure the API endpoint to access
*/
baseURL := os.Getenv("COZE_API_BASE")
cozeCli := coze.NewCozeAPI(authCli, coze.WithBaseURL(baseURL))
}
First, create a bot instance in Coze. The bot ID is the last number in the web link URL.
The SDK provides a convenient wrapper function for non-streaming chat operations. It handles polling and message retrieval automatically:
func main() {
token := os.Getenv("COZE_API_TOKEN")
botID := os.Getenv("PUBLISHED_BOT_ID")
uid := os.Getenv("USER_ID")
authCli := coze.NewTokenAuth(token)
cozeCli := coze.NewCozeAPI(authCli, coze.WithBaseURL(os.Getenv("COZE_API_BASE")))
ctx := context.Background()
req := &coze.CreateChatReq{
BotID: botID,
UserID: uid,
Messages: []coze.Message{
coze.BuildUserQuestionText("What can you do?", nil),
},
}
chat, err := cozeCli.Chat.CreateAndPoll(ctx, req, nil)
if err != nil {
fmt.Println("Error:", err)
return
}
if chat.Status == coze.ChatStatusCompleted {
fmt.Printf("Token usage: %d\n", chat.Usage.TokenCount)
}
}
Use cozeCli.Chat.Stream() to create a streaming chat session:
func main() {
// ... initialize client as above ...
ctx := context.Background()
req := &coze.CreateChatReq{
BotID: botID,
UserID: userID,
Messages: []coze.Message{
coze.BuildUserQuestionObjects([]coze.MessageObjectString{
coze.NewTextMessageObject("Describe this picture"),
coze.NewImageMessageObjectByID(imageInfo.FileInfo.ID),
}, nil),
},
}
resp, err := cozeCli.Chat.Stream(ctx, req)
if err != nil {
fmt.Println("Error starting stream:", err)
return
}
defer resp.Close()
for {
event, err := resp.Recv()
if errors.Is(err, io.EOF) {
fmt.Println("Stream finished")
break
}
if err != nil {
fmt.Println(err)
break
}
if event.Event == coze.ChatEventConversationMessageDelta {
fmt.Print(event.Message.Content)
} else if event.Event == coze.ChatEventConversationChatCompleted {
fmt.Printf("Token usage:%d\n", event.Chat.Usage.TokenCount)
}
}
}
func main() {
// ... initialize client as above ...
ctx := context.Background()
filePath := os.Getenv("FILE_PATH")
// Upload file
uploadResp, err := cozeCli.Files.Upload(ctx, coze.NewUploadFilesReqWithPath(filePath))
if err != nil {
fmt.Println("Error uploading file:", err)
return
}
fileInfo := uploadResp.FileInfo
// Wait for file processing
time.Sleep(time.Second)
// Retrieve file
retrievedResp, err := cozeCli.Files.Retrieve(ctx, &coze.RetrieveFilesReq{
FileID: fileInfo.ID,
})
if err != nil {
fmt.Println("Error retrieving file:", err)
return
}
fmt.Println(retrievedResp.FileInfo)
}
The SDK provides an iterator interface for handling paginated results:
func main() {
// ... initialize client as above ...
ctx := context.Background()
datasetID, _ := strconv.ParseInt(os.Getenv("DATASET_ID"), 10, 64)
// Use iterator to automatically retrieve next page
documents, err := cozeCli.Datasets.Documents.List(ctx, &coze.ListDatasetsDocumentsReq{
Size: 1,
DatasetID: datasetID,
})
if err != nil {
fmt.Println("Error fetching documents:", err)
return
}
for documents.Next() {
fmt.Println(documents.Current())
}
fmt.Println("has_more:", documents.HasMore())
}
The SDK uses Go's standard error handling patterns. All API calls return an error value that should be checked:
resp, err := cozeCli.Chat.Create(ctx, req)
if err != nil {
if cozeErr, ok := coze.AsCozeError(err); ok {
// Handle Coze API error
fmt.Printf("Coze API error: %s (code: %s)\n", cozeErr.ErrorMessage, cozeErr.ErrorCode)
return
}
}