
Maintaining a consistent online presence in today’s digital landscape is crucial for businesses and individuals. Automating your social media posts, particularly on Twitter, can save time and ensure regular engagement with your audience. In this article, we’ll use Python, the Tweepy library, and Anaconda to post tweets using a script.
There are multiple ways of running this script. However, I like using Jupyter Notebook to run a Python script, so I’ll show it in this article and skip the steps on installing Anaconda.
What You’ll Need
- Python: Anaconda, which includes Python, should be installed on your computer.
- Twitter Developer Account: Sign up for a Twitter Developer account and create a new app to get your API keys and tokens. I subscribed for the free version, and it worked.
- Tweepy Library: Install the Tweepy library in your Anaconda environment. You can do this using the Anaconda Prompt or a Jupyter Notebook.
👆The links in this section will take you to download or documentation pages.
Step 1: Installing Tweepy
Open your terminal and run the following command:
pip install tweepy
Step 2: Setting Up X API Credentials
To interact with X’s API, you must authenticate your requests using API credentials. Follow these steps to get your credentials:
- Go to the X Developer Portal.
- Create a new app and navigate to the “Keys and tokens” section.
- Generate your API key, API secret key, Access token, and Access token secret.
Bonus: make sure your Access Token and Secret is created with Read and Write permissions, if it says just “Read” but then you modify it to “Read and Write” but it still shows only “Created with Read permissions”, you’ll need to Regenrate the tokens to get them updated.

Step 3: Writing the Script in Jupyter Notebook
Open Jupyter Notebook and create a new Python Notebook and copy this substituing keys with your own values:
import tweepy
# Twitter API credentials
consumer_key = 'your_new_consumer_key'
consumer_secret = 'your_new_consumer_secret'
access_token = 'your_new_access_token'
access_token_secret = 'your_new_access_token_secret'
bearer_token = 'your_bearer_token'
# Authenticate to Twitter
client = tweepy.Client(
bearer_token=bearer_token,
consumer_key=consumer_key,
consumer_secret=consumer_secret,
access_token=access_token,
access_token_secret=access_token_secret
)
# Define the tweet content
text_test = 'Hello! This is a Test Tweet 👏💰⭐️'
# Post the tweet
response = client.create_tweet(
text=text_test
)
print(f"https://twitter.com/user/status/{response.data['id']}")
Step 4: Running the Script
Execute the script: Run the cell by pressing Shift + Enter. If everything is set up correctly, you should see the URL of the posted tweet printed in the output, and your tweet should be posted to your account.
Recap
This script is handy; this basic automation can include more complex tasks such as scheduling, fetching and analyzing, and interacting with your followers programmatically.
There are countless ways to automate daily tasks these days. Whenever I come across a new one, it’s like a little mind-blowing moment. What about you? What other automation methods have you discovered? Share your experiences in the comments below!
Thank you for reading! If you enjoyed this article, please consider supporting me in the following ways:
- Like this story 👏 as often as you like, or Leave a comment 💬 to share your thoughts and feedback.
- Follow me on Medium to stay updated with my latest articles.
- Follow me on X for daily programming tips and more!
- Buy me a coffee ☕️.
- Visit My Digital Store for PostgreSQL Tips, Ebooks, Excel Templates and more!
Your support means the world to me. Thank you!







Leave a comment