A collection of pragmatic, real-world examples guiding you from basic to advanced use of xAI's Grok APIs.
572
stars
28
commits
TypeScript
primary language
Apr 16, 2026
updated
A collection of pragmatic, real-world examples guiding you from basic to advanced use of xAI's Grok APIs for practical, powerful applications.
Below are steps to run these notebooks locally with your own API key, which you can obtain over at the xAI Console. If you’d rather not run them, all notebook cells are preserved with outputs intact, so you can follow along just by reading.
git clone https://github.com/xai-org/xai-cookbook.git
cd xai-cookbook
uv sync
uv run pre-commit install
git lfs install
.env.example file and rename it to .env and add your API key (the repo is set-up to ignore .env so that it won't be committed).:
cp .env.example .env
uv run jupyter notebook examples/
Alternatively, use VS Code’s notebook extension:
.venv (created by uv sync) via the interpreter selector.Never hardcode API keys (e.g., XAI_API_KEY=my-key), even during development. This risks accidental commits. Instead, use the .env file. In your notebook, load the key like this:
from dotenv import load_dotenv
import os
load_dotenv()
XAI_API_KEY = os.getenv("XAI_API_KEY")
This repo uses Git LFS to track large data files (e.g., images, large JSON) used by some notebooks. To work with these files:
Install Git LFS:
git lfs install
Files are tracked via .gitattributes, check or update it if adding new large files.
This repo includes a pre-commit hook with Gitleaks to block accidental API key commits. It’s not foolproof though, if you accidentally commit your key, revoke it immediately in the xAI Console and generate a new one.
There’s also a pre-commit hook to verify that your notebooks run end-to-end without errors.
It’s fine to skip these hooks during development using git commit --no-verify, especially if your notebook makes frequent requests to the xAI APIs, skipping can help preserve your API credits. That said, we strongly encourage you to run your changes with the hooks enabled at least once before submitting a pull request or responding to comments on an open PR. This ensures code quality and catches potential issues. To manually trigger the hooks, use pre-commit run after staging your changes. (e.g. uv run pre-commit run --files examples/your_notebook.ipynb)
Want to add a notebook? See CONTRIBUTING.md for guidelines. Key steps:
uv run jupyter notebook examples/..gitattributes for Git LFS.TypeScript
50.6%
Swift
21.6%
Kotlin
21.0%
Python
2.8%
Shell
2.5%
A collection of pragmatic, real-world examples guiding you from basic to advanced use of xAI's Grok APIs.
572
stars
28
commits
TypeScript
primary language
Apr 16, 2026
updated
A collection of pragmatic, real-world examples guiding you from basic to advanced use of xAI's Grok APIs for practical, powerful applications.
Below are steps to run these notebooks locally with your own API key, which you can obtain over at the xAI Console. If you’d rather not run them, all notebook cells are preserved with outputs intact, so you can follow along just by reading.
git clone https://github.com/xai-org/xai-cookbook.git
cd xai-cookbook
uv sync
uv run pre-commit install
git lfs install
.env.example file and rename it to .env and add your API key (the repo is set-up to ignore .env so that it won't be committed).:
cp .env.example .env
uv run jupyter notebook examples/
Alternatively, use VS Code’s notebook extension:
.venv (created by uv sync) via the interpreter selector.Never hardcode API keys (e.g., XAI_API_KEY=my-key), even during development. This risks accidental commits. Instead, use the .env file. In your notebook, load the key like this:
from dotenv import load_dotenv
import os
load_dotenv()
XAI_API_KEY = os.getenv("XAI_API_KEY")
This repo uses Git LFS to track large data files (e.g., images, large JSON) used by some notebooks. To work with these files:
Install Git LFS:
git lfs install
Files are tracked via .gitattributes, check or update it if adding new large files.
This repo includes a pre-commit hook with Gitleaks to block accidental API key commits. It’s not foolproof though, if you accidentally commit your key, revoke it immediately in the xAI Console and generate a new one.
There’s also a pre-commit hook to verify that your notebooks run end-to-end without errors.
It’s fine to skip these hooks during development using git commit --no-verify, especially if your notebook makes frequent requests to the xAI APIs, skipping can help preserve your API credits. That said, we strongly encourage you to run your changes with the hooks enabled at least once before submitting a pull request or responding to comments on an open PR. This ensures code quality and catches potential issues. To manually trigger the hooks, use pre-commit run after staging your changes. (e.g. uv run pre-commit run --files examples/your_notebook.ipynb)
Want to add a notebook? See CONTRIBUTING.md for guidelines. Key steps:
uv run jupyter notebook examples/..gitattributes for Git LFS.TypeScript
50.6%
Swift
21.6%
Kotlin
21.0%
Python
2.8%
Shell
2.5%