Documentation

Hello World

Let's get started by creating a "Hello World" service that runs on your local machine and communicates with IFTTT.

Sign in or sign up before following this tutorial.

This tutorial and code sample will help get you up and running on the IFTTT Platform quickly and show you how to verify that your service is working correctly using the IFTTT endpoint tests. If you'd prefer, feel free to skim over this section or dive right into the Service API Reference!

Download the Rails app

To get started, copy the following to a file named "hello_world.rb" in your home directory:

IFTTT_SERVICE_KEY = "REPLACE_ME"

require "bundler/inline"

gemfile do
 source "https://rubygems.org"
 gem "rails", "~> 5.0"
end

require "action_controller/railtie"
require "active_model/railtie"

class HelloWorld < Rails::Application
  routes.append do
    root to: "hello#world"

    get  "/ifttt/v1/status", to: "ifttt#status"
    post "/ifttt/v1/test/setup", to: "ifttt#setup"

    post "/ifttt/v1/triggers/new_thing_created", to: "ifttt#new_thing_created"
    post "/ifttt/v1/actions/create_new_thing", to: "ifttt#create_new_thing"
  end

  config.cache_store = :memory_store
  config.eager_load = false
  config.logger = Logger.new(STDOUT)
  config.secret_key_base = SecureRandom.hex(30)
end

class Thing
  include ActiveModel::Model
  attr_accessor :created_at

  def self.all
    Rails.cache.fetch("things") do
      [
        Thing.new(created_at: Time.parse("Jan 1")),
        Thing.new(created_at: Time.parse("Jan 2")),
        Thing.new(created_at: Time.parse("Jan 3")),
      ]
    end
  end

  def self.create
    Thing.new.tap do |new_thing|
      new_thing.created_at = all.last.created_at + 1.day
      Rails.cache.write("things", all.push(new_thing))
    end
  end

  def id
    created_at.to_i
  end

  def to_json
    {
      created_at: created_at.to_json,
      meta: { id: id, timestamp: created_at.to_i }
    }
  end

  def to_limited_json
    { id: id }
  end
end

class HelloController < ActionController::Base
  def world
    render plain: "Hello, World!" + "\n" + Thing.all.map(&:to_json).join("\n")
  end
end

class IftttController < ActionController::Base
  before_action :return_errors_unless_valid_service_key
  before_action :return_errors_unless_valid_action_fields, only: :create_new_thing

  def status
    head :ok
  end

  def setup
    data = {
      samples: {
        actionRecordSkipping: {
          create_new_thing: { invalid: "true" }
        }
      }
    }

    render plain: { data: data }.to_json
  end

  def new_thing_created
    data = Thing.all.sort_by(&:created_at).reverse.map(&:to_json).first(params[:limit] || 50)
    render plain: { data: data }.to_json
  end

  def create_new_thing
    data = [ Thing.create.to_limited_json ]
    render plain: { data: data }.to_json
  end

  private
    def return_errors_unless_valid_service_key
      unless request.headers["HTTP_IFTTT_SERVICE_KEY"] == IFTTT_SERVICE_KEY
        return render plain: { errors: [ { message: "401" } ] }.to_json, status: 401
      end
    end

    def return_errors_unless_valid_action_fields
      if params[:actionFields] && params[:actionFields][:invalid] == "true"
        return render plain: { errors: [ { status: "SKIP", message: "400" } ] }.to_json, status: 400
      end
    end
end

HelloWorld.initialize!
Rack::Server.start app: HelloWorld, Port: 3000

Install Ruby version 2.2.2 or greater

In order to run the Rails app you'll need version 2.2.2 or greater of the Ruby programming language installed. You can see which version of Ruby you have installed by running:

ruby -v

If you need to install a new version of Ruby, you can follow the installation instructions on the Ruby website.

Run the Rails app

You'll need to open your Terminal and install the bundler Ruby gem:

gem install bundler

And then you'll be able to run the application:

ruby hello_world.rb

Now, you can view the Rails app running at http://localhost:3000/ in your browser. You should see a "Hello, World!" message.

You'll need to keep your Terminal window open so the application keeps running.

Install ngrok

Since the application is running on your local machine, you'll need to install something to forward IFTTT's request to your local development environment. We recommend ngrok for this because it's easy to install and free for our simple use case.

Open a new Terminal window and follow the ngrok installation instructions. Once you have ngrok installed, you can expose the app running on your local machine to the internet. Just tell ngrok what port your web server is listening on. In this case, it should be port 3000:

ngrok http 3000

You'll see some output which will include "Forwarding" URLs for http and https. These are the URLs where your application is accessible on the internet.

We'll be using the secure https URL, which will look something like this: https://130fd810.ngrok.io (where the "130fd810" will be some other random identifier.)

Visit the forwarding URL in your browser and you should see that "Hello, World!" message again.

Give IFTTT your API URL

Now that your app is up and running on the internet, you can give IFTTT the ngrok URL. Visit the Partner Platform and select the "Hello World" service that we pre-configured in your account. Then, click the "API" tab and put the ngrok Forwarding URL into the "IFTTT API URL" field and click Save.

Add your service key to your Rails app

Your service key is a secret key that is shared between your app and IFTTT. You'll need to copy and paste this key into your Rails app so IFTTT can correctly identify your service.

From the "API" page, copy the service key. Then, open your local Rails app and set "IFTTT_SERVICE_KEY" to that value. You'll need to restart the Rails app for the change to take effect.

Run the endpoint tests

IFTTT provides endpoint tests that you can use to verify that your service is configured and running correctly. From the "API" page, click the "Endpoint tests" link on the sidebar. Then, click "Begin test" in the upper right and wait a moment for the test results to come in.

If all goes well, you'll see that you have a valid service configured in the Partner Platform and running on your local machine!

Build your own service

Now you can review the service configuration on the Partner Platform and compare it to the Rails app you have running locally, make changes, and re-run the test suite to see what happens.

Try experimenting by adding new actions and triggers until you get the hang of things, and then get started building your own service using the full Service API Reference docs!

Next:

View Service API Reference