API keys and setup for five popular coding agents

  • API keys
  • Codex
  • Claude Code
  • DeepSeek Harness
  • OpenCode
  • Pi

Once you claim an API credit from the free AI offers directory, most providers give you two things: an API key and a base URL. Whether you use a chat client or a terminal coding agent, the job is the same—send requests with that key to that base URL, and pass an exact model ID from the provider’s list (the same model often has slightly different IDs across providers).

Sanity-check the API with curl first

Every request needs three pieces:

  • API key — authenticates your request and bills usage to your account.
  • Base URL — where requests go, often ending in /v1 depending on the protocol.
  • Model ID — the exact model slug accepted by that endpoint.

Most providers speak one of three protocols, and getting the /v1 suffix wrong is the most common reason a fresh setup fails:

  • OpenAI Chat Completions (/v1/chat/completions): the default for most gateways and chat apps. Clients usually expect /v1 at the end of the base URL (https://api.example.com/v1).
  • OpenAI Responses (/v1/responses): used by Codex CLI by default. A provider that only proxies Chat Completions won’t handle Responses unless it explicitly supports it.
  • Anthropic Messages (/v1/messages): required by Claude Code. The Anthropic SDK appends /v1/messages itself, so omit /v1 from your base URL (use https://api.example.com/api).

Before editing client configs, send a one-line curl request to confirm the key and URL actually work. For Chat Completions:

curl -i https://api.example.com/v1/chat/completions \
  -H "Authorization: Bearer $MY_PROVIDER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"your-model-id","messages":[{"role":"user","content":"hi"}]}'

For an Anthropic Messages provider using x-api-key:

curl -i https://api.example.com/api/v1/messages \
  -H "x-api-key: $MY_PROVIDER_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{"model":"your-model-id","max_tokens":64,"messages":[{"role":"user","content":"hi"}]}'

If it fails: 401 usually means a wrong/revoked key or an incomplete account step; 404 almost always means a missing or duplicated /v1 (or the wrong protocol); 429 means rate-limited or out of credit; 400 points to a misspelled model ID, unsupported parameter, or context limit. See the API troubleshooting guide for deeper fixes.

Configuration at a glance

Tool Config location Key things to check
Codex CLI ~/.codex/config.toml Responses API support and env_key
Claude Code Shell env or ~/.claude/settings.json Messages endpoint (no trailing /v1) and auth header
DeepSeek Harness Web UI or profile YAML Protocol selection, base URL, and credentials
OpenCode Project or global opencode.json V1 vs. V2 schema, provider package, and key reference
Pi ~/.pi/agent/models.json api type, model fields, and env var key

1. Codex CLI

Add a named provider block in ~/.codex/config.toml (on Windows, under %USERPROFILE%\.codex\config.toml, or override with CODEX_HOME):

model = "your-model-id"
model_provider = "my_provider"

[model_providers.my_provider]
name = "My Custom Provider"
base_url = "https://api.example.com/v1"
wire_api = "responses"
env_key = "MY_PROVIDER_API_KEY"

One easy mistake: env_key takes the name of the environment variable, not the literal sk-... secret. If you paste your key into env_key, Codex looks for an environment variable named sk-... and fails. Export MY_PROVIDER_API_KEY in your shell, then run codex. Also make sure the endpoint actually supports Responses (wire_api = "responses").

2. Claude Code

For an Anthropic-compatible provider that uses bearer tokens, launch Claude Code from a dedicated shell so it doesn’t collide with existing settings:

unset ANTHROPIC_API_KEY
export ANTHROPIC_BASE_URL="https://api.example.com/api"
export ANTHROPIC_AUTH_TOKEN="$MY_PROVIDER_API_KEY"
claude

Watch out for two traps: don’t put /v1 at the end of ANTHROPIC_BASE_URL, and unset ANTHROPIC_API_KEY when switching to ANTHROPIC_AUTH_TOKEN so the two auth variables don’t conflict. If your provider uses native x-api-key auth instead, follow its docs. You can also set ANTHROPIC_DEFAULT_OPUS_MODEL or ANTHROPIC_DEFAULT_SONNET_MODEL to remap model aliases to the provider’s exact model IDs.

For a persistent setup, put non-secret defaults in ~/.claude/settings.json and keep keys in your environment or keychain:

{
  "env": {
    "ANTHROPIC_BASE_URL": "https://api.example.com/api"
  }
}

3. DeepSeek Harness

With a DeepSeek key in DEEPSEEK_API_KEY, run npx -y @deepseek-ai/dsh. DeepSeek’s standard endpoint is https://api.deepseek.com, and its Anthropic-compatible endpoint is https://api.deepseek.com/anthropic.

To wire up a third-party provider, start the Web UI with npx -y @deepseek-ai/dsh web, open Settings → Models → Add model provider, pick the API protocol (openai-completions, openai-responses, or anthropic-messages), and paste your base URL and key. Credentials saved through the UI live in $DSH_HOME/.credentials.yaml. If you prefer editing files directly, you can also configure profiles under $DSH_HOME/profiles/<profile>/cordis.patch.yml.

4. OpenCode

OpenCode accepts a project-level opencode.json or a global config. Here is an OpenAI-compatible setup for OpenCode V2:

{
  "$schema": "https://opencode.ai/config.json",
  "providers": {
    "my-gateway": {
      "package": "aisdk:@ai-sdk/openai-compatible",
      "name": "My Gateway",
      "settings": {
        "baseURL": "https://api.example.com/v1",
        "apiKey": "{env:MY_PROVIDER_API_KEY}"
      },
      "models": {
        "your-model-id": { "name": "My Model" }
      }
    }
  },
  "model": "my-gateway/your-model-id"
}

Check your version with opencode --version. OpenCode V1 uses provider, npm (e.g. @ai-sdk/openai-compatible or @ai-sdk/anthropic), and options instead of providers, package, and settings—see the OpenCode V1 migration guide if you are on an older config. To avoid putting env vars in JSON at all, run opencode auth login (or /connect inside the TUI) to store keys in OpenCode’s local auth store.

5. Pi

Define custom providers in ~/.pi/agent/models.json:

{
  "providers": {
    "my-provider": {
      "baseUrl": "https://api.example.com/v1",
      "api": "openai-completions",
      "apiKey": "$MY_PROVIDER_API_KEY",
      "models": [
        {
          "id": "your-model-id",
          "name": "My Model",
          "contextWindow": 128000,
          "maxTokens": 8192
        }
      ]
    }
  }
}

Set api to openai-completions, openai-responses, or anthropic-messages to match your provider, and keep contextWindow and maxTokens within what the endpoint supports. Launch directly with pi --provider my-provider --model your-model-id, or press Ctrl+L (/model) inside Pi to switch on the fly.

Keep credentials and quotas manageable

Burn expiring trial credits first and save daily-reset tiers for routine tasks. Keep keys in environment variables or a secret store—never commit real keys into opencode.json or shell history—and check provider data policies before sending private code. For multi-provider workflows, see managing API keys and quotas across providers.