Twitter/X post Retrieval
Example project using the Exa Python SDK
Click here to try Exa-powered Twitter Retrieval
What this doc covers
- Creating an alternative way to retrieve tweets powered by Exa by searching our index (updated daily)
- Explain usage of Exa’s includeDomain and date filters
If you’re interested in searching for and retrieving Tweets, Exa can be a great way to do this semantically. X is included in our indexing and we are constantly crawling to keep public data fresh.
Why search for tweets?
- Read about your favorite topic or domain in a synthesized/targeted way
- Get notified about new hackathons/events (often primarily announced via Twitter)
- Monitor feedback/user experiences for your product
In this quick tutorial, we’ll use Exa to retrieve relevant X posts using the includeDomain parameter to filter to Twitter.com/X.com content only. The Jupyter notebook for this tutorial is available on Colab for easy experimentation.
To play with this code, we just need a Exa API key
Let’s start by installing the Exa Python SDK, importing the required libraries, and instantiating the Exa client.
Next, we’re going to use Exa to pull tweets from Twitter/X based on any query. But first, let’s define a helper function that renders tweets in Colab like you’d see them on Twitter (instead of plain text), for visualization purposes. You don’t need to understand how this works.
Now, let’s specify some parameters and call the Exa API.
We set include_domains
so that Exa only searches Twitter.com and X.com URLs for results. We also set the start_published_date
to thirty days before the current date. This way, we only get recent tweets.
And since things move so fast in AI, let’s set our query to find advances in AI so we can catch up on news from the last month.
Now, let’s put everything together.
You should see the actual tweets appearing right in your Colab output! Notice that Exa also provides a relevance score (from 0 to 1), indicating how well the retrieved content matches your query. A low score doesn’t necessary mean something is irrelevant but you may want to keep an eye on relevance scores, especially if you are requesting a lot of results. Similarly, if you Google something and go to the fiftieth result, that’s going to be less relevant than the first result.
Besides Twitter/X, Exa also indexes YouTube (and many other sites, see detailed info about what’s in our index here ). As a follow-up, experiment with setting includeDomain to “youtube.com” to find cool videos to binge!