From DreamFactory
Jump to: navigation, search

The Twitter API requires OAuth authentication, but because we're going to interact with the API programmatically rather than on behalf of an authenticated user we'll need to take advantage of a third-party library to handle the OAuth logic. For PHP we recommend TwitterOAuth (, created and maintained by Abraham Williams.

To install the package, navigate to your DreamFactory root directory and execute this command:

$ composer require abraham/twitteroauth --no-dev

With that done, it's time to configure the service. Login to your DreamFactory instance as an administrator and follow these steps:

  1. Select Services > Create > Script
  2. Choose one of the four supported scripting engines (Node.js, PHP, Python, V8)
  3. On the Info tab, enter a name, label, and description. Remember, the name will constitute part of the service URL, so be sure to enter something meaningful such as twitter.
  4. Click the Config tab.

Within this tab you'll be presented with several options for configuring your script. For the sake of simplicity we'll leave the options untouched and just embed the script directly into the textarea field.

$consumerKey    = YOUR_CONSUMER_KEY;
$consumerSecret = YOUR_CONSUMER_SECRET;
$oauthToken     = YOUR_OAUTH_TOKEN; 
$oauthSecret    = YOUR_OAUTH_SECRET;

$connection = new \Abraham\TwitterOAuth\TwitterOAuth($consumerKey, $consumerSecret, $oauthToken, $oauthSecret);

if ($event['request']['method'] == "GET") {

    $response = $connection->get("statuses/home_timeline", ["count" => 10, "exclude_replies" => true]);

} elseif ($event['request']['method'] == "POST") {

    $response = $connection->post("statuses/update", ["status" => "hello world"]);


return json_encode(["response" => $response]);

With the service saved, you'll want to create the usual role and companion API key to limit access. Once done, use an HTTP client such as Insomnia to test the service. Send an GET request to /api/v2/twitter to retrieve the tweets made by the associated API account. Send a POST request to the same URL to post hello world to the account. One easy modification of the latter behavior would be to send a JSON body along with the POST request containing the desired tweet text!