Watch the video
Show transcript
Welcome back. In Lesson A1 you designed an agent on paper. Today you'll write your first real line of AI code. It runs in your browser, so there's nothing to install. By the end, Grok will answer a question that your own code sent.
First, two new words. An API is a way for one program to talk to another. Think of a drive-through. Your code pulls up and orders. Grok's servers hand back the answer.
An API key is a secret code that proves the order is yours. It's tied to your bill. So treat it like a password.
Let's talk about cost before we touch anything. The Grok API is pay as you go. It is not Free. Here's xAI's pricing page. It has a section called "Text API Pricing," which lists prices per token. A token is a small piece of text, about part of a word. Always check this page, because prices change.
Our tip: check your xAI console for spending and key controls, and set a limit if one is offered, before you run anything. That way a mistake is less likely to run up a big bill.
Now make a key in the xAI console, on the API Keys page. Copy it. Notice that on my screen the key is fake. Never show your real key to anyone, and never put it in a screenshot.
Next, open Google Colab and start a new notebook. Look on the left side for a little key icon. That's Secrets.
Click it. Add a new secret. Name it X-A-I underscore A-P-I underscore K-E-Y, all capital letters. Paste your key as the value. Then turn on notebook access so this notebook can read it.
Why go to this trouble? OWASP, a respected security group, warns that secrets often end up, quote, "hardcoded within the source code in plaintext." End quote. That means anyone who sees the code can see the key. Secrets keep it hidden.
Now copy the code block from the lesson page and paste it into one cell. Let's walk through it.
The first line installs a small helper. The next lines read your key from Secrets. Notice that the key is never typed into the code. Then we tell it where Grok lives, api dot x dot a i slash v1. That's the address from xAI's quickstart and overview docs. Then we pick the model, grok 4.7, and write our question. "Explain what an API is in one sentence, for a 12-year-old."
Press Run. Give it a moment. And there's Grok's answer, printed right under the cell. When we ran it, Grok said an API is like a waiter. It takes your request to another program and brings back what you asked for. Your code just talked to Grok.
Here's what happened. One, your code read the key from Secrets. Two, it sent your question to Grok. Three, Grok sent back an answer.
If you get an error about the model name, don't worry. Model names change. Open the xAI docs, copy the current name, and run it again.
Try changing the question. Ask Grok anything you like, and press Run again. Our tip: each run uses tokens, so keep your tests short.
Other AI companies work the same way. A key, a request, an answer. OpenAI and Anthropic each have their own keys and their own prices. Links are on the lesson page.
Safety check. Keep the key in Colab Secrets or a dot env file, never in code. Our tip: check your xAI console for spending and key controls. And if your key ever shows up in a screenshot or a chat, look for a way to remove it there and make a new one.
Quick check. What's an API key like? A password tied to your bill. Where did we keep it? In Colab Secrets. Is the Grok API Free? No, it's pay as you go. Great job. Next, in Lesson A3, Grok becomes your coding partner.
What you'll learn
- What an API and an API key are
- How to keep a key secret in Colab
- How to send your first message to Grok with code
Step 1: Two new words
- API: a way for one program to talk to another. Think of it as a drive-through window. Your code orders, and Grok's servers hand back an answer.
- API key: a secret code that proves the order is yours. It's tied to your bill, so treat it like a password.
Step 2: Know the cost first
The Grok API is pay as you go, not Free. xAI's pricing page has a section called "Text API Pricing," which lists prices per token. (xAI pricing) A token is a small piece of text, about part of a word. Check the pricing page before you start, because prices change.
Our tip: check your xAI console for spending and key controls, and set a limit if one is offered, before you run anything.
Step 3: Get a key and hide it
- Make an API key in the xAI console. xAI's quickstart says to get one from the "API Keys page." (xAI quickstart)
- Open a new notebook at colab.research.google.com.
- Click the key icon on the left side to open Secrets. Add a secret named
XAI_API_KEY, paste your key as the value, and turn on notebook access so this notebook can read it. (Google Colab example notebook)
Why not paste the key into the code? OWASP, a respected security group, warns that secrets often end up "hardcoded within the source code in plaintext." (OWASP Secrets Management Cheat Sheet) That means anyone who sees your code sees your key.
Step 4: Try it (copy, paste, Run)
Paste this into one Colab cell and press the Run button.
!pip -q install openai
from google.colab import userdata
from openai import OpenAI
client = OpenAI(
api_key=userdata.get("XAI_API_KEY"), # read from Colab Secrets, never typed here
base_url="https://api.x.ai/v1",
)
response = client.responses.create(
model="grok-4.7",
input="Explain what an API is in one sentence, for a 12-year-old.",
)
print(response.output_text)
This matches the pattern in xAI's own docs, which use base_url = "https://api.x.ai/v1" and the model name grok-4.7. (xAI quickstart, xAI overview) If you see an error about the model, open the xAI docs and copy the current model name.
What just happened: 1. Your code read the key from Secrets. 2. It sent your question to Grok through the API. 3. Grok's answer printed below the cell.
Grok's real answer (our test run)
We ran this exact prompt through Grok (model grok-4.7) on Oct 30, 2026. Grok's answers change from run to run, so yours will differ. Quoted as Grok returned it, except where we mark [trimmed] and where we swapped dashes for commas to match our style.
An API is like a waiter: it takes your request to another program, brings back what you asked for, and you don’t have to know how the kitchen works.
Step 5: Same idea, other AIs
Other AI companies work the same way: a key, a request, an answer. See the OpenAI agents guide and Anthropic tool use docs. Each has its own key and its own prices.
Check yourself
- What is an API key like? (Answer: a password tied to your bill)
- Where did we store the key? (Answer: Colab Secrets)
- Is the Grok API Free? (Answer: No, it's pay as you go)
Caution box Keep the key in Colab Secrets or a .env file, never in code. (OWASP) Our tip: check your xAI console for spending and key controls. If a key ever shows up in a screenshot or a chat, look for a way to remove it there and make a new one.
Builds on
Sources
- xAI pricing: https://docs.x.ai/developers/pricing
- xAI quickstart: https://docs.x.ai/developers/quickstart
- xAI overview: https://docs.x.ai/overview
- Google Colab example notebook (Secrets, key icon, notebook access): https://github.com/googlecolab/colabtools/blob/main/notebooks/Last_minute_Holiday_shopping_with_Gemini.ipynb
- OWASP Secrets Management Cheat Sheet: https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html
- OpenAI agents guide: https://developers.openai.com/api/docs/guides/agents
- Anthropic tool use: https://platform.claude.com/docs/en/agents-and-tools/tool-use/overview
Creator credits
- xAI (Grok): X https://x.com/grok , https://x.com/spacexsi
- OWASP: X https://x.com/owasp , YouTube https://www.youtube.com/@OWASPGlobal
- Anthropic: X https://x.com/AnthropicAI , YouTube https://www.youtube.com/@anthropic-ai