This is a quick hands-on introduction to bt.tn REST API usage. Check the bt.tn REST API reference for details.
To get started with the bt.tn REST API, you will need the cURL command line HTTP client. This is usually pre-installed
on Mac & Linux but needs to be installed separately on Windows. Test that
it’s set up by running curl
from the command line.
To try out the REST API:
curl https://api.bt.tn/2014-06/ID/feed
&npsb;
where ID is your bttn ID.You can see the full list of API endpoints available by browsing the bt.tn API Reference documentation.
Some endpoints require you to be authenticated using an API key. You can find them under the "Authenticated" header in the API Reference. To retrieve an API key you can use for development:
curl --header "X-Api-Key: API_KEY" https://api.bt.tn/2014-06/bttns
where API_KEY
is the key you just copied, to see the list of bttns the application has access to.By using the API key retrieved via my.bt.tn, you will be able to see only your own data. If you want to access data on behalf of other users, you will need to create a bt.tn application and use OAuth to request access to users’ data. At the moment, this functionality is provided only to our partners. If you would like to become a partner, please email info@bt.tn.
Below, you can find the source of a simple single page app using the bt.tn REST API to display the counter values of two bttns:
<!DOCTYPE>
<html>
<head>
<title>bt.tn Duel</title>
<script src="https://code.jquery.com/jquery-1.11.0.js"></script>
<script>
var BASE = "https://api.bt.tn/2014-06/", BTTNS = [1, 3];
$(function() {
(function update() {
$.get(BASE + "counter?id=" + encodeURIComponent(BTTNS.join()), function(data) {
$("body").html("");
$("body").append(data[0].counter);
for (var i = 1; i < data.length; i++)
$("body").append(" : " + data[i].counter);
setTimeout(update, 2000);
});
})();
})
</script>
<style>
html, body { height: 100%; }
html { display: table; margin: auto; }
body {
display: table-cell;
vertical-align: middle;
font: bold 20vw monospace;
}
</style>
</head>
<body>
</body>
</html>
To see it in action, copy paste the code above into a file with the extension ".html" on your computer and open it with your browser.
Here's a quick overview of the code:
script
tag includes jQueryscript
tag contains the app spefic code:
BASE
and IDs of BTTNS
- you can
update one of these to be the ID of your bttn, which we configured in the "Getting Started" section.update
functionupdate
function makes one HTTP GET request to fetch the counter value of both bttns - this is possible
from any domain thanks to CORS support
in the bt.tn REST APIsetTimeout
to have the update
function run again after 2 secondsstyle
tag is used to set the font and position of the body textTo create and release a standalone web app using the bt.tn REST API, you will need to complete the following steps:
That's it! Now whenever the user presses their bttn, you will receive a web hook HTTP request. Verify it using the random key, if available, and process as required by your application.
Note that users may disassociate their bttns from your application via my.bt.tn or by authorizing another app, so you may want to periodically check if your access token is still valid and your web hook URL hasn't been changed using the get web hook endpoint.