logo

How to Use OpenAI Codex CLI your own AI API Key?

Unlock the potential of Codex CLI with Zeabur AI Hub

Kyle ChungKyle Chung

How to Use OpenAI Codex CLI your own AI API Key?

The Codex CLI is a powerful interface for interacting with AI in your terminal. However, locking yourself into a single provider limits your potential.

By connecting the Codex CLI to Zeabur AI Hub, you can route your queries to any top-tier model (like Claude, Grok, or Gemini) using the exact same tool. This setup works on a pay-as-you-go basis—no need to juggle multiple subscriptions for different AI providers.

Here is how to set it up.

🧱 Prerequisites

  1. Install Codex CLI:

    npm install -g @openai/codex
    
    
  2. Zeabur Credentials:

    • Go to your Zeabur Dashboard to get your AI Hub API Key.
  3. Choose your Endpoint

    Zeabur currently provides two endpoints. Pick the one geographically closest to you:

    • Asia (Tokyo): https://hnd1.aihub.zeabur.ai/
    • US West (San Francisco): https://sfo1.aihub.zeabur.ai/

⚙️ Quick Setup Guide

You need to configure Codex to talk to Zeabur's unified API instead of a specific provider directly.

1. Create the Configuration File

Create or edit the config file at ~/.codex/config.toml. Paste the following configuration to define Zeabur as a provider:

# Default model to use
model = "gemini-3-pro-preview"
model_provider = "zeabur"

[model_providers.zeabur]
name = "Zeabur AI Hub"
# Note: Ensure you append /v1 to the endpoint
base_url = "<https://hnd1.aihub.zeabur.ai/v1>"
env_key = "ZEABUR_API_KEY"
wire_api = "chat"

2. Set your API Key

You need to make your API key available to the tool.

Option A: Just trying it out (Temporary)

For a single session usage:

export ZEABUR_API_KEY="your-zeabur-api-key"
codex "Who are you?"

Option B: Set it and forget it (Permanent)

To avoid setting the key every time, add it to your shell profile.

  1. Open your shell configuration (e.g., ~/.zshrc or ~/.bashrc):

    nano ~/.zshrc
    
    
  2. Paste this line at the bottom:

    export ZEABUR_API_KEY="your-zeabur-api-key"
    
    
  3. Save and reload: source ~/.zshrc


💸 Start Coding (With Any Model)

Now you can use the CLI tool with the flexibility to switch models on the fly.

Basic Usage:

codex "Write a hello world function in Python"

Switching Models:

You can override the default model defined in your config using the -m flag.

# Use Gemini
codex -m gemini-3-pro-preview "hi"

# Use Claude
codex -m claude-sonnet-4-20250514 "hi"

# Use GPT-4o
codex -m gpt-4o "hi"

Why this is better:

  • Flexibility: Switch between Grok, Claude, and GPT without changing tools.
  • Cost Efficiency: Pay only for the tokens you use via Zeabur, rather than paying fixed monthly fees for multiple AI subscriptions.

🔧 Troubleshooting

If you run into issues:

  • 401 Unauthorized:
    • Check that base_url in your config includes /v1.
    • Verify your environment variable is set: echo $ZEABUR_API_KEY.
  • Model Not Found:
    • Ensure the model name (e.g., gpt-4o) is currently supported by Zeabur AI Hub. Check the Zeabur dashboard for the list of available model IDs.

Happy Coding!