Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

dashscope-sdk-official

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dashscope-sdk-official

Official Node.js SDK for Alibaba Cloud Model Studio (DashScope) APIs

latest
Source
npmnpm
Version
1.25.3
Version published
Weekly downloads
91
-1.09%
Maintainers
1
Weekly downloads
 
Created
Source

English · 中文 · Changelog

DashScope Node.js SDK

Official Node.js client for Alibaba Cloud Model Studio (DashScope). Feature set is kept in sync with the Python SDK where possible.

Installation

npm install dashscope-sdk-official

From a clone of this repository:

npm install
npm run build

Tests use nock and do not call the real API (default: npm test).

To run the same specs against the real DashScope HTTP API (creates real tasks/files/jobs and consumes quota), set a key and use the live script:

export DASHSCOPE_API_KEY='your-key'
npm run test:live

Optional: copy .env.example to .env so test:live can pick up DASHSCOPE_API_KEY (loaded in test/nock-hooks.cjs).

The WebSocket TTS case (test/audio/tts.test.ts) is skipped unless you use test:live (real service, billable).

The published package ships CommonJS (lib/index.js) and ESM (lib/index.mjs); package.json exports selects the right entry for require vs import.

Quick start

const { Configuration, DashscopeApi } = require('dashscope-sdk-official');

const configuration = new Configuration({
  apiKey: process.env.DASHSCOPE_API_KEY || 'YOUR-DASHSCOPE-API-KEY',
});

const api = new DashscopeApi(configuration);

const result = await api.createGeneration({
  model: 'qwen-turbo',
  prompt: 'Is the weather good today?',
  stream: true,
});

for await (const chunk of result) {
  console.log(chunk.output);
}

ESM (Node 18+):

import { Configuration, DashscopeApi } from 'dashscope-sdk-official';

const configuration = new Configuration({
  apiKey: process.env.DASHSCOPE_API_KEY || 'YOUR-DASHSCOPE-API-KEY',
});
const api = new DashscopeApi(configuration);

API key

See the Model Studio documentation for how to create an API key.

In code

const { Configuration } = require('dashscope-sdk-official');

const configuration = new Configuration({
  apiKey: 'YOUR-DASHSCOPE-API-KEY',
});

If you omit apiKey, the SDK reads process.env.DASHSCOPE_API_KEY.

Environment variables

export DASHSCOPE_API_KEY='YOUR-DASHSCOPE-API-KEY'

Optional:

  • DASHSCOPE_HTTP_BASE_URL — HTTP API base (default https://dashscope.aliyuncs.com/api/v1)
  • DASHSCOPE_WEBSOCKET_BASE_URL — WebSocket base for streaming audio/TTS

Features (overview)

AreaDescription
GenerationSingle- and multi-turn text; streaming supported
Chat CompletionsOpenAI-compatible /compatible-mode/v1/chat/completions
Code generationTongyi Lingma–style code scenes
Image / video synthesisAsync task pattern with polling
Multimodal chatText + image inputs
EmbeddingsText, batch, and multimodal embeddings
Understanding / rerankNLU and reranking endpoints
Files / models / deployments / fine-tunesResource and lifecycle APIs
SpeechTranscription (async), streaming ASR, translation, TTS (WebSocket + Qwen HTTP)
Assistants & threadsAgent-style threads, messages, runs

Generation (sync and stream)

// Single turn
const r = await api.createGeneration({ model: 'qwen-turbo', prompt: 'Hello!' });

// Multi-turn
const r2 = await api.createGeneration({
  model: 'qwen-turbo',
  messages: [
    { role: 'system', content: 'You are a helpful assistant.' },
    { role: 'user', content: 'How do I braise beef with tomatoes?' },
  ],
});

// Stream
for await (const chunk of api.createGeneration({
  model: 'qwen-turbo',
  prompt: 'Outline a short essay on ocean plastic pollution.',
  stream: true,
})) {
  console.log(chunk.output);
}

Chat completions

const r = await api.createChatCompletion({
  model: 'qwen-turbo',
  messages: [{ role: 'user', content: 'Reply with one word.' }],
});
// Pass stream: true and use for await for streaming mode

Image synthesis (async)

const r = await api.createImageSynthesis({
  model: 'wanx-v1',
  prompt: 'A cute cat sitting on a windowsill',
});

Text embedding

const r = await api.createEmbedding({
  model: 'text-embedding-v1',
  input: 'hello world',
  text_type: 'query',
});

TTS (WebSocket)

const { SpeechSynthesizer, Configuration } = require('dashscope-sdk-official');
const synth = new SpeechSynthesizer(new Configuration({ apiKey: process.env.DASHSCOPE_API_KEY }));
const result = await synth.call({ model: 'cosyvoice-v1', text: 'Hello from DashScope', format: 'wav' });
const audio = result.getAudioData();

Configuration object

const config = new Configuration({
  apiKey: 'YOUR-DASHSCOPE-API-KEY',
  basePath: 'https://dashscope.aliyuncs.com/api/v1',
  workspace: 'optional-workspace-id',
});

Output shape

Typical fields on result objects mirror the HTTP envelope:

  • request_id — tracing id
  • status_code200 means success in the SDK’s normalized sense
  • code / message — populated on errors
  • output — model payload
  • usage — token usage when returned by the API

Contributing

Issues and pull requests are welcome. Please run npm run lint, npm run typecheck, npm run build, and npm test locally when you change behavior. When adding or adjusting HTTP behavior, extend the corresponding nock expectations in test/ (mock origin and helpers live under test/helpers/mockConfig.ts).

Changelog

See CHANGELOG.md. Maintainer release steps (npm + Git tag + GitHub Release): docs/RELEASING.md.

License

This project is licensed under the Apache License, Version 2.0. See LICENSE.

Keywords

dashscope-sdk-official

FAQs

Package last updated on 24 Apr 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts