INTEGRITY Документация

xAI

Конечная точка

https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok

Структура URL

При отправке запросов к Grok, замените https://api.x.ai/v1 в URL, который вы сейчас используете с https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok.

Предварительные требования

Отправляя запросы к Grok, убедитесь, что у вас есть следующее:

Примеры

cURL

Запрос
curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok/v1/chat/completions \
  --header 'content-type: application/json' \
  --header 'Authorization: Bearer {xai_api_token}' \
  --data '{
    "model": "grok-4",
    "messages": [
        {
            "role": "user",
            "content": "What is Cloudflare?"
        }
    ]
}'

Использование OpenAI SDK с JavaScript

Если вы используете OpenAI SDK с JavaScript, эндпойнт можно задать так:

JavaScript
import OpenAI from "openai";

const openai = new OpenAI({
	apiKey: "<api key>",
	baseURL:
		"https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok",
});

const completion = await openai.chat.completions.create({
	model: "grok-4",
	messages: [
		{
			role: "system",
			content:
				"You are Grok, a chatbot inspired by the Hitchhiker's Guide to the Galaxy.",
		},
		{
			role: "user",
			content: "What is the meaning of life, the universe, and everything?",
		},
	],
});

console.log(completion.choices[0].message);

Использование OpenAI SDK с Python

Если вы используете OpenAI SDK с Python, эндпойнт можно задать так:

Python
import os
from openai import OpenAI

XAI_API_KEY = os.getenv("XAI_API_KEY")
client = OpenAI(
    api_key=XAI_API_KEY,
    base_url="https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok",
)

completion = client.chat.completions.create(
    model="grok-4",
    messages=[
        {"role": "system", "content": "You are Grok, a chatbot inspired by the Hitchhiker's Guide to the Galaxy."},
        {"role": "user", "content": "What is the meaning of life, the universe, and everything?"},
    ],
)

print(completion.choices[0].message)

Использование Anthropic SDK с JavaScript

Если вы используете Anthropic SDK с JavaScript, эндпойнт можно задать так:

JavaScript
import Anthropic from "@anthropic-ai/sdk";

const anthropic = new Anthropic({
	apiKey: "<api key>",
	baseURL:
		"https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok",
});

const msg = await anthropic.messages.create({
	model: "grok-beta",
	max_tokens: 128,
	system:
		"You are Grok, a chatbot inspired by the Hitchhiker's Guide to the Galaxy.",
	messages: [
		{
			role: "user",
			content: "What is the meaning of life, the universe, and everything?",
		},
	],
});

console.log(msg);

Использование Anthropic SDK с Python

Если вы используете Anthropic SDK с Python, эндпойнт можно задать так:

Python
import os
from anthropic import Anthropic

XAI_API_KEY = os.getenv("XAI_API_KEY")
client = Anthropic(
    api_key=XAI_API_KEY,
    base_url="https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok",
)

message = client.messages.create(
    model="grok-beta",
    max_tokens=128,
    system="You are Grok, a chatbot inspired by the Hitchhiker's Guide to the Galaxy.",
    messages=[
        {
            "role": "user",
            "content": "What is the meaning of life, the universe, and everything?",
        },
    ],
)

print(message.content)

OpenAI-совместимый эндпойнт

Вы также можете обращаться к моделям Grok через схему OpenAI API, используя REST API. Отправляйте свои запросы на:

https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/v1/chat/completions

Укажите:


{
"model": "grok/{model}"
}