DEV Community

Cover image for I sent tweets via CLI with a new tool from X.
Everton Tenorio
Everton Tenorio

Posted on • Edited on

1 1

I sent tweets via CLI with a new tool from X.

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.

xurl

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!"}'
Enter fullscreen mode Exit fullscreen mode

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!"
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay