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
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:Authentication Setup
The SDK requires API credentials to communicate with AI models. You can use either OpenAI-compatible APIs or Qwen’s OAuth service.OpenAI-Compatible API (Recommended)
Set your API key as an environment variable:~/.bashrc, ~/.zshrc, etc.):
Qwen OAuth (Alternative)
If you still want to use Qwen OAuth:TypeScript Configuration
For TypeScript projects, ensure yourtsconfig.json includes:
tsconfig.json
Verify Installation
Create a simple test file to verify everything works:test.ts
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:| Variable | Description | Example |
|---|---|---|
OPENAI_API_KEY | API key for OpenAI-compatible services | sk-... |
OPENAI_MODEL | Default model to use | gpt-4 |
QWEN_MODEL | Qwen-specific model (fallback) | qwen-max |
QWEN_CODE_CLI_PATH | Custom path to CLI executable | /path/to/qwen |
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:- Ensure the CLI is installed globally:
npm install -g qwen-code@^0.4.0 - Check your PATH includes the global npm bin directory
- Or upgrade to SDK version 0.1.1+ to use the bundled CLI
Module Resolution Errors
If you encounter module resolution errors:- Ensure Node.js version is >= 18.0.0
- Check that
"type": "module"is in yourpackage.jsonfor ESM projects - Use
node --loader tsxornpx tsxfor TypeScript files
API Authentication Errors
If queries fail with authentication errors:- Verify
OPENAI_API_KEYis set correctly - Check that the API key has sufficient permissions
- Ensure you’re not hitting rate limits
Next Steps
Quick Start
Run your first query
API Reference
Explore the query() function
