In this categoryGrok · 23
- How to Search X in Real Time with GrokStart
- How to Write a Blog Post Draft with Grok
- How to Fact-Check a Claim with Grok
- How to Summarize a Long X Thread with Grok
- How to Use Think Mode for Hard Problems in Grok
- How to Analyze an Image or Screenshot with Grok
- How to Research a Company Before a Meeting with Grok
- How to Search with Grok and Verify the Sources
- How to Build a Reusable Prompt Template in Grok
- How to Turn Grok Research into a Cited Report
- How to Track a Breaking News Event with Grok
- How to Call the Grok API from Python
- How to Generate Your First Image with Grok
- How to Write Grok Prompts That Get Better Images
- How to Edit an Uploaded Photo in Grok
- How to Remove or Replace a Background with Grok
- How to Create a Profile Picture or Avatar with Grok
- How to Generate a Short Video Clip with Grok
- How to Add or Remove Objects in a Photo with Grok
- How to Make a Product Mockup with Grok
- How to Make a Social Post Graphic with Readable Text in Grok
- How to Restore and Upscale an Old Photo with Grok
- How to Keep a Character Consistent Across Grok Images
How to Call the Grok API from Python
Send your first request to the xAI Grok API in Python using the OpenAI-compatible endpoint and a secure API key.
Beyond the chat interface, xAI offers an API so you can call Grok from your own code. It is OpenAI-compatible, which means existing OpenAI client libraries work with only a base URL and key change. This guide gets you from zero to a working Python request, with the key kept out of your source.
What you need
- An xAI account with API access and billing set up
- Python 3.9 or newer installed
- Basic comfort with the terminal and pip
- About 10 minutes
Step 1: Create an API key
Sign in to the xAI developer console and create a new API key. Copy it once and store it somewhere safe; you cannot view it again after closing the dialog. Never paste a key into code you will commit or share.
Step 2: Store the key as an environment variable
Export the key in your shell so your code reads it from the environment instead of a hardcoded string. This keeps the secret out of your codebase. Add the export to your shell profile if you want it to persist.
Step 3: Write the request
Point the OpenAI client at the xAI base URL and pass a Grok model name. The rest of the call looks like any chat completion. The example below reads the key from the environment and prints Grok's reply.
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["XAI_API_KEY"],
base_url="https://api.x.ai/v1",
)
resp = client.chat.completions.create(
model="grok-4",
messages=[
{"role": "system", "content": "You are a concise assistant."},
{"role": "user", "content": "Give me 3 uses for the xAI API."},
],
)
print(resp.choices[0].message.content)Step 4: Run it and handle errors
Run the script. A 401 means your key is wrong or not exported; a 429 means you hit a rate or spend limit. Wrap the call in a try block in real code so a failed request does not crash your program.
Result
You have made a live API call to Grok from Python with your key safely in the environment. From here you can swap in your own prompts, stream responses, or wire it into an app.
Watch related tutorials
16:24
09:15
13:20
11:10
14:05
15:30New guides in your inbox
Fresh step-by-step how-to guides as we publish them. One email a week, no more.