AuthorPost

This Web API is designed for companies or private developers looking to implement an app to be used by the public. This is more indepth than ACB (which brings most of web API functionality to the Custom Behaviors in Portal without having to write code) and many things will be handled by the developer such as consuming OAuth2 to obtain an access token from a Client ID and Client Secret. We provide a way to use POSTMAN to get a token but your code is supposed to do this. Please do not send a support ticket asking to use OAuth2.

This guide will cover the basics of an HTTP request and how to interface your Ubi connected to Portal, how to build a request, how to send results to a callback url and how to subscribe to API updates in real-time. This guide assumes you are a developer familiar with using APIs.

How does the Ubi work?The Ubi runs Android and has a persistent connection to the Internet. It hosts a supervisor app that is able to detect a local trigger word and then perform a command based on whatever is spoken after the trigger. The Ubi runs a set of local natural language understanding algorithms to decide the intent of the user. Based on its understanding of the command, the Ubi will fetch information from various Internet services and present this information back to the user through speech.

A major difference between the Ubi and other devices running Android is its ability to accept and adjust settings remotely as well as transmit setting information back to a server. This includes its natural language understanding algorithms.

Settings and status of the Ubi can be accessed through the Ubi Portal. This portal has permissions to receive and push down data to individual Ubis. From the portal, a user can access sensor information on the Ubi, create alarms, add contact information, and setup custom commands. Users can also configure integration with home automation devices and outside services.

How does the Ubi API work?The Ubi API allows for outside services to access data on the Ubi Portal. The Ubi Portal acts as an intermediary between these outside services and individual Ubis.

Individual Ubis constantly pass sensor data to the Ubi Portal. With proper permission, an outside service can access the sensor data that is stored on the Ubi Portal. Likewise, if an outside service wants to push a message or command to the Ubi, the Ubi Portal will receive the request and decide how to appropriately pass down the request to the individual Ubis.

The current version of the Ubi API responds to GET and POST requests from outside services. Pushing requests from the Ubi to outside services would involve setting up “custom behaviors” in the Ubi Portal. Currently, the custom behaviours are defined only by the user, not the developer, and there is no API access for it so you would use ACB.

Custom behaviors allow the Ubi to trigger different events based on either the Ubi sensing a change in the environment or if someone says a particular command to the Ubi. These are programmed on the Ubi Portal and run from there.

Access to outside APIs is granted through tokens. Tokens are requested through the Ubi Portal and are created separately for each user account. When the user authorizes a web app to access her account, a token is generated for that web app to access the Ubis under that account. The web app will only have access to the Ubis under that account that are authorized by the user. Tokens can allow access to sensor data, receipt of speech requests, or both. It’s recommended that you request a separate token for each application that you’re creating.

What can I do with the Ubi Web API?With the current version of the Ubi API, you can send a speech command to Ubi, you can push text to individual Ubis that will then be spoken out of the Ubi. The text that can be pushed to the Ubi to be spoken is limited to 200 characters. To end sentences smoothly, the Ubi will stop speaking at the first period detected after 150 characters. Speech requests pushed to the Ubi can take up to ten seconds to be the spoken.

You can also give your app the ability to sense what’s happening at the Ubi’s location.

Example:You can find out what temperature the basement Ubi is sensing.You can get light measurements in the kitchen.You can get the Ubi in the bedroom to tell you to wake up.

Setting Up API Access and AuthenticationUbi API authentication is based on OAuth2. The purpose of the OAuth2 is to allow a developer’s app hosted on a separate web server to gain an access token that will allow the app to access an Ubi user’s information.

The developer registers their app on the Ubi Portal and is provided with a Secret and an App ID. This information is embedded into their web app.

When an Ubi user wants to use this web app, the user is sent from the web app to the Ubi Portal where the user gives permission to the web app to access specific information about the user’s Ubis.

The user is redirected back to the web app’s site, along with an Authorization Code.

The web app passes the Authorization Code along with its Secret back to the Ubi Portal and is return a security token.

The web app can now make requests to the Ubi Portal on behalf of the user by bunding the security token with API requests.

Getting Started

The first step to setting up API access is to login to Portal and request a Client ID and Secret to be used by your app for OAuth2 access (you’ll need to own an Ubi to fully test your application as no simulator is currently available). The return types will be JSON objects.

It’s time to write some code. Within your code, you must first send a request to the Ubi Portal for authorization. If authorization is granted, the Ubi Portal will respond back with Get Parameter "code". Taking this code, the app will then need to send request in the background with Client ID and Client Secret. After the web app goes through a successful Oauth2 authorization process, a token will be granted. A token will look like this:

accessToken=ea1113f2-b098-431b-ab36-3cb6465e6f95

When the user deletes web apps in their Ubi Portal account, its token will also be revoked. If the user installs the web app again, a new token will be issued to the web app.

To obtain an access token to use in your requests install POSTMAN Chrome Extension. This is a quick way to test endpoint access without having to write the code. Launch POSTMAN and click the OAuth 2.0 tab. Now click "Get access token". Use these settings:

https://portal.theubi.com/v2/ubi/[ubiID]/speak?access_token=[access_token]&phrase=[phrase]&conversation=[true or false]

A few notes about [phrase]:

Keep your phrase under 200 characters

The Ubi will cut off sentences at the first period after 150 characters

Avoid making more than one request per Ubi per 15 seconds

It may take up to 5 seconds for the Ubi to receive the request and start speaking

If the “conversation” parameter is set to true then the Ubi will enable speech recognition after announcing the “phrase”. The result of the STT will be sent to the API callback URL if the API has subscribed a callback.

To adds spaces to URL use %20 for example phrase can be "Testing%20the%20API

Subscribing to API updates in real-timeThe Ubi API has a subscription feature that enables apps to subscribe to changes in certain pieces of data. When a change occurs, an HTTP 'POST' request will be sent to a callback URL belonging to that app. For the apps that will be authorized by the Ubi user, the user utterance will be submitted to those apps through webhooks provided that the utterance matches the phrase to which the app is subscribed. This makes apps more efficient, as they know exactly when a change has happened, and don't need to rely on continuous or even periodic API pulling requests when changes aren't happening.