Creating a Twitter Collection via API

Example collection. Can also be viewed here:

Getting Started

Authenticating with Twitter

import json
from requests_oauthlib import OAuth1Session
api_key = 'your api key'
api_secret_key = 'your api secret key'
access_token = 'your access token'
access_token_secret = 'your access token secret'
# twitter oauth
twitter = OAuth1Session(api_key,

Creating a Collection

# create
url = ''
params_create = {
'name': 'cutest puppies EVER',
'timeline_order': 'tweet_reverse_chron'
r =, data=params_create)
# print the full returned json to ensure success
# print the response to get the id of the timeline created. This is needed for adding/removing tweets

Adding Tweets to a Collection

tweet_ids = [1148286877616803840, 1180845365542752256, 1180123991165607937, 1158830129080098823]# add
url = ''
for tweet_id in tweet_ids:
params_add = {
'tweet_id': tweet_id,
'id': 'custom-1180945428222595074'
r =, data=params_add)

Bulk Adding Tweets to a Collection

# bulk add
url = ''
# split into batches of 100 for the uploads
n = 100
batches = [tweet_ids[i:i + n] for i in range(0, len(tweet_ids), n)]
print (len(batches))
for batch in batches:
params_add = {
"id": "custom-1180945428222595074",
"changes": []
for tweet_id in batch:
sub_params_add = {
"tweet_id": str(tweet_id),
"op": "add"

r =, data=json.dumps(params_add))

Other useful collections endpoints





Data Scientist at Twitter | Previously Grad Student at MIT Media Lab

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Why Elon Musk Challenged Putin to a Fight

Message Queues

How to make Git “forget” about a file that was committed but is now in .gitignore?

How I Ended up Having 25,000 Math Problems for Free — Understanding Web Scraping

TeamCode x Merico, dive into an efficient cloud development era

The “Stack” Data Structure and its Python Implementation

All 49 Keywords in Java part 2

Object Oriented Programming(OOP) Series: Access modifiers

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Lauren Fratamico

Lauren Fratamico

Data Scientist at Twitter | Previously Grad Student at MIT Media Lab

More from Medium

Automating Article Schema Implementation in Hubspot

Hubspot logo on a piece of card stock

Cleaning Data with Node.js, Sending SMS Twilio

Build your Own Google meet launcher (chrome extension) less than 15minutes

Transferring data from MySQL to MsSQL DataBase