Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:2aronS Duel Agents Key Validation and Resolution

From Leeroopedia
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 regex
  • requireApiKey — a fail-fast wrapper that throws with an actionable message if the key is invalid
  • resolveApiKeyFromEnv — reads the key from `DUEL_API_KEY` or `DUEL_AGENTS_API_KEY` env vars
  • promptForKey — 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();

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment