Skip to content

Instantly share code, notes, and snippets.

@musistudio
Last active October 23, 2025 10:10
Show Gist options
  • Save musistudio/f5a67841ced39912fd99e42200d5ca8b to your computer and use it in GitHub Desktop.
Save musistudio/f5a67841ced39912fd99e42200d5ca8b to your computer and use it in GitHub Desktop.
qwen-cli.js
const os = require("os");
const path = require("path");
const fs = require("fs/promises");
const OAUTH_FILE = path.join(os.homedir(), ".qwen", "oauth_creds.json");
class QwenCLITransformer {
name = "qwen-cli";
async transformRequestIn(request, provider) {
if (!this.oauth_creds) {
await this.getOauthCreds();
}
if (this.oauth_creds && this.oauth_creds.expiry_date < +new Date()) {
await this.refreshToken(this.oauth_creds.refresh_token);
}
if (request.stream) {
request.stream_options = {
include_usage: true,
};
}
return {
body: request,
config: {
headers: {
Authorization: `Bearer ${this.oauth_creds.access_token}`,
"User-Agent": "QwenCode/v22.12.0 (darwin; arm64)",
},
},
};
}
refreshToken(refresh_token) {
const urlencoded = new URLSearchParams();
urlencoded.append("client_id", "f0304373b74a44d2b584a3fb70ca9e56");
urlencoded.append("refresh_token", refresh_token);
urlencoded.append("grant_type", "refresh_token");
return fetch("https://chat.qwen.ai/api/v1/oauth2/token", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: urlencoded,
})
.then((response) => response.json())
.then(async (data) => {
data.expiry_date =
new Date().getTime() + data.expires_in * 1000 - 1000 * 60;
data.refresh_token = refresh_token;
delete data.expires_in;
this.oauth_creds = data;
await fs.writeFile(OAUTH_FILE, JSON.stringify(data, null, 2));
});
}
async getOauthCreds() {
try {
const data = await fs.readFile(OAUTH_FILE);
this.oauth_creds = JSON.parse(data);
} catch (e) {}
}
}
module.exports = QwenCLITransformer;
@admk
Copy link

admk commented Aug 28, 2025

refreshToken有点问题,无法refresh,这个改动似乎可以让它正确工作,不用总是隔一段时间启动qwen了:https://gist.github.com/admk/b927e85bc897cc64d8e27cbad40dac55/revisions#diff-845b885d933e5d2b9834e9a44d40ddb6d30ddbdf30bc4c8e195694a704d3a2c6

Never mind, still not working.

@oniharnantyo
Copy link

https://portal.qwen.ai/v1/chat/completions

{
  "name": "qwen-cli",
  "api_base_url": "https://portal.qwen.ai/v1/chat/completions",
  "api_key": "any-string-is-ok",
  "models": [
    "qwen3-coder-plus"
  ],
  "transformer": {
    "use": ["qwen-cli"]
  }
},

我的config.json中的写法,之前我没写出正确的api_base_url导致一直出错

this was the configuration I was looking for. with api_base_url "https://portal.qwen.ai/v1" was not working. on roo code was working. why this api_base_url works for claude code with claude code router?

it works after i update and reauthenticate through qwen-code

@mir-xiong
Copy link

要使用qwen.js 是不是要先安装qwen-code呀?

@tilak28
Copy link

tilak28 commented Oct 23, 2025

are their any working configs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment