Twitter API v2 "TypeError: Load failed" while trying to post

Viewed 39

I'm trying to post a tweet and continually run into "TypeError: Load failed" without any explanation as to why the tweet will not post.

import { Client } from 'twitter-api-sdk';

const client = new Client(process.env.TWITTER_BEARER_TOKEN!);

interface PostTweetProps {
  tweetBody: string;
}

export async function PostTweet({ tweetBody }: PostTweetProps) {
  console.log(tweetBody), //successful logging 
    //breaks here 
    await client.tweets.createTweet({
        text: tweetBody,
      })
      .catch((err) => console.log(err));
}

My PostTweet function is sound, and the console is outputting the text that is passed from my index.ts so there is no issue in the frontend, just running into the error when it goes to post.

1 Answers

Definitely sounds like the error message could be more helpful there. If you are using a bearer token you're probably not going via the login / authentication flow to authorize a user account to Tweet. You'll need to look at the OAuth 2.0 with PKCE code path and request an API token with tweet.write permission to call the createTweet method.

Related