I had tested the toot tool to make some posts on Mastodon via bash, and I really liked it because it made posting so much easier. So, I thought I should look for a similar way to do it on X.
Until then, I had only known about Tweepy, a Python library installable via pip. However, by the time I would have to run python tweepy.py
, I could waste a lot of time, so I needed a simpler method.
That's when I found out about a recent tool launched by the developers of Twitter(X), called xurl, a tool that resembles the famous utility curl.
So, following the README.md, I realized that I just needed to use the POST method on the '/2/tweets' endpoint, but first, important: authentication.
Authenticate
Make sure you first have a developer account and an app on X, pay close attention to how you will use the X API and the limits based on the plan you choose, then collect your Tokens and follow the steps in the project's README.md.
In the end, you will be able to successfully send a tweet like this:
xurl -X POST /2/tweets -d '{"text":"Hello world!"}'
However... typing this in bash every time I want to send a tweet is a bit tedious, so I created tw.sh
with a shell script.
tw.sh
tw "Message sent successfully!"
And that's it. Simple!
#!/bin/bash
# author: Everton Tenorio
send_tweet() {
tweet_text="$1"
if [ -z "$tweet_text" ]; then
echo "❌ You need to provide a tweet text!"
exit 1
fi
# Send tweet using xurl
xurl -X POST /2/tweets -d '{"text": "'"$tweet_text"'"}'
if [ $? -eq 0 ]; then
echo "✅ Tweet sent successfully!"
else
echo "❌ Error sending tweet!"
fi
}
if [ $# -eq 0 ]; then
echo "❌ Please provide the tweet text!"
echo "Usage: ./tw.sh 'Tweet text'"
exit 1
fi
# Send tweet
send_tweet "$1"
The next step will be sending media in tweets, but from what I understand, they are still working on that endpoint.
I didn’t save the script with .sh
since bash automatically recognizes it as a shell script. I just made it executable with chmod +x tw
. That’s it!
Top comments (0)