Grok is xAI's model family. Its API follows an OpenAI-compatible format, which means developers can call it with the same OpenAI SDK they already use for other models. In mainland China, the practical problems are usually access, payment, and keeping model quotas under control.

This guide walks through how to obtain a Grok API key, how to make your first call, and how to connect through a gateway when direct access is not practical.

Note: Grok and xAI are trademarks of their respective owners. This article reflects common versions as of October 2026. Interfaces and prices are subject to the official provider or the pricing page on this site.

1. What Grok API can do

Grok models cover a broad set of everyday development tasks:

  • General question answering, writing, and summarization.
  • Code generation and code explanation.
  • Tool calling and agent workflows.
  • Long-context document analysis.
  • Side-by-side testing against other models.

The exact model names and context lengths available to you depend on your provider. Check model pricing for the current list before you hard-code a model name.

2. Two ways to get a key

Official xAI

Create an API key in the xAI console and attach a payment method. This path suits teams that can access xAI reliably and already have an overseas payment method. Before you start, confirm the account requirements, billing currency, and whether your use case is permitted under the terms of service.

ZSQH AI Gateway

  1. Open the ZSQH AI Gateway and sign up.
  2. Create an API key and set a spending limit.
  3. Top up your balance.
  4. Call the API with the OpenAI SDK or curl.

This path suits teams whose access from mainland China is unreliable, or that want one bill and the ability to switch between models without managing several accounts.

3. Base URL

The official xAI endpoint is usually:

https://api.x.ai/v1

The ZSQH OpenAI-compatible endpoint is:

https://ai.zhongshangqihang.com/v1

Do not mix the two addresses. A key issued by one provider will not work against the other provider's base URL. Keep the key and the base URL paired.

4. curl test

The quickest way to confirm that your key, base URL, and model name are correct is a single curl request:

export GROK_API_KEY="sk-你的密钥"

curl "https://ai.zhongshangqihang.com/v1/chat/completions" \
  -H "Authorization: Bearer $GROK_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "grok-4",
    "messages": [{"role": "user", "content": "只回复 OK"}]
  }'

A successful response returns a normal Chat Completions object. Model names are subject to model pricing.

5. Python example

If you already use the OpenAI Python SDK, the only changes are the API key and the base URL:

from openai import OpenAI

client = OpenAI(
    api_key="sk-你的密钥",
    base_url="https://ai.zhongshangqihang.com/v1",
)

resp = client.chat.completions.create(
    model="grok-4",
    messages=[{"role": "user", "content": "用三句话解释 API 网关"}],
)
print(resp.choices[0].message.content)

Keep the client configuration in an environment variable or a server-side secret store. Do not ship the key inside a browser application.

6. Common errors

Error Cause Fix
401 Key and URL do not match Check the key and the base URL
404 Wrong path Use only up to /v1
model not found Wrong model name Copy the name from the pricing page
429 Rate limited Lower concurrency and retry with backoff
insufficient quota Low balance Top up and check the key limit

Most setup failures come from one of these five causes. Check them in order before changing your application code.

7. Cost and model selection

Grok's unit price, context window, and tool-calling behavior all affect the final bill. A practical estimate looks like this:

  1. Measure average daily input and output tokens from real logs.
  2. Look up the current price for the model you plan to use.
  3. Add the cost of retries and long-context requests.
  4. Compare with other models and assign tasks to the cheapest model that meets the quality bar.

Do not look only at the unit price. Long contexts and output tokens are often the largest part of the bill, especially in agent-style workloads that resend conversation history on every turn.

FAQ

Is Grok API compatible with OpenAI?

It generally supports the Chat Completions format, so the OpenAI SDK works for most calls. Specific parameter support depends on the official documentation or the gateway's documentation; test any advanced feature before relying on it.

How can I call it reliably from mainland China?

Use a direct official connection if your network allows it, or a gateway that is reachable from mainland China. In both cases, start with a small key and confirm latency and error rates before moving production traffic.

Can one key call multiple models?

With a gateway, yes. You specify the model in the request parameters, so the same key can reach different models as long as your account has access to them.

Next steps

If you need domestic access or a single entry point for multiple models, sign up for the ZSQH AI Gateway and test with a small key. Models and prices are subject to model pricing.

Related reading: