Documentation

Cette page n'est pas encore traduite — version anglaise affichée.

How to Call Grok Models on Code By AI

Ce guide nécessite une clé API

Créez un compte et générez-en une en une minute environ, puis utilisez-la là où les étapes ci-dessous attendent une clé.

Inscription sans carte · un rechargement est requis pour utiliser l’API

Code By AI is a third-party, prepaid, multi-model API gateway. Grok models are provided by xAI, but you reach them through Code By AI's OpenAI-compatible Chat Completions endpoint with a Code By AI key, not through xAI's official API. This guide covers the base URL, explicit Grok model IDs, curl and OpenAI SDK requests, and the OpenCode configuration.

Before you start

You need a Code By AI account, an API key, and a positive balance. Creating an account does not require a card, but requests draw on your prepaid balance, so top up before sending one.

  1. Sign in or create an account.
  2. Create a key on the API Keys page. Keys start with sk-proj-.
  3. Add funds on the wallet page. The current first-top-up offer, if any, is shown there and on the homepage; this guide does not restate a fixed amount.

Keep the key in the CODEBYAI_API_KEY environment variable used by the examples below. Do not paste it into shared code.

Models and the Chat Completions endpoint

Code By AI serves three Grok models. Each model page links the current rates:

ModelAPI model IDModel page
Grok 4.5grok-4.5grok-4.5
Grok 4.6grok-4.6grok-4.6
Grok 4.7grok-4.7grok-4.7

Grok is available only on OpenAI-compatible Chat Completions:

  • Full request URL: https://www.codebyai.net/api/agent/v1/chat/completions. The gateway also routes https://www.codebyai.net/api/agent/chat/completions.
  • Base URL for SDKs and OpenCode: https://www.codebyai.net/api/agent. The client appends /chat/completions.

Send the exact catalog ID in model on every request. These models are not channel defaults.

If model is omitted, Chat Completions uses the configured Codex default model, not Grok. A Grok ID the gateway does not recognize returns HTTP 400 with an unknown_model error; the gateway does not fall back to a default.

Send a request with curl

Export the key, then call the full Chat Completions path with an explicit Grok ID:

1
export CODEBYAI_API_KEY="sk-proj-xxxx"
2
 
3
curl --fail-with-body "https://www.codebyai.net/api/agent/v1/chat/completions" \
4
  -H "Authorization: Bearer $CODEBYAI_API_KEY" \
5
  -H "content-type: application/json" \
6
  -d '{
7
    "model": "grok-4.5",
8
    "messages": [{ "role": "user", "content": "Hello" }]
9
  }'

Replace grok-4.5 with grok-4.6 or grok-4.7 to use another model. The request is billed to your Code By AI balance at the Grok rate shown on the model page.

Use the OpenAI SDK

Point the official OpenAI SDK at the Code By AI base URL and pass the model ID on every call. The SDK reads the key from the environment variable set above:

1
import os
2
from openai import OpenAI
3
 
4
client = OpenAI(
5
    base_url="https://www.codebyai.net/api/agent",
6
    api_key=os.environ["CODEBYAI_API_KEY"],
7
)
8
 
9
response = client.chat.completions.create(
10
    model="grok-4.5",
11
    messages=[{"role": "user", "content": "Hello"}],
12
)
13
print(response.choices[0].message.content)

The SDK appends /chat/completions to the base URL. Do not set the base URL to the full /v1/chat/completions path.

Configure OpenCode

OpenCode reaches Code By AI through its existing OpenAI-compatible provider. Merge the provider.codebyai entry below into your OpenCode configuration, keeping any other models you already have:

1
{
2
  "provider": {
3
    "codebyai": {
4
      "npm": "@ai-sdk/openai-compatible",
5
      "options": {
6
        "baseURL": "https://www.codebyai.net/api/agent",
7
        "apiKey": "{env:CODEBYAI_API_KEY}"
8
      },
9
      "models": {
10
        "grok-4.5": { "name": "Grok 4.5" }
11
      }
12
    }
13
  }
14
}

The key under provider.codebyai.models is the exact ID OpenCode sends. Add grok-4.6 or grok-4.7 the same way, then start OpenCode with the model you declared:

1
opencode -m codebyai/grok-4.5

Replace grok-4.5 with the ID you added. The full OpenCode setup, including where the configuration file lives and how to export the key, is in the OpenCode guide.

What this configuration does not cover

Grok is served on Chat Completions only. Do not send a Grok ID to these routes or clients:

  • the Responses API (/v1/responses);
  • the Anthropic Messages API (/v1/messages);
  • the Gemini generateContent endpoint;
  • Claude Code, Codex CLI, or Gemini CLI.

Those routes serve other model families and reject Grok IDs. This page documents Chat Completions with curl, the OpenAI SDK, and OpenCode only; it makes no claim about Grok support in Pi, Cline, CodeBuddy, or other clients.

This guide also does not configure or promise xAI's Web Search, X Search, Imagine, image or video generation, or Voice, and it uses no latest alias. Use the exact catalog IDs listed above.

Troubleshooting

  • 400 with unknown_model: the gateway does not recognize the ID. Use grok-4.5, grok-4.6, or grok-4.7 exactly.
  • 401 with authentication_error: the API key is missing or malformed. Check that CODEBYAI_API_KEY is set in the terminal that runs the request and sent as a Bearer token.
  • 403 with Invalid key: the supplied key is invalid, revoked, or expired. Check it on the API Keys page and replace it if needed.
  • 404 page not found: the full URL must end in /chat/completions. If you use an SDK, set only the /api/agent base URL.
  • Insufficient balance: add funds on the wallet page. A request rejected before the model answers is not charged.
  • A request to /v1/responses or /v1/messages fails with a Grok ID: that is expected. Use /v1/chat/completions.

Frequently asked questions

Which base URL should the OpenAI SDK or OpenCode use?

Use https://www.codebyai.net/api/agent. Those clients append /chat/completions themselves. For curl, send the full https://www.codebyai.net/api/agent/v1/chat/completions URL.

Which Grok model IDs can I send?

Use grok-4.5, grok-4.6, or grok-4.7 for new requests. The legacy grok-4-5 ID is still accepted and billed at the grok-4.5 rate. Unrecognized Grok IDs return HTTP 400; the gateway does not map latest aliases.

Does omitting model use Grok?

No. On Chat Completions, an omitted model selects the configured Codex default model, which is a GPT model. Always send an explicit Grok ID.

Can I use Grok with Claude Code, Codex CLI, or Gemini CLI?

No. Grok is available on Chat Completions only. Use curl, the OpenAI SDK, or the OpenCode configuration on this page.

Is this xAI's official API?

No. Code By AI is a third-party prepaid gateway. The Grok models come from xAI, but keys, billing, endpoints, and support are handled by Code By AI. Use an xAI account and xAI's own API if you need xAI's direct service.

Create a key on the API Keys page, top up on the wallet page, and check pricing for the current Grok rates. Model pages: grok-4.5, grok-4.6, grok-4.7.

Premiers pas

Quatre étapes jusqu’à votre première requête

  1. 01

    Créer un compte gratuit

    Inscrivez-vous avec Google, GitHub ou votre email et définissez un mot de passe. Aucune carte n’est requise.

  2. 02

    Générer une clé API

    Créez une clé en un clic dans le tableau de bord pour authentifier votre CLI auprès de Code By AI.

  3. 03

    Recharger votre solde

    Ajoutez du crédit avant votre première requête ; l’utilisation est facturée au token sur votre solde.

  4. 04

    Pointer votre CLI vers Code By AI

    Exportez une variable d’URL de base, ou ajoutez un bloc de fournisseur de modèle dans config.toml. Les guides d’installation donnent les lignes exactes pour chaque agent.

Utiliser maintenantRecharger pour envoyer une requêteLe coût d’un mois de travail

Inscription sans carte · un rechargement est requis pour utiliser l’API