# CLI & API — OpenGen Setup

> Use the portable CLI with browser login, or create one scoped workspace API key.

- **Client kind:** CLI / API
- **Integration approach:** OpenGen browser PKCE login / scoped API key
- **Verification status:** LIVE GENERATION VERIFIED
- **Reviewed:** 2026-11-30
- **Canonical HTML:** https://opengen.dev/docs/clients/cli
- **Source reference:** [OpenGen MCP & CLI guide](https://opengen.dev/docs/mcp)

## Agent Prompt

```text
Walk the user through connecting CLI & API to OpenGen (https://opengen.dev/mcp for authenticated read+generate OAuth, https://opengen.dev/mcp/readonly for zero-auth catalog discovery, or @opengen-dev/cli for shell workflows) following https://opengen.dev/docs/clients/cli.md. Review configuration commands before running them, keep credentials out of prompts and version control, and verify read-only access (balance / find_models / opengen doctor) before proposing any paid generation.
```

## Setup Steps

1. Install @opengen-dev/cli into an isolated prefix on Node 22.12 or later (npm install --prefix "$HOME/.opengen/cli" @opengen-dev/cli --ignore-scripts --no-audit --no-fund), or install from the checksum-verified offline release bundle, then add "$HOME/.opengen/cli/node_modules/.bin" to your PATH.
2. Set OPENGEN_API_URL if targeting a self-hosted gateway (defaults to https://opengen.dev; MEDIA_ROUTER_URL remains a supported alias), then run login and doctor below. Review the paying workspace and read/generate permissions in your browser.
3. For a backend API integration, create a separate expiring read/generate key in Workspace → API keys (/app/keys). Save it once as OPENGEN_API_KEY in your secret manager and send it only in the Authorization header.

## Configuration & Commands

### Sign in, diagnose your workspace, and search the catalog

```bash
export OPENGEN_API_URL="https://opengen.dev"
opengen login
opengen doctor
opengen catalog search "gpt-image-2"
```

### Submit once with a persisted idempotency key

```bash
cat <<'EOF' > request.json
{
  "modelId": "openai/gpt-image-2",
  "operation": "text-to-image",
  "input": {
    "prompt": "A matte ceramic teapot on dark slate, studio lighting",
    "resolution": "1K",
    "quality": "low"
  },
  "billing": "managed",
  "maxCostMicros": 50000
}
EOF

# After confirming route, published rate and balance (save OPENGEN_IDEMPOTENCY_KEY once and reuse it on retry):
opengen generate --input request.json --idempotency-key "${OPENGEN_IDEMPOTENCY_KEY:?Set and save a unique ID before submitting}"
```

## Verification Evidence & Cautions

**Recorded evidence (2026-11-30):** Source apps/cli 0.1.2 at 484d99f (2026-11-30): verified unauthenticated pricing (178 routes)/catalog/deployments/operations, live browser PKCE login, authenticated status/balance/models.schema, bounded live openai/gpt-image-2 text-to-image (1K, low quality; $0.005985 settled) via generate → job wait → job get → job recover --idempotency-key, and first-party /media PNG delivery (with ?utm_source=test) against https://opengen.dev; published @opengen-dev/cli 0.1.2 (2026-11-28) verified clean npm install, login, read-only doctor/balance/catalog search, and logout revocation. Long-lived CLI token refresh, upload/edit flows, other models/operations, and post-publish npm install of @opengen-dev/cli 0.1.3 remain separately unverified.

**Important boundaries:** Published @opengen-dev/cli 0.1.2 (2026-11-28) completed clean isolated npm install, live browser opengen login (read/generate), authenticated doctor/balance/catalog search, and opengen logout with server revocation against https://opengen.dev. Source apps/cli 0.1.2 at 484d99f (2026-11-30) verified unauthenticated pricing (178 routes)/catalog/deployments/operations, live browser PKCE login, authenticated status/balance/models.schema, a bounded live openai/gpt-image-2 text-to-image generation (1K, low quality; $0.005985 settled) via generate → job wait → job get → job recover --idempotency-key, and first-party /media PNG delivery (?utm_source=test resilient). Long-lived CLI token refresh, upload/edit flows, other models/operations, and post-publish npm install of @opengen-dev/cli 0.1.3 remain separately unverified. New personal workspaces start with $0 managed credit. Do not pass tokens in command arguments, URLs or checked-in files.

## Related Links

- [All Agent Clients & Support Matrix](https://opengen.dev/docs/clients.md)
- [MCP Integration Guide](https://opengen.dev/docs/mcp.md)
- [Quickstart Guide](https://opengen.dev/docs/quickstart.md)
- [Installable Skill (skill.md)](https://opengen.dev/skill.md)
- [AI Index (llms.txt)](https://opengen.dev/llms.txt)
