fetch twitter api in javascript

To fetch Twitter API in JavaScript, you can use the built-in fetch() method. Here's an example:

index.tsx
const endpoint = 'https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=twitter&count=10';

fetch(endpoint, {
  headers: {
    'Authorization': 'Bearer YOUR_BEARER_TOKEN'
  }
})
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error(error));
305 chars
11 lines

This code will fetch the 10 most recent tweets from the twitter account using the Twitter API, and then output the response data to the console.

Note that this example assumes you have obtained a bearer token from Twitter and included it in the request headers. Also, the fetch() method returns a Promise, so we can use the .then() and .catch() methods to handle the response and any errors that may occur.

gistlibby LogSnag