jackwener/OpenCLI · critical · AuthRequiredError
WEREAD_API_KEY is not set. Export it with `export WEREAD_API
Error message
WEREAD_API_KEY is not set. Export it with `export WEREAD_API_KEY=<wrk-...>`.
What it means
getApiKey() reads WEREAD_API_KEY from the environment and throws AuthRequiredError when it is missing or blank after trimming. The WeRead official API requires a wrk- prefixed key on every request, so the client fails fast before making any network call. The message shows the exact export command to fix it.
Source
Thrown at clis/weread-official/utils.js:39
export const WEREAD_GATEWAY_URL = 'https://i.weread.qq.com/api/agent/gateway';
export const WEREAD_DOMAIN = 'weread.qq.com';
/**
* Skill version reported with every gateway request. Bump when official
* `weread-skills.zip` ships a new SKILL.md `version:` line.
*/
export const SKILL_VERSION = '1.0.4';
const DEFAULT_TIMEOUT_MS = 30_000;
/** errcodes that mean "Bearer key invalid / token expired" — map to AuthRequiredError. */
const AUTH_ERRCODES = new Set([-2010, -2012]);
/** Resolve API key from env. Throws AuthRequiredError on missing / blank value. */
export function getApiKey() {
const key = String(process.env.WEREAD_API_KEY ?? '').trim();
if (!key) {
throw new AuthRequiredError(
WEREAD_DOMAIN,
'WEREAD_API_KEY is not set. Export it with `export WEREAD_API_KEY=<wrk-...>`.',
);
}
return key;
}
/**
* Build the gateway request body. Business params are flattened next to
* `api_name` and `skill_version` — never wrapped in a `params` / `data` /
* `body` object (the gateway silently drops them and returns page 1).
*/
export function buildGatewayBody(apiName, params = {}) {
if (!apiName || typeof apiName !== 'string') {
throw new ArgumentError('weread-official: api_name is required');
}
const body = { api_name: apiName, skill_version: SKILL_VERSION };
for (const [key, value] of Object.entries(params ?? {})) {View on GitHub (pinned to 49907e53dc)
Solutions
- Run `export WEREAD_API_KEY=<wrk-...>` in the current shell before invoking the command
- Add the export to ~/.bashrc / ~/.zshrc so it persists across sessions
- In CI, add WEREAD_API_KEY to the pipeline/secret manager environment settings
- If using a .env file, ensure it's loaded into the environment before the CLI runs
- Verify with `echo ${WEREAD_API_KEY:+set}` that the variable is non-empty
Example fix
// before (fails)
await wereadSearch({ keyword: '三体' });
// after
if (!process.env.WEREAD_API_KEY) {
throw new Error('WEREAD_API_KEY is not set. Export it with `export WEREAD_API_KEY=<wrk-...>`.');
}
await wereadSearch({ keyword: '三体' }); Defensive patterns
Strategy: validation
Validate before calling
if (!String(process.env.WEREAD_API_KEY ?? '').trim()) {
throw new Error('WEREAD_API_KEY is not set. Export it with `export WEREAD_API_KEY=<wrk-...>`.');
} Type guard
function hasApiKey() { return String(process.env.WEREAD_API_KEY ?? '').trim().length > 0; } Try / catch
try { await wereadCall(); }
catch (e) { if (e.name === 'AuthRequiredError') { console.error('Set WEREAD_API_KEY first: export WEREAD_API_KEY=<wrk-...>'); process.exit(1); } throw e; } Prevention
- Export WEREAD_API_KEY in your shell profile
- Add the secret to CI environment settings
- Fail fast with a startup check before running commands
- Verify non-empty after switching shells, sudo, or containers
When it happens
Trigger: Running the CLI in a shell where WEREAD_API_KEY was never exported; exporting it only in another shell/session/CI job; key set as empty string or whitespace; dotenv file not loaded; running under systemd/cron/Docker where the env var wasn't propagated.
Common situations: New machine setup where the key was never configured; CI pipelines missing the secret in the environment settings; switching from a .env-based tool to this CLI that reads process.env directly; sudo or subshell dropping the variable.
Understand the failure class
Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.
Related errors
- Missing ${label}
- 12306 tk auth cookie missing
- Invalid ${label}: ${raw}
- ${label} returned HTTP ${resp.status}: ${summarizeApiError(p
- ${config.site} login
AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29).
Data as JSON: /api/errors/47a53c32df4b14a2.
Report an issue: GitHub.