Công cụ & IDE

Cấu hình OpenCode

OpenCode là AI coding agent chạy trong terminal, hỗ trợ nhiều nhà cung cấp model thông qua cơ chế provider. Tài liệu này hướng dẫn cấu hình OpenCode dùng API OpenAI-compatible của ShopAIKey.

Endpoint

code
https://api.shopaikey.com/v1

1. Cài đặt OpenCode

Bạn có thể cài bằng npm:

code
npm install -g opencode

Hoặc dùng Homebrew trên macOS/Linux:

code
brew install opencode-ai/tap/opencode

Kiểm tra sau khi cài:

code
opencode --version

2. Thêm API key vào OpenCode

Chạy lệnh:

code
opencode auth login

Chọn Other, nhập provider id:

code
shopaikey

Sau đó nhập API key ShopAIKey của bạn.

Provider id shopaikey phải trùng với key trong file opencode.json ở bước tiếp theo.

3. Tạo file opencode.json

Trong thư mục dự án bạn muốn dùng OpenCode, tạo hoặc cập nhật file:

code
opencode.json

Nội dung mẫu:

code
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "shopaikey": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "ShopAIKey",
      "options": {
        "baseURL": "https://api.shopaikey.com/v1"
      },
      "models": {
        "gpt-5.2": {
          "name": "GPT-5.2"
        },
        "gpt-5.3-codex": {
          "name": "GPT-5.3 Codex"
        },
        "claude-sonnet-4-5": {
          "name": "Claude Sonnet 4.5"
        },
        "claude-opus-4-5": {
          "name": "Claude Opus 4.5"
        }
      }
    }
  }
}

4. Chọn model

Mở OpenCode:

code
opencode

Trong giao diện OpenCode, chạy:

code
/models

Chọn provider ShopAIKey và model bạn muốn dùng.

5. Cấu hình API key trực tiếp trong file

Nếu không muốn dùng opencode auth login, bạn có thể đặt key qua biến môi trường.

macOS / Linux

code
export SHOPAIKEY_API_KEY="sk-your-api-key"

Windows PowerShell

code
$env:SHOPAIKEY_API_KEY = "sk-your-api-key"

Sau đó sửa opencode.json:

code
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "shopaikey": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "ShopAIKey",
      "options": {
        "baseURL": "https://api.shopaikey.com/v1",
        "apiKey": "{env:SHOPAIKEY_API_KEY}"
      },
      "models": {
        "gpt-5.2": {
          "name": "GPT-5.2"
        }
      }
    }
  }
}

6. Lưu ý

  • Dùng @ai-sdk/openai-compatible cho endpoint https://api.shopaikey.com/v1.
  • Nếu đổi opencode.json, hãy khởi động lại OpenCode để chắc chắn cấu hình mới được nạp.
  • Model id phải đúng với model ShopAIKey đang hỗ trợ.
  • Nếu gặp lỗi Invalid API Key, kiểm tra key còn credit và chưa bị thu hồi.
  • Nếu gặp lỗi Model Not Found, thử đổi sang model khác trong trang Models.

7. Kiểm tra nhanh

Trong thư mục dự án, chạy:

code
opencode

Gửi thử yêu cầu:

code
Giải thích cấu trúc dự án này và đề xuất điểm cần cải thiện.

Nếu phản hồi trả về bình thường, cấu hình đã hoạt động.