{"id":36591557,"url":"https://github.com/coze-dev/coze-go","last_synced_at":"2026-01-14T23:27:19.682Z","repository":{"id":269791598,"uuid":"870021183","full_name":"coze-dev/coze-go","owner":"coze-dev","description":"The Go SDK for the Coze API","archived":false,"fork":false,"pushed_at":"2025-12-17T09:00:46.000Z","size":13321,"stargazers_count":98,"open_issues_count":2,"forks_count":58,"subscribers_count":7,"default_branch":"main","last_synced_at":"2026-01-12T16:28:31.612Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/coze-dev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2024-10-09T09:57:23.000Z","updated_at":"2025-12-27T14:09:01.000Z","dependencies_parsed_at":null,"dependency_job_id":"a5c9b21a-cf20-430a-bab3-d997c287f546","html_url":"https://github.com/coze-dev/coze-go","commit_stats":null,"previous_names":["coze-dev/coze-go"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/coze-dev/coze-go","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coze-dev%2Fcoze-go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coze-dev%2Fcoze-go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coze-dev%2Fcoze-go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coze-dev%2Fcoze-go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/coze-dev","download_url":"https://codeload.github.com/coze-dev/coze-go/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coze-dev%2Fcoze-go/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28438173,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T22:37:52.437Z","status":"ssl_error","status_checked_at":"2026-01-14T22:37:31.496Z","response_time":107,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-01-12T08:16:50.316Z","updated_at":"2026-01-14T23:27:19.674Z","avatar_url":"https://github.com/coze-dev.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Coze Go API SDK\n\n[![codecov](https://codecov.io/github/coze-dev/coze-go/graph/badge.svg?token=UXitaQ0wp7)](https://codecov.io/github/coze-dev/coze-go)\n\n## Introduction\n\nThe Coze API SDK for Go is a powerful tool designed to seamlessly integrate Coze's open APIs into your Go projects.\n\nKey Features:\n- Full support for Coze open APIs and authentication APIs\n- Both synchronous and streaming API calls\n- Optimized streaming APIs with io.Reader interface\n- Optimized list APIs with Iterator interface\n- Simple and idiomatic Go API design\n\n## Installation\n\n```bash\ngo get github.com/coze-dev/coze-go\n```\n\n## Usage\n\n### Examples\n\n| Example                       | File                                                                                    |\n|-------------------------------|-----------------------------------------------------------------------------------------|\n| pat auth                      | [pat_example.go](examples/auth/token/main.go)                                           |\n| oauth by web code             | [web_oauth_example.go](examples/auth/web_oauth/main.go)                                 |\n| oauth by jwt flow             | [jwt_oauth_example.go](examples/auth/jwt_oauth/main.go)                                 |\n| oauth by pkce flow            | [pkce_oauth_example.go](examples/auth/pkce_oauth/main.go)                               |\n| oauth by device flow          | [device_oauth_example.go](examples/auth/device_oauth/main.go)                           |\n| handle auth exception         | [handle_auth_exception_example.go](examples/auth/error/main.go)                         |\n| bot create, publish and chat  | [publish_bot_example.go](examples/bots/publish/main.go)                                 |\n| get bot and bot list          | [retrieve_bot_example.go](examples/bots/retrieve/main.go)                               |\n| non-stream chat               | [non_stream_chat_example.go](examples/chats/chat/main.go)                               |\n| stream chat                   | [stream_chat_example.go](examples/chats/chat_with_image/main.go)                        |\n| chat with local plugin        | [submit_tool_output_example.go](examples/chats/submit_tool_output/main.go)              |\n| chat with image               | [chat_with_image_example.go](examples/chats/chat_with_image/main.go)                    |\n| audio live retrieve           | [audio_live_retrieve_example.go](examples/audio/live/retrieve/main.go)                  |\n| non-stream workflow chat      | [non_stream_workflow_run_example.go](examples/workflows/runs/create/main.go)            |\n| stream workflow chat          | [stream_workflow_run_example.go](examples/workflows/runs/stream/main.go)                |\n| async workflow run            | [async_workflow_run_example.go](examples/workflows/runs/async_run/main.go)              |\n| conversation                  | [conversation_example.go](examples/conversations/crud/main.go)                          |\n| list conversation             | [list_conversation_example.go](examples/conversations/list/main.go)                     |\n| workspace                     | [list_workspace_example.go](examples/workspaces/list/main.go)                           |\n| create update delete message  | [create_update_delete_message_example.go](examples/conversations/messages/crud/main.go) |\n| list message                  | [list_message_example.go](examples/conversations/messages/list/main.go)                 |\n| create update delete document | [create_update_delete_document_example.go](examples/datasets/documents/crud/main.go)    |\n| list documents                | [list_documents_example.go](examples/datasets/documents/list/main.go)                   |\n| initial client                | [init_client_example.go](examples/client/init/main.go)                                  |\n| how to handle error           | [handle_error_example.go](examples/client/error/main.go)                                |\n| get response log id           | [log_example.go](examples/client/log/main.go)                                           |\n\n### Initialize the Coze Client \n\nTo get started, visit https://www.coze.com/open/oauth/pats (or https://www.coze.cn/open/oauth/pats for the CN environment).\n\nCreate a new token by clicking \"Add Token\". Configure the token name, expiration time, and required permissions. Click OK to generate your personal access token.\n\nImportant: Store your personal access token securely to prevent unauthorized access.\n\n```go\nfunc main() {\n    // Get an access_token through personal access token or oauth.\n    token := os.Getenv(\"COZE_API_TOKEN\")\n    authCli := coze.NewTokenAuth(token)\n    \n    /*\n     * The default access is api.coze.com, but if you need to access api.coze.cn\n     * please use baseUrl to configure the API endpoint to access\n     */\n    baseURL := os.Getenv(\"COZE_API_BASE\")\n    cozeCli := coze.NewCozeAPI(authCli, coze.WithBaseURL(baseURL))\n}\n```\n\n### Chat\n\nFirst, create a bot instance in Coze. The bot ID is the last number in the web link URL.\n\n#### Non-Stream Chat\n\nThe SDK provides a convenient wrapper function for non-streaming chat operations. It handles polling and message retrieval automatically:\n\n```go\nfunc main() {\n    token := os.Getenv(\"COZE_API_TOKEN\")\n    botID := os.Getenv(\"PUBLISHED_BOT_ID\")\n    uid := os.Getenv(\"USER_ID\")\n    \n    authCli := coze.NewTokenAuth(token)\n    cozeCli := coze.NewCozeAPI(authCli, coze.WithBaseURL(os.Getenv(\"COZE_API_BASE\")))\n    \n    ctx := context.Background()\n    req := \u0026coze.CreateChatReq{\n        BotID:  botID,\n        UserID: uid,\n        Messages: []coze.Message{\n            coze.BuildUserQuestionText(\"What can you do?\", nil),\n        },\n    }\n    \n    chat, err := cozeCli.Chat.CreateAndPoll(ctx, req, nil)\n    if err != nil {\n        fmt.Println(\"Error:\", err)\n        return\n    }\n    \n    if chat.Status == coze.ChatStatusCompleted {\n        fmt.Printf(\"Token usage: %d\\n\", chat.Usage.TokenCount)\n    }\n}\n```\n\n#### Stream Chat\n\nUse cozeCli.Chat.Stream() to create a streaming chat session:\n\n```go\nfunc main() {\n    // ... initialize client as above ...\n    \n    ctx := context.Background()\n    req := \u0026coze.CreateChatReq{\n        BotID:  botID,\n        UserID: userID,\n        Messages: []coze.Message{\n            coze.BuildUserQuestionObjects([]coze.MessageObjectString{\n                coze.NewTextMessageObject(\"Describe this picture\"),\n                coze.NewImageMessageObjectByID(imageInfo.FileInfo.ID),\n            }, nil),\n        },\n    }\n    \n    resp, err := cozeCli.Chat.Stream(ctx, req)\n    if err != nil {\n        fmt.Println(\"Error starting stream:\", err)\n        return\n    }\n    defer resp.Close()\n    \n    for {\n        event, err := resp.Recv()\n        if errors.Is(err, io.EOF) {\n            fmt.Println(\"Stream finished\")\n            break\n        }\n        if err != nil {\n            fmt.Println(err)\n            break\n        }\n        \n        if event.Event == coze.ChatEventConversationMessageDelta {\n            fmt.Print(event.Message.Content)\n        } else if event.Event == coze.ChatEventConversationChatCompleted {\n            fmt.Printf(\"Token usage:%d\\n\", event.Chat.Usage.TokenCount)\n        }\n    }\n}\n```\n\n### Files\n\n```go\nfunc main() {\n    // ... initialize client as above ...\n    \n    ctx := context.Background()\n    filePath := os.Getenv(\"FILE_PATH\")\n    \n    // Upload file\n    uploadResp, err := cozeCli.Files.Upload(ctx, coze.NewUploadFilesReqWithPath(filePath))\n    if err != nil {\n        fmt.Println(\"Error uploading file:\", err)\n        return\n    }\n    fileInfo := uploadResp.FileInfo\n    \n    // Wait for file processing\n    time.Sleep(time.Second)\n    \n    // Retrieve file\n    retrievedResp, err := cozeCli.Files.Retrieve(ctx, \u0026coze.RetrieveFilesReq{\n        FileID: fileInfo.ID,\n    })\n    if err != nil {\n        fmt.Println(\"Error retrieving file:\", err)\n        return\n    }\n    fmt.Println(retrievedResp.FileInfo)\n}\n```\n\n### Pagination\n\nThe SDK provides an iterator interface for handling paginated results:\n\n```go\nfunc main() {\n    // ... initialize client as above ...\n    \n    ctx := context.Background()\n    datasetID, _ := strconv.ParseInt(os.Getenv(\"DATASET_ID\"), 10, 64)\n    \n    // Use iterator to automatically retrieve next page\n    documents, err := cozeCli.Datasets.Documents.List(ctx, \u0026coze.ListDatasetsDocumentsReq{\n        Size: 1,\n        DatasetID: datasetID,\n    })\n    if err != nil {\n        fmt.Println(\"Error fetching documents:\", err)\n        return\n    }\n    \n    for documents.Next() {\n        fmt.Println(documents.Current())\n    }\n    \n    fmt.Println(\"has_more:\", documents.HasMore())\n}\n```\n\n### Error Handling\n\nThe SDK uses Go's standard error handling patterns. All API calls return an error value that should be checked:\n\n```go\nresp, err := cozeCli.Chat.Create(ctx, req)\nif err != nil {\n    if cozeErr, ok := coze.AsCozeError(err); ok {\n    // Handle Coze API error\n    fmt.Printf(\"Coze API error: %s (code: %s)\\n\", cozeErr.ErrorMessage, cozeErr.ErrorCode)\n    return\n    }\n}\n``` ","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoze-dev%2Fcoze-go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcoze-dev%2Fcoze-go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoze-dev%2Fcoze-go/lists"}