usingCoze;usingMicrosoft.Extensions.AI;varbotId=Environment.GetEnvironmentVariable("COZE_BOT_ID")!;IChatClientchatClient=newCozeClient(apiKey).WithBotId(botId).WithUserId("demo-user");varresponse=awaitchatClient.GetResponseAsync([new ChatMessage(ChatRole.User, "Reply with only the word ok.")]);Console.WriteLine(response.Text);
Coze chat is bot-centric. You must provide a bot_id via WithBotId(...), ChatOptions.ModelId, or ChatOptions.AdditionalProperties["bot_id"].
Generate
Basic example showing how to create a client and make a request.
1
usingvarclient=newCozeClient(apiKey);
Bots
Examples of listing published bots in a workspace.
1 2 3 4 5 6 7 8 910111213141516171819202122232425
// To list bots, you need a workspace (space) ID. You can find your workspace ID// in the Coze dashboard URL when viewing your workspace.varworkspaceId=Environment.GetEnvironmentVariable("COZE_WORKSPACE_ID")is{Length:>0}value?value:thrownewAssertInconclusiveException("COZE_WORKSPACE_ID environment variable is not found.");usingvarclient=newCozeClient(apiKey);// List published bots in the workspace using `GetSpacePublishedBotsListAsync`.// This returns bots that have been published and are available for use.varresponse=awaitclient.GetSpacePublishedBotsListAsync(spaceId:workspaceId,pageSize:10);// The response contains a `Code` field (0 means success) and a `Data` field// with the list of bots and total count.// Each bot in the list has a `BotId`, `BotName`, and optional `Description`.if(response.Data.SpaceBotsis{Count:>0}bots){varfirstBot=bots[0];Console.WriteLine($"First bot: {firstBot.BotName} (ID: {firstBot.BotId})");}
// To list workflows, you need a workspace ID. You can find your workspace ID// in the Coze dashboard URL when viewing your workspace.varworkspaceId=Environment.GetEnvironmentVariable("COZE_WORKSPACE_ID")is{Length:>0}value?value:thrownewAssertInconclusiveException("COZE_WORKSPACE_ID environment variable is not found.");usingvarclient=newCozeClient(apiKey);// List workflows in the workspace using `OpenAPIGetWorkflowListAsync`.// The `pageNum` parameter starts at 1 (one-based pagination).varresponse=awaitclient.OpenAPIGetWorkflowListAsync(workspaceId:workspaceId,pageNum:1,pageSize:10);// The response contains a `Code` field (0 means success) and a `Data` field// with the workflow items and pagination info.// Each workflow has a `WorkflowId`, `WorkflowName`, and optional `Description`.foreach(varworkflowinresponse.Data.Items){Console.WriteLine($"Workflow: {workflow.WorkflowName} (ID: {workflow.WorkflowId})");}Console.WriteLine($"Has more: {response.Data.HasMore}");