Implementation:2aronS Duel Agents Key Validation and Resolution
| Knowledge Sources | |
|---|---|
| Domains | Security, Configuration |
| Last Updated | 2026-10-27 14:00 GMT |
Overview
Concrete tool for validating, requiring, and resolving Duel API keys provided by the `@duel-agents/core` and `@duel-agents/install` packages.
Description
The Key Validation and Resolution group of functions enforces the `duel_<prefix>_<secret>` credential format across the CLI and SDK. It comprises four cooperating functions spread across two packages:
validateApiKey— a pure boolean predicate that tests a string against the key regexrequireApiKey— a fail-fast wrapper that throws with an actionable message if the key is invalidresolveApiKeyFromEnv— reads the key from `DUEL_API_KEY` or `DUEL_AGENTS_API_KEY` env varspromptForKey— interactive readline fallback used by the CLI when no valid env key is present
These functions are the single source of truth for key validation; every other module (`getEnvForTarget`, `DuelClient`, `checkConnectivity`, `patchOpenClawConfig`) calls `requireApiKey` or `validateApiKey` rather than re-implementing the check.
Usage
Import these functions when you need to:
- Validate a user-supplied API key before writing it to a config file
- Require a valid key at the entry point of a CLI command or SDK constructor
- Resolve a key from environment variables with a fallback to interactive prompt
- Distinguish "key missing/invalid" (local error) from "key rejected" (remote error)
Code Reference
Source Location
- Repository: Duel-Agents
- Files:
packages/core/src/config.ts—validateApiKey,requireApiKey(lines 23-39)packages/cli/src/install.ts—resolveApiKeyFromEnv(lines 59-64)packages/cli/src/cli.ts—promptForKey(lines 33-50)
Signature
// packages/core/src/config.ts
const API_KEY_PATTERN = /^duel_[0-9a-zA-Z]{8}_[0-9a-zA-Z]{32}$/;
/** Returns true only for a well formed `duel_<prefix>_<secret>` key. */
export function validateApiKey(plaintext: string | undefined | null): boolean;
/** Throws if invalid; returns trimmed key if valid. */
export function requireApiKey(
input: string | undefined | null,
label?: string, // default "DUEL_API_KEY"
): string;
// packages/cli/src/install.ts
/** Reads key from DUEL_API_KEY or DUEL_AGENTS_API_KEY env vars. */
export function resolveApiKeyFromEnv(): string | undefined;
// packages/cli/src/cli.ts
/** Interactive readline prompt; validates env key first, prompts if missing/invalid. */
async function promptForKey(): Promise<string>;
Import
import { validateApiKey, requireApiKey } from "@duel-agents/core";
import { resolveApiKeyFromEnv } from "@duel-agents/install"; // re-exported from cli/install.ts
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| plaintext | undefined \| null | Yes | Raw key to validate |
| input | undefined \| null | Yes | Raw key to require |
| label | string |
No | Env var name shown in error (default DUEL_API_KEY)
|
| (env) | process.env.DUEL_API_KEY |
No | Preferred env var source |
| (env) | process.env.DUEL_AGENTS_API_KEY |
No | Legacy env var source |
Outputs
| Name | Type | Description |
|---|---|---|
validateApiKey returns |
boolean |
true iff key matches duel_ + 8 chars + _ + 32 chars
|
requireApiKey returns |
string |
Trimmed valid key |
requireApiKey throws |
Error |
Message with dashboard URL and format hint |
resolveApiKeyFromEnv returns |
undefined | First non-empty env var value, or undefined
|
promptForKey returns |
Promise<string> |
Validated key from env or interactive prompt |
Usage Examples
Validate and Require in SDK Constructor
import { validateApiKey, requireApiKey } from "@duel-agents/core";
// Pure check — no throw
const isKey = validateApiKey(process.env.DUEL_API_KEY);
if (!isKey) console.warn("DUEL_API_KEY is missing or malformed.");
// Fail-fast — throws if invalid
const key = requireApiKey(process.env.DUEL_API_KEY);
console.log(`Using key duel_${key.slice(5, 13)}_…`);
Resolve from Env in CLI
import { resolveApiKeyFromEnv } from "@duel-agents/install";
import { requireApiKey } from "@duel-agents/core";
const raw = resolveApiKeyFromEnv(); // tries DUEL_API_KEY, then DUEL_AGENTS_API_KEY
const key = requireApiKey(raw); // throws if neither is set or invalid
Interactive Prompt Fallback
// promptForKey is called by the CLI main() when env resolution fails.
// It tries the env first, warns if malformed, then opens a readline prompt.
import * as readline from "node:readline/promises";
import { stdin as input, stdout as output } from "node:process";
const rl = readline.createInterface({ input, output });
const key = requireApiKey((await rl.question("Paste your Duel API key: ")).trim());
rl.close();