Library
xAI API
The xAI API gives Grok a tool called X Search. When you turn it on, Grok can search live posts on X by keyword, by topic, even filtered to specific handles or date ranges and pull real content back with citations to the original posts.
Step 1 : Get into the xAI developer console and add $5
- Go to the xAI developer console at console.x.ai.
- Sign in with your X account.
- This is a paid API — there's no free tier that lets you skip this. You need to add a minimum of $5 in credits under the Billing section before your key will work. (It's genuinely cheap for learning — a handful of test queries costs cents.)
Step 2 : Create your API key
- In the console, go to API Keys.
- Click Create API Key, give it a name (like
x-search-project), and copy it. - Copy it now — you usually can't see the full key again after you close that box.
Then set it as an environment variable so you never paste it into your code:
Mac / Linux:
export XAI_API_KEY="your_key_here"
Windows (PowerShell):
setx XAI_API_KEY "your_key_here"
Step 3 : Install the xAI SDK
pip install xai-sdk
Step 4 : Run the sample code
xai_x_search.py
import os
from xai_sdk import Client
from xai_sdk.chat import user
from xai_sdk.tools import x_search
client = Client(api_key=os.getenv("XAI_API_KEY"))
chat = client.chat.create(
model="grok-4.6", # reasoning model
tools=[x_search()],
include=["verbose_streaming"],
)
chat.append(user("What are people saying about xAI on X?"))
is_thinking = True
for response, chunk in chat.stream():
for tool_call in chunk.tool_calls:
print(f"\nCalling tool: {tool_call.function.name} with arguments: {tool_call.function.arguments}")
if response.usage.reasoning_tokens and is_thinking:
print(f"\rThinking... ({response.usage.reasoning_tokens} tokens)", end="", flush=True)
if chunk.content and is_thinking:
print("\n\nFinal Response:")
is_thinking = False
if chunk.content and not is_thinking:
print(chunk.content, end="", flush=True)
print("\n\nCitations:")
print(response.citations)
Step 5 : Turn the results into a real dataset with pandas
xai_to_dataset.py
import os
import re
import json
import pandas as pd
from xai_sdk import Client
from xai_sdk.chat import user, system
from xai_sdk.tools import x_search
client = Client(api_key=os.getenv("XAI_API_KEY"))
chat = client.chat.create(
model="grok-4.6",
tools=[x_search()],
)
# Tell Grok to return clean, structured rows instead of a paragraph
chat.append(system(
"You are a data collection assistant. Use X Search to find real posts. "
"Return ONLY a JSON array. Each item must have these exact keys: "
"author_handle, post_text, date, sentiment, url. "
"sentiment must be one of: positive, negative, neutral. "
"Do not add any commentary before or after the JSON."
))
# Your research question — change this to whatever you want a dataset about
chat.append(user("Find 20 recent posts about xAI on X."))
response = chat.sample() # get the full response (no streaming needed here)
raw = response.content
# Grok sometimes wraps JSON in ```json ... ``` fences — strip them out
match = re.search(r"\[.*\]", raw, re.DOTALL)
data = json.loads(match.group(0)) if match else []
# Build the dataset
df = pd.DataFrame(data)
print(df.head())
# Save it so you can use it anywhere (Excel, Power BI, Tableau, notebooks)
df.to_csv("x_posts_dataset.csv", index=False)
print(f"\nSaved {len(df)} rows to x_posts_dataset.csv")
# Quick analysis — sentiment breakdown, one line
print("\nSentiment counts:")
print(df["sentiment"].value_counts())
Project ideas you can actually put on your resume
- Brand sentiment tracker — pull posts about a product (or two competitors) and compare the positive/negative split.
- Event reaction analysis — pick something that just happened (a launch, a match, an earnings call) and analyze how X reacted over the day.
- Topic trend monitor — run the same query daily for a week, append to one CSV, and chart how sentiment shifts over time.
- "What's the community complaining about?" — pull posts about a tool (even the one you're learning, like Power BI or SQL) and cluster the common complaints.