Search code examples
node.jstwitter

Can I post images/media using Twitter's API


I have been looking for information on this topic for the past few days. I would like to be able to post messages with both text and one image with my Twitter bot. I get an error message instead:

ApiResponseError: Request failed with code 403 - You currently have access to a subset and limited v1.1 endpoints (e.g. media post, 0auth) only. If you need access to this endpoint, you may need a different access level. You can learn more here: https://developer.twitter.com/en/portal/product (Twitter code 453)

So on the one hand, the message I get seems to say I have to pay since I don't have the right access level, but on the other hand the official documentation seems to say that the free tier allows the upload of images (https://developer.x.com/en/docs/x-api).

Rate-limited access to v2 post posting and media upload endpoints

So are there people out there that have been able to post images with the free tier? My Twitter bot is written in NodeJS.


Solution

  • I'm leaving an answer here instead of in the comments as I believe this is your issue, but the best I can do is guess since I don't have access to X's apis at the moment to confirm. Given your error says (bolded statements are my own):

    ApiResponseError: Request failed with code 403 - You currently have access to a subset and limited v1.1 endpoints (e.g. media post, 0auth) only. If you need access to this endpoint, you may need a different access level. You can learn more here: https://developer.twitter.com/en/portal/product (Twitter code 453)

    Notice how it says you have access to media post from the limited subset of v1.1 endpoints (likely due to you being on the free tier). So I also believe when it says:

    Rate-limited access to v2 post posting and media upload endpoints

    The post posting must be from the v2 API, but since I can't find media upload anywhere else and due to the above error message I believe that the media upload endpoint comes from the v1.1 API, again I'm simply giving my best guess.

    So I'm assuming you are using the v1.1 api currently to try and create the tweet and that's what is causing the problem? You should instead be using the media upload from API v1.1 (as I don't see a v2 of it) then grab the media id from the response of that and then use that media id like so using the v2 tweet post api as outlined in their examples repo:

    const token = {
        key: "<<YOUR OAUTH TOKEN HERE>>",
        secret: "<<YOUR OAUTH TOKEN SECRET HERE>>"
    };
    
    const authHeader = oauth.toHeader(oauth.authorize({
        url: "https://api.x.com/2/tweets",
        method: 'POST'
    }, token));
    
    const req = await got.post("https://api.x.com/2/tweets", {
        json: { tweet: "<<YOUR TWEET TEXT HERE>>", media: {"media_ids": ["<<YOUR MEDIA ID HERE>>"]} },
        responseType: 'json',
        headers: {
          Authorization: authHeader["Authorization"],
          'user-agent': "v2CreateTweetJS",
          'content-type': "application/json",
          'accept': "application/json"
        }
    });
    console.log(req.body);
    

    Notice that the above will only do the second half of what you need to do. You will need to figure out the first half from the docs I also linked above for the media upload v1.1 api.