Last updated: 2026-04-20
xAI (Grok) Integration
Route xAI Grok API calls through SpendLil.
SpendLil proxies xAI's API. Swap api.x.ai for xai.gateway.spendlil.ai and add X-SpendLil-Key. xAI is OpenAI-compatible — use the OpenAI SDK with the xAI gateway.
URL Pattern
diff Before → After
- POST https://api.x.ai/v1/chat/completions
+ POST https://xai.gateway.spendlil.ai/v1/chat/completions OpenAI SDK (Python)
python Use the OpenAI SDK with the xAI gateway
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ['XAI_API_KEY'],
base_url='https://xai.gateway.spendlil.ai/v1',
default_headers={'X-SpendLil-Key': os.environ['SPENDLIL_KEY']},
)
response = client.chat.completions.create(
model='grok-2-latest',
messages=[{'role': 'user', 'content': 'Hello'}],
)
print(response.choices[0].message.content) cURL
bash Direct HTTP request
curl https://xai.gateway.spendlil.ai/v1/chat/completions \
-H "Authorization: Bearer $XAI_API_KEY" \
-H "X-SpendLil-Key: $SPENDLIL_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "grok-2-latest",
"messages": [{"role": "user", "content": "Hello"}]
}' OpenAI-compatible
xAI's API mirrors OpenAI's chat completions schema. Any OpenAI SDK or tool that accepts a base_url override will work unchanged.