npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@coze/api

v1.0.4

Published

Coze Javascript library

Downloads

261

Readme

Coze API SDK

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 } 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
  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: '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 } from '@coze/api';

async function streamChat() {
  const stream = await client.chat.stream({
    bot_id: 'your_bot_id',
    additional_messages: [{
      role: '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.mjs | | Bot Management | Create and manage bots | bot.mjs | | Knowledge | Document management | knowledge.mjs | | Workflow | Run workflow | workflow.mjs | | Voice | Speech synthesis | voice.mjs |

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
node chat.mjs

Browser

cd examples/coze-js-web
npm run start

Documentation

For detailed API documentation and guides, visit: