Back to use cases

    Use case / X DATA / Python 3 / Published

    Find X posts for a prompt with Python

    Pass a prompt and get the X posts that match it, including text, links and engagement counts. The example is one SDK call that prints the posts it received.

    Card for Find X Posts for a Prompt with Python

    X Post Links Search sends a prompt and count to POST /desearch/ai/search/links/twitter through ai_x_links_search. It takes a prompt and a count, asks for X posts that match, and prints them. Each post can include text, a URL and engagement counts.

    The problem

    A keyword search on X returns a timeline. When you want posts for a prompt, with the URL and the counts in one object, a single links call is the smaller tool.

    What the app does

    • Sends a prompt and a count.
    • Prints the posts that came back.

    How it works

    The file calls ai_x_links_search with prompt and count. It posts to POST /desearch/ai/search/links/twitter.

    This is separate from GET /twitter, which takes an X query string, sort and date filters. Use that endpoint when you need operators such as since: or min_faves:.

    API details

    Verified against the live OpenAPI specification on 2026-11-25. These are schema fields, not a captured run.

    The SDK call shown below passes prompt="Bittensor" and count=10. In the HTTP request, both are JSON body fields. LinksSearchRequestTwitter requires prompt; count defaults to 10 and accepts 10 to 200.

    The response is an object with a required miner_tweets array, while GET /twitter documents a bare post list as one of its response shapes. This schema projection retains the post identifier and text:

    JSON
    {
      "type": "object",
      "properties": {
        "miner_tweets": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "id": {
                "type": "string"
              },
              "text": {
                "type": "string"
              }
            },
            "required": [
              "id",
              "text"
            ]
          }
        }
      },
      "required": [
        "miner_tweets"
      ]
    }

    Post metadata has different guarantees: url and view_count are optional nullable fields in TwitterScraperTweet. Code that exports results should allow them to be missing rather than assuming every post has a URL or a view count. This prompt request does not send the keyword endpoint's sort or date parameters.

    API reference: POST /desearch/ai/search/links/twitter. Related walkthrough: AI search links and cited answers.

    The code

    PYTHON
    import asyncio
    import os
    
    from desearch_py import Desearch
    
    # Set DESEARCH_API_KEY in your environment to a key from the Desearch console. Do not commit a real key.
    API_KEY = os.environ["DESEARCH_API_KEY"]
    
    
    async def main():
        # desearch-py 1.2.1: ai_x_links_search(prompt, count)
        # posts to POST /desearch/ai/search/links/twitter.
        async with Desearch(api_key=API_KEY) as desearch:
            result = await desearch.ai_x_links_search(prompt="Bittensor", count=10)
            print(result)
    
    
    if __name__ == "__main__":
        asyncio.run(main())

    Set DESEARCH_API_KEY in your environment to your API key. Do not commit a real key.

    Endpoints

    • POST /desearch/ai/search/links/twitter

    Example output

    The script prints the response. The file does not include a saved sample run or a measured cost.

    Run it yourself

    The file linked above is twitter_links_search.py in the examples repository. Install desearch-py 1.2.1, create a key at console.desearch.ai, and call ai_x_links_search with a prompt and count.

    What it costs

    This example does not record X-Desearch-Cost-Usd. Current responses include that header. See pricing.

    Ideas to extend it

    • Write the posts to the CSV exporter's columns.
    • Follow the loudest post into the replies example.
    • Combine the posts with web links for the same prompt.

    Use with AI agents

    Point an MCP client at the hosted Desearch server. Replace the placeholder with a key from the console. Do not commit the key.

    JSON
    {
      "mcpServers": {
        "desearch": {
          "url": "https://mcp.desearch.ai/mcp",
          "headers": {
            "Authorization": "Bearer <key>"
          }
        }
      }
    }

    Prompt you can paste for this example:

    Use Desearch to find X posts for the prompt I give you. Return the posts you received with text, URL and engagement counts. Do not add posts you did not retrieve.

    Build your own example.

    Give your agents access to web and social search. Get an API key to start building, or talk to us about your integration.

    Read docs