How to get Tweets from the Twitter API in 10 Minutes

No comments

In this tutorial I will teach you how to create a Code Endpoint to retrieve the latest tweets for a user given their twitter handle!

The Finished Product

Try it out below. It works! Or click here to go to the endpoint page.

Getting Started - Get your Twitter API Key!

Accessing the Twitter REST API requires you to register your application and obtain an API key to make API calls. To obtain your key, you will need to register your application here. After registering your app, click the Keys and Access Tokens tab and make note of four values (see below): Consumer Key (API Key), Consumer Secret (API Secret), Access Token, and Access Token Secret. If this is your first time at this dashboard, you probably do not have an access token created yet. Simply click the Generate Consumer Key Button to obtain an access token (see below).

Create Your Code Endpoint

A few weeks back we announced our latest and most powerful feature: Code Endpoints. These API endpoints execute a user-defined block of code when they are called to calculate and return a value. In our case, we will will be fetching the tweets from the Twitter API and returning them to the API user.

To create a new Code Endpoint in Apitite, navigate to your API dashboard and click Create Endpoint. Follow the steps to create a Node.js Code Endpoint and enter the necessary documentation  about your endpoint (see below) like the endpoint's name, description, and root URL, then click Create.

Defining a Parameter

The endpoint we are building will take in a Twitter handle as a parameter and use it to search the Twitter API for their latest Tweets. To define a parameter for a code endpoint, click the Add Parameter button and enter a parameter name, in my case I chose "handle"  (see below). Then enter a useful description.

To retrieve this parameter in your code, make a call to apitite.param('handle') with the parameter name you are trying to retrieve, in our case "handle".

Coding Your Code Endpoint

Now comes the fun part: entering the Node.js code that will retrieve the Tweets. Luckily there is an npm package that we can use to easily make calls to the Twitter API. Enter the following code below into the source code window. Then replace the Consumer Key (API Key), Consumer Secret (API Secret), Access Token, Access Token Secret, with the values from your registered Twitter app. Hit Save, and you're done!

// Import Twitter npm package
var Twitter = require('twitter');

// Twitter API Credentials
var client = new Twitter({
  consumer_secret: 'ENTER YOUR CONSUMER SECRET',
  access_token_key: 'ENTER YOUR ACCESS TOKEN',
  access_token_secret: 'ENTER YOUR ACCESS TOKEN SECRET'

// Get twitter handle from API user request
var handle = apitite.param('handle');

// Make call to Twitter API to get user's timeline
client.get('statuses/user_timeline', {screen_name: handle}, function(error, tweets, response){
  if (!error) {
    apitite.done(tweets); // return the tweets to the API user
  } else {
   console.error('An error occurred!'); //error handling

No comments :

Post a Comment