API / set-x-search-date-range

API

Set an X Search date range

Set an X Search date range

Pass from_date and to_date on the x_search tool to limit results to that inclusive window. Dates use ISO 8601 (YYYY-MM-DD). Base setup: Search X posts with the Grok API.

xAI SDK

In the xAI Python SDK, pass datetime.datetime objects:

import os
from datetime import datetime

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(
            from_date=datetime(2025, 10, 1),
            to_date=datetime(2025, 10, 10),
        ),
    ],
)

chat.append(user("What did people on X say about xAI that week?"))
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 did people on X say about xAI that week?"
    }
  ],
  "tools": [
    {
      "type": "x_search",
      "from_date": "2025-10-01",
      "to_date": "2025-10-10"
    }
  ]
}'

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 did people on X say about xAI that week?',
  tools: {
    x_search: xai.tools.xSearch({
      fromDate: '2025-10-01',
      toDate: '2025-10-10',
    }),
  },
});

console.log(text);

Combine with handle filters

You can set a date window together with allowed_x_handles or excluded_x_handles on the same tool. See Restrict X Search to specific handles.

Pitfalls

  • Both ends of the range are inclusive.
  • Use YYYY-MM-DD strings on Responses / AI SDK; Python SDK accepts datetime objects.
  • Tool calls bill tokens plus invocations. Narrow windows cut noise and often cut cost.
  • Console API credits are separate from SuperGrok's weekly pool on grok.com.