The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Build a Twitter-to-Notion sync script

Here’s a Python script that syncs tweets from a Twitter account to a Notion database using the Twitter API and Notion API. It captures tweets from a given username and logs them in a Notion database with fields like tweet text, date, and URL.

Requirements

  • Twitter API credentials (v2 with bearer token)

  • Notion integration token

  • Notion database ID

  • Python packages: requests, datetime


Python Script: Twitter to Notion Sync

python
import requests from datetime import datetime # === CONFIGURATION === TWITTER_BEARER_TOKEN = 'YOUR_TWITTER_BEARER_TOKEN' NOTION_TOKEN = 'YOUR_NOTION_INTEGRATION_TOKEN' NOTION_DATABASE_ID = 'YOUR_NOTION_DATABASE_ID' TWITTER_USERNAME = 'twitter_username_to_track' # === HEADERS === twitter_headers = { "Authorization": f"Bearer {TWITTER_BEARER_TOKEN}" } notion_headers = { "Authorization": f"Bearer {NOTION_TOKEN}", "Content-Type": "application/json", "Notion-Version": "2022-06-28" } # === STEP 1: GET USER ID FROM TWITTER USERNAME === def get_twitter_user_id(username): url = f"https://api.twitter.com/2/users/by/username/{username}" response = requests.get(url, headers=twitter_headers) data = response.json() return data['data']['id'] if 'data' in data else None # === STEP 2: GET RECENT TWEETS FROM USER === def get_recent_tweets(user_id, max_results=5): url = f"https://api.twitter.com/2/users/{user_id}/tweets?max_results={max_results}&tweet.fields=created_at" response = requests.get(url, headers=twitter_headers) tweets = response.json().get("data", []) return tweets # === STEP 3: CREATE A NEW PAGE IN NOTION === def send_to_notion(tweet): create_url = "https://api.notion.com/v1/pages" tweet_url = f"https://twitter.com/{TWITTER_USERNAME}/status/{tweet['id']}" created_time = datetime.strptime(tweet['created_at'], "%Y-%m-%dT%H:%M:%S.%fZ") data = { "parent": { "database_id": NOTION_DATABASE_ID }, "properties": { "Tweet Text": { "title": [ { "text": { "content": tweet['text'][:100] } } ] }, "Date": { "date": { "start": created_time.isoformat() } }, "URL": { "url": tweet_url } } } response = requests.post(create_url, headers=notion_headers, json=data) if response.status_code != 200: print("Error sending to Notion:", response.text) # === MAIN FUNCTION === def sync_tweets_to_notion(): user_id = get_twitter_user_id(TWITTER_USERNAME) if not user_id: print("Twitter user not found.") return tweets = get_recent_tweets(user_id) for tweet in tweets: send_to_notion(tweet) print(f"Synced {len(tweets)} tweets to Notion.") # === RUN === if __name__ == "__main__": sync_tweets_to_notion()

Notion Database Setup

Your Notion database should have the following fields:

  • Tweet Text (Title)

  • Date (Date)

  • URL (URL)

Make sure your Notion integration has access to the database.

Would you like to schedule this to run daily using a cron job or apscheduler?

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About