← Back to Blogs

Productivity Hack

Automating Your Social Media Posts with the Twitter/X API

Context switching is the enemy of deep work. Learn how to write a week's worth of content in 30 minutes and let a Python script handle the posting.

5 hrs/wk Time Saved
100% Consistency

Maintaining a personal brand is essential for modern developers, but logging into Twitter/X every few hours disrupts your flow state. The solution isn't to post less; it's to post programmatically.

In this guide, we will build a simple "Bot" that reads tweets from a spreadsheet and posts them on a schedule.

⚠️ Note on Pricing: X (Twitter) has changed their API tiers. The "Free" tier creates write-only bots (posting tweets), which is perfect for this tutorial. However, reading tweets requires a paid Basic tier ($100/mo).

Step 1: Get Your Credentials

To interact with X programmatically, you need to register as a developer.

  1. Go to the Developer Portal.
  2. Create a new "Project" and "App".
  3. Generate your API Key, API Secret, Access Token, and Access Token Secret.
  4. Crucial: Ensure your App Permissions are set to "Read and Write".

Step 2: The Tech Stack

We will use Python and tweepy, the standard library for accessing the X API.

# Install dependencies
pip install tweepy pandas

Step 3: The Script

Create a file named bot.py. This script authenticates with X using OAuth 1.0a (required for posting tweets on the free tier).

import tweepy
import os

# 1. Configuration (Use Environment Variables in production)
API_KEY = "YOUR_API_KEY"
API_SECRET = "YOUR_API_SECRET"
ACCESS_TOKEN = "YOUR_ACCESS_TOKEN"
ACCESS_SECRET = "YOUR_ACCESS_SECRET"

# 2. Authenticate (Client v2)
client = tweepy.Client(
    consumer_key=API_KEY,
    consumer_secret=API_SECRET,
    access_token=ACCESS_TOKEN,
    access_token_secret=ACCESS_SECRET
)

# 3. The Posting Function
def post_tweet(text):
    try:
        response = client.create_tweet(text=text)
        print(f"✅ Tweet Posted! ID: {response.data['id']}")
    except Exception as e:
        print(f"❌ Error: {e}")

# 4. Execute
post_tweet("Hello World! This tweet was posted via the API. #Python")

Step 4: The "Batching" Workflow

Coding the bot is easy. The productivity hack is in the workflow.

Instead of thinking of a tweet every day, spend Sunday evening filling out a tweets.csv file:

id,text,scheduled_time,posted
1,"Just shipped a new feature! #coding",2025-10-25 09:00,false
2,"Python tip: Use enumerate() instead of range(len())",2025-10-26 09:00,false

Then, update your script to read this CSV, check the time, post the tweet, and mark it as posted=true. Set this script to run every hour using a CRON job on a $5 DigitalOcean droplet (or even a free GitHub Action).

Why this beats Buffer/Hypefury

Tools like Buffer are great, but building your own solution gives you: