Restrict X Search to specific handles
Restrict X Search to specific handles
Pass allowed_x_handles (max 20) on the x_search tool so Grok only considers posts from those accounts. Base setup: Search X posts with the Grok API.
xAI SDK
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",
tools=[
x_search(allowed_x_handles=["elonmusk", "xai"]),
],
)
chat.append(user("What is the current status of xAI?"))
response = chat.sample()
print(response.content)
OpenAI Responses
curl https://api.x.ai/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $XAI_API_KEY" \
-d '{
"model": "grok-4.6",
"input": [
{
"role": "user",
"content": "What is the current status of xAI?"
}
],
"tools": [
{
"type": "x_search",
"allowed_x_handles": ["elonmusk", "xai"]
}
]
}'
Vercel AI SDK
import { xai } from '@ai-sdk/xai';
import { generateText } from 'ai';
const { text } = await generateText({
model: xai.responses('grok-4.6'),
prompt: 'What is the current status of xAI?',
tools: {
x_search: xai.tools.xSearch({
allowedXHandles: ['elonmusk', 'xai'],
}),
},
});
console.log(text);
Exclude handles instead
Use excluded_x_handles (also max 20) when you want the open firehose minus a short blocklist. Pass handles without the @.
x_search(excluded_x_handles=["elonmusk"])
Pitfalls
allowed_x_handlesandexcluded_x_handlescannot share the same request.- Handles are account names without
@, up to 20 each. - Pair with
from_date/to_datewhen you also need a window: Set an X Search date range. - Console API credits are separate from SuperGrok's weekly pool on grok.com.