title: How to determine how many more requests my app can make before it hits the Twitter API's rate limit?
body: <p>My application habitually makes a request to the Twitter API for a user's timeline (the user's tweets).</p>
  
  <p>As an aside, I'm using the twitter gem and configured a Twitter client in order to enable my app to make the request:</p>
  
  <pre><code>client = Twitter::REST::Client.new do |config|
    config.consumer_key        = ENV["TWITTER_CONSUMER_KEY"]
    config.consumer_secret     = ENV["TWITTER_CONSUMER_SECRET"]
    config.access_token        = ENV["TWITTER_ACCESS_TOKEN"]
    config.access_token_secret = ENV["TWITTER_ACCESS_TOKEN_SECRET"]
  end
  </code></pre>
  
  <p><a href="https://developer.twitter.com/en/docs/tweets/timelines/api-reference/get-statuses-user_timeline.html" rel="nofollow noreferrer">The endpoint</a> that I'm making a request to -- <code>https://api.twitter.com/1.1/statuses/user_timeline.json</code> -- has a rate limit of 900 requests every 15 minutes.</p>
  
  <p>If I understand it correctly, there are 2 common ways to determine if my app has hit its rate limit -- but neither is what I need:</p>
  
  <p><strong>First Approach:</strong></p>
  
  <p>This is a function I wrote that attempts to make the request for a user's tweets, and if the app has it the rate limit, it'll raise an exception.</p>
  
  <pre><code>def get_tweets_from_TwitterAPI(twitter_handle)
    tweets = []
    begin
      tweets = CLIENT.user_timeline(twitter_handle)
    rescue Twitter::Error::TooManyRequests =&gt; error
      raise error
    end
    return tweets
  end
  </code></pre>
  
  <p>The problem with that approach is I'd like to find out how many more requests my app <strong>can safely make before I even make the request to the Twitter API.</strong> I fear that this approach would have my app pinging the Twitter API long after it hit the rate limit, and that would open my app up to punitive actions by Twitter (like my app being blacklisted for instance.)</p>
  
  <p><strong>Second Approach</strong>
  The second approach is to make a request to <a href="https://developer.twitter.com/en/docs/basics/rate-limiting.html" rel="nofollow noreferrer">this Twitter endpoint</a> --<code>https://api.twitter.com/1.1/application/rate_limit_status.json</code> -- that sends data back about where the application's status for a given rate limit.</p>
  
  <p>But again, this endpoint also has its own rate limit (180 requests every 15 minutes) -- which isn't very high. My app would blow past that limit. In an ideal world, I would like to determine my app's current rate-limit status before I make a request to the API at all. And so:</p>
  
  <pre><code>def start_tweets_fetch
    number_of_requests_in_last_15 minutes = WHERE DO I GET THIS NUMBER FROM??
    if number_of_requests_in_last_15 minutes &lt;= 900
      get_tweets_from_TwitterAPI(twitter_handle)
    end
  end
  </code></pre>
  
  <p>I'm imagining I would have to increment some number that I've persisted to  my database to keep track of requests to the API. Or is there an easier way?</p>