Skip to main content

Install the SDK

Install the SDK using your preferred package manager:

Requirements

Ensure your environment meets the following requirements:
  • Node.js: >= 18.0.0
  • TypeScript: >= 5.0.0 (for TypeScript projects)

Check Your Node.js Version

If you need to upgrade Node.js, visit nodejs.org or use a version manager like nvm.

CLI Bundling

From SDK version 0.1.1 onwards, the Qwen Code CLI is bundled with the SDK package. You don’t need to install the CLI separately.

SDK Version 0.1.0 (Legacy)

If you’re using SDK version 0.1.0, you must install the Qwen Code CLI separately:
The CLI must be accessible in your PATH. Verify the installation:
We recommend upgrading to SDK version 0.1.1 or later to benefit from the bundled CLI and avoid installation complexity.

Authentication Setup

The SDK requires API credentials to communicate with AI models. You can use either OpenAI-compatible APIs or Qwen’s OAuth service. Set your API key as an environment variable:
For persistent configuration, add it to your shell profile (~/.bashrc, ~/.zshrc, etc.):

Qwen OAuth (Alternative)

Using Qwen OAuth in the SDK is not recommended because credentials are stored in ~/.qwen and may need periodic refresh.
If you still want to use Qwen OAuth:
You’ll need to authenticate with the Qwen CLI first:

TypeScript Configuration

For TypeScript projects, ensure your tsconfig.json includes:
tsconfig.json

Verify Installation

Create a simple test file to verify everything works:
test.ts
Run the test:
You should see the AI’s response printed to the console.

Dependencies

The SDK has minimal dependencies:
package.json
  • @modelcontextprotocol/sdk: For MCP server integration
  • zod: For schema validation in custom tools

Environment Variables

The SDK respects the following environment variables:
The model option in QueryOptions takes precedence over environment variables.

Troubleshooting

Command Not Found Error

If you see “qwen: command not found” when using SDK version 0.1.0:
  1. Ensure the CLI is installed globally: npm install -g qwen-code@^0.4.0
  2. Check your PATH includes the global npm bin directory
  3. Or upgrade to SDK version 0.1.1+ to use the bundled CLI

Module Resolution Errors

If you encounter module resolution errors:
  1. Ensure Node.js version is >= 18.0.0
  2. Check that "type": "module" is in your package.json for ESM projects
  3. Use node --loader tsx or npx tsx for TypeScript files

API Authentication Errors

If queries fail with authentication errors:
  1. Verify OPENAI_API_KEY is set correctly
  2. Check that the API key has sufficient permissions
  3. Ensure you’re not hitting rate limits

Next Steps

Quick Start

Run your first query

API Reference

Explore the query() function