How to use premium API for the first time (beginner)?


I am working on my Master’s thesis, in which I am trying to do predictive analysis from historical Twitter data. My application for the premium beta sandbox API has been accepted, so now I am trying to get started. However, I am wondering what is the best way to connect to the API? Currently, I am using R in Rstudio, however I am not finding all the search functionality as is available on the Twitter advanced search. Basically, this is what I need to do:

  1. Collect tweets between certain dates which match a specific hashtag
  2. Exclude tweets which contain certain words

So for example, on the advanced search operators it could look something like "#iPhoneX -win -giveaway -buy -sell since:2017-10-01 until:2017-12-01 lang:en. "

Also, I have made several requests today in R, however on my dev dashboard, it shows that no requests have been made. Does this mean that my premium account is not connected to the app keys and tokens I’ve been using?

Any advice on where/how to get started using the API would be extremely helpful, as most of the information I find online is aimed at more experienced developers. Thanks in advance!



Premium API endpoint

As far as I know, the two main R libraries that connect to the Twitter API do not currently support the premium search APIs (actually, I don’t know of any R libraries that can access premium search at this time - Twitter does not provide any).

The only suggestion I have would be to use our Python search-tweets library as an alternative.

Also, note that the search rules for premium do not work in the same way as those for standard. See the reference documentation here.


Hey Ross! This is unrelated, but how long did it take for twitter to approve your premium API request? I am conducting similar work and I’m anxious to get my hands on my data asap but I havent been approved yet.

Also - good luck with your research!


I applied on a Monday and was approved on a Friday. Good luck!


How do I connect my Premium account to one of the apps I’ve created? I tried to link them on the dev environments, but it didn’t seem to work. When I access the API, it still acts as if it was a basic version and not the premium. Any tips?

Thanks again!


If you see the app’s name linked in the Dashboard ( then it should be set. What errors are you seeing that lead you to believe there’s an issue?

There are code samples on this page that may help you.


I do not have much experience with Python, so this is all a bit confusing for me since most of the available documentation is geared for experienced developers, and not beginners. I have viewed the documentation and all the links you’ve sent me, but it still doesn’t seem to work. I sat today for about 3 hours with a friend who does Python, and we still can’t get it to work. Is there not simply one example code available, where I can copy/paste my unique credentials and then perform the desired query?


You can try using one of the simple command-line examples, for instance:

curl --http1.1 --request POST  
--header 'authorization: Bearer YOURTOKEN'  --header 'content-type: application/json' 
--data '{"query": "andypiper has:images","maxResults": 20}'

Your token can be generated using this script if needed.


Hi, i am in the same situation as Ross… Providing some full working examples could really help to work with R or other products.


Hi Ross,

# minimum example for R


appname <- ",,,"
key <- "..."
secret <- "..."

# base64 encoding
kands <- paste(key, secret, sep=":")
base64kands <- base64encode(charToRaw(kands))
base64kandsb <- paste("Basic", base64kands, sep=" ")

# request bearer token
resToken <- POST(url = "",
                add_headers("Authorization" = base64kandsb, "Content-Type" = "application/x-www-form-urlencoded;charset=UTF-8"),
                body = "grant_type=client_credentials")

# get bearer token
bearer <- content(res)
bearerToken <- bearer[["access_token"]]
bearerTokenb <- paste("Bearer", bearerToken, sep=" ")

# get example from full archive
resTweets <- POST(url = "",
            add_headers("authorization" = bearerTokenb, "content-Type" = "application/json"),
            body = "{\"query\": \"andypiper has:images\",\"maxResults\": 20}")

tweets <- content(resTweets)

Premium APIs with R
I can't get old tweets having "Premium - Search Tweets: Full-archive endpoint"
Premium API endpoint
Premium API with R?

@andypiper can you please explain how this even possible? 5 days? People are waiting for months but some however are getting just in one week!


I’m sorry, but we are unable to comment on individual requests for access to the APIs. We appreciate your patience.


As applications for the beta come in, we are doing our best to quickly process those that have a well defined use case and explain how they plan to use the APIs. Those that don’t match those criteria often require email correspondence, to understand what it is you are actually trying to do with the API, which takes us time.

Hang in there - I know this is not ideal and we are working to expedite the process.


Let me know if i can help to create some examples for R like above. :slight_smile: Glad to help


That’s a great offer @hupseb thank you - we don’t have deep experience in R on the @TwitterDev team so community contributions are welcomed. If you’d like to work with the existing library author for rtweet to offer premium search and/or account activity support then I’m sure they would welcome it; if you put any of your own code on Github to help others, I’ll be sure to make sure that the community is aware!


That would be wonderful, @hupseb!


Hi Andy,

Another quick question: is there a way to collect a sample when searching the twitter archives? For example, if I want to look at tweets from one week, is there a way to take a random sample from that time period? Or will the API just return tweets in chronological order?



There is a sample operator - but it is only available in the historical and realtime streaming PowerTrack API, which is an enterprise product. There’s no way to achieve this via the premium search APIs.


I am also looking at tweets for my M.A. thesis using R. Thank you for posting this.
Would you mind explaining what is going to in your “get example from full archive”. I set the url to and in the body instead of andy piper I put my application name. Should I be application name or the name of the person whose tweets I am trying to retrieve? Either way I end up getting a list of length 3 which I don’t understand what I am supposed to do with.

NOTE: I am trying to get tweets of specific people during specific period of time.


Hi all.
Browsing through all Premium API topics, I could not find any suitable title other than this. So… may I ask my newbie question? I already worked standard search API over a year or so with one appplication registered at . Upon my approval as premium API user, tells me to “Create an app.” Should I create another app for premium API or can I user already registered app (and its keys) for premium API use? I guess I need to create a new one, but to be clear, would someone kindly tell me? :smile:

Edit: Can I use my application from seems to relate to my question, but it is not clear if a new app is needed or desirable, or it is not always necessary.