API & agents
M08 · 10 min
API quickstart
Account → API key → SDK → first request with grok-4.5.
- 01
Create account & key
console.x.ai → API keys. export XAI_API_KEY=…
- 02
Install an SDK
Python: xai-sdk or openai. JS: ai + @ai-sdk/xai, or openai with baseURL https://api.x.ai/v1.
- 03
First text call
Model grok-4.5 for code and general agentic work.
- 04
First image call
Imagine models e.g. grok-imagine-image-quality for assets.
curl sketch (coding)
curl https://api.x.ai/v1/responses \
-H "Authorization: Bearer $XAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "grok-4.5",
"input": "Fix this function and explain the bug: function median(a){a.sort();return a[a.length/2]}"
}'Exercise
Key hygiene plan
Write how you’ll store XAI_API_KEY (env, secrets manager) and where you will never put it (git, screenshots, client bundles).
- Storage plan
- Forbidden places listed
- Rotation idea (what if leaked)