1

I'm working with the twitter-api-v2 library in conjunction with the @twitter-api-v2/plugin-rate-limit plugin for Node.js. I'm facing two main issues:

  1. When I attempt to fetch the rate limit for the 'users/me' endpoint using the plugin, it returns undefined.
const rateLimitStatus = await rateLimitPlugin.v2.getRateLimit('users/me');
console.log('Rate Limit Status:', rateLimitStatus);
  1. When I try to post a tweet using twitterClient.v2.tweet(tweetText), even though I still have a large number of requests remaining (39999 out of 40000), the plugin indicates that I've hit the rate limit:
[2023.08.14-08.34.14.0642] Rate limit exceeded when replying to tweet. Limit: 40000 Remaining: 39999 Last reset: 14/08/2023, 08:49:14

This is confusing as I'd expect to hit the rate limit only when the remaining count reaches 0.

Here's a relevant snippet of my code:

import { TwitterApi } from "twitter-api-v2";
import { TwitterApiRateLimitPlugin } from '@twitter-api-v2/plugin-rate-limit';

const rateLimitPlugin = new TwitterApiRateLimitPlugin();

// ... (TwitterApi configuration)

export const postTweet = async (tweetText) => {
    try {
        const rateLimitStatus = await rateLimitPlugin.v2.getRateLimit('users/me');
        console.log('Rate Limit Status:', rateLimitStatus);
        if (rateLimitStatus?.remaining <= 40000) {
            // ... (wait logic)
        }
        
        const response = await twitterClient.v2.tweet(tweetText);
        // ... (Logging and handling the response)

    } catch (e) {
        if (e.code === 429) {
            // ... (Handling rate limit exceeded error)
        } else {
            // ... (Handling other errors)
        }
    }
};

Any insights on these issues would be greatly appreciated. Specifically:

Is there an updated endpoint string to check rate limits instead of 'users/me'? How do I interpret the rate limit data and why am I receiving an alert even though I haven't exhausted my limits? Thanks in advance!

TheMightyLlama
  • 1,243
  • 1
  • 19
  • 51
  • _"Is there an updated endpoint string to check rate limits instead of 'users/me'?"_ - that was not "the" endpoint to check - because such a thing does not exist to begin with. _Each_ endpoints has limits, and for each of them you would be getting specific rate limit data returned, via the HTTP headers. – CBroe Aug 14 '23 at 09:27
  • How it _should_ currently work, is documented under https://developer.twitter.com/en/docs/twitter-api/rate-limits Whether anything has changed with that, compared to what the library you are using there does to try and determine if it hit a limit - couldn't tell you. – CBroe Aug 14 '23 at 09:28
  • @CBroe I agree that it's not possible to tell exactly what the library does. I've just retested with 'tweets' as well as '/2/tweets' (as indicated in the documentation) and both fail. I'll keep trying. Any ideas on the 40k rate limit number? the remaining counts down from 40000 and the rate limit is hit arbitrarily... – TheMightyLlama Aug 14 '23 at 10:22
  • Temporarily modifying the library code, so that you could check what header values it actually received (if node doesn't have a log for that somewhere outside the scope of this library already), would be my first step to start investigating here. Or making the corresponding request using a tool such as postman. – CBroe Aug 14 '23 at 10:25

0 Answers0