Implementation:21st dev Magic mcp resolveApiKey
| Knowledge Sources | |
|---|---|
| Domains | Authentication, Configuration |
| Last Updated | 2026-10-19 16:40 GMT |
Overview
Concrete function for resolving the 21st MCP API key from every legacy argv/env form, provided by the `@21st-dev/magic` compatibility proxy.
Description
The resolveApiKey() function reads the API key from `process.argv` and `process.env`, accepting every key form the old Magic client ever supported. It exists because the legacy install command (`npx -y @21st-dev/magic@latest API_KEY="..."`) is baked into countless agent configs and model memories, each passing the key differently. The function normalizes all of those forms into a single string (or `undefined`) at process startup; the value is never re-read during the process lifetime.
Accepted argv forms (matched by regex, then quote-stripped):
- `API_KEY=value` (positional, the form models memorized)
- `--API_KEY=value`
- `/API_KEY:value` (Windows style)
- `-API_KEY=value`
- `-API_KEY value` (two separate argv entries)
Accepted environment variables (in resolution order):
- `TWENTY_FIRST_API_KEY`
- `API_KEY_21ST`
- `API_KEY`
Resolution precedence: argv `API_KEY` → argv `TWENTY_FIRST_API_KEY` → argv `API_KEY_21ST` → env `TWENTY_FIRST_API_KEY` → env `API_KEY_21ST` → env `API_KEY`. The first non-undefined value wins.
Usage
Call this (implicitly, at startup) when the proxy process launches to obtain the `apiKey` used for all subsequent `x-api-key` authenticated requests to the 21st MCP endpoint. Agents configuring the proxy should pass the key as `API_KEY="..."` on the command line, or set `API_KEY_21ST` / `TWENTY_FIRST_API_KEY` in the server's environment.
Code Reference
Source Location
- Repository: magic-mcp
- File: src/index.ts
- Lines: L28-61 (function body), L63-72 (`stripQuotes` helper), L74 (assignment to `apiKey`)
Signature
/**
* Resolve the 21st MCP API key from argv and env.
* Reads every legacy key form; returns the first match.
* Reads once at startup — the value never changes mid-process.
*/
function resolveApiKey(): string | undefined
// Helper: strip surrounding single/double quotes from a value.
function stripQuotes(v: string): string
// Resolved once at module load:
const apiKey: string | undefined = resolveApiKey();
Import
// Not exported — resolveApiKey is internal to src/index.ts and runs at
// module load. To supply a key, pass it via argv or env when launching the
// proxy, e.g.:
// npx -y @21st-dev/magic@latest API_KEY="your-key"
// or set env API_KEY_21ST / TWENTY_FIRST_API_KEY.
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| process.argv | string[] | Yes (read internally) | CLI arguments after the script path; scanned for key forms |
| process.env | Record<string, string> | No | Environment variables; fallback key sources |
Outputs
| Name | Type | Description |
|---|---|---|
| return value | undefined | The resolved API key, or `undefined` if no form matched |
| stderr log | text | When `undefined`, a warning is printed to stderr (L81-86) pointing to https://21st.dev/mcp |
Usage Examples
Positional key (the memorized form)
# The form baked into old agent configs and model memories:
npx -y @21st-dev/magic@latest API_KEY="21st-xxxxx"
Environment variable (recommended for plugin configs)
{
"mcpServers": {
"21st": {
"command": "npx",
"args": ["-y", "@21st-dev/magic@latest"],
"env": { "API_KEY_21ST": "21st-xxxxx" }
}
}
}
Windows-style flag
# Windows-style flag, also accepted:
npx -y @21st-dev/magic@latest /API_KEY:21st-xxxxx
Related Pages
Implements Principle
Requires Environment
Notes
- Requires a Node.js runtime to execute (the proxy is a zero-dependency Node script).
- Companion to Implementation:21st_dev_Magic_mcp_forward, which consumes the resolved `apiKey` for every proxied request.