@coze/api
TypeScript icon, indicating that this package has built-in type declarations

1.0.15 • Public • Published

Coze API SDK

npm version npm downloads bundle size License: MIT

Official Node.js and Browser SDK for Coze(or 扣子) API platform.

Quick Start

1. Installation

npm install @coze/api
# or
pnpm install @coze/api

2. Basic Usage

import { CozeAPI, COZE_COM_BASE_URL, ChatStatus, RoleType } from '@coze/api';

// Initialize client with your Personal Access Token
const client = new CozeAPI({
  token: 'your_pat_token', // Get your PAT from https://www.coze.com/open/oauth/pats
  // or
  // token: async () => {
  //   // refresh token if expired
  //   return 'your_oauth_token';
  // },
  baseURL: COZE_COM_BASE_URL,
});

// Simple chat example
async function quickChat() {
  const v = await client.chat.createAndPoll({
    bot_id: 'your_bot_id',
    additional_messages: [{
      role: RoleType.User,
      content: 'Hello!',
      content_type: 'text',
    }],
  });

    if (v.chat.status === ChatStatus.COMPLETED) {
    for (const item of v.messages) {
      console.log('[%s]:[%s]:%s', item.role, item.type, item.content);
    }
    console.log('usage', v.chat.usage);
  }
}

Key Features

  • 🌐 Full API Support: Covers all Coze Open Platform APIs
  • 🔐 Multiple Auth Methods: PAT, OAuth, JWT, OAuth PKCE
  • 🔄 Streaming Support: Real-time responses for chat and workflow
  • 🌍 Cross-Platform: Works in Node.js (≥14) and modern browsers
  • ⚙️ Configurable: Timeout, headers, signal, debug options

Authentication Options

  1. Personal Access Token (Simplest)
const client = new CozeAPI({
  token: 'your_pat_token',
  baseURL: COZE_COM_BASE_URL, // Use COZE_CN_BASE_URL for China region
});
  1. Other Auth Methods
  • OAuth Web Application
  • OAuth PKCE
  • JWT
  • Device Code Flow

View authentication examples →

Advanced Usage

Streaming Chat

import { CozeAPI, ChatEventType, RoleType } from '@coze/api';

async function streamChat() {
  const stream = await client.chat.stream({
    bot_id: 'your_bot_id',
    additional_messages: [{
      role: RoleType.User,
      content: 'Hello!',
      content_type: 'text',
    }],
  });

  for await (const part of stream) {
    if (part.event === ChatEventType.CONVERSATION_MESSAGE_DELTA) {
      process.stdout.write(part.data.content); // Real-time response
    }
  }
}

More Examples

Feature Description Example
Chat Text conversations chat.ts
Bot Management Create and manage bots bot.ts
Datasets Document management datasets.ts
Workflow Run workflow workflow.ts
Voice Speech synthesis voice.ts

View all examples →

Development

# Install dependencies
rush update  # If `rush` command is not installed, see ../../README.md

# Run tests
npm run test

Try Examples

Node.js

cd examples/coze-js-node
cp config.default.js config.js  # Edit config.js with your credentials
npm start src/chat.ts

Browser

cd examples/coze-js-web
npm run start

Documentation

For detailed API documentation and guides, visit:

Package Sidebar

Install

npm i @coze/api

Weekly Downloads

874

Version

1.0.15

License

MIT

Unpacked Size

351 kB

Total Files

46

Last publish

Collaborators

  • coze_sdk