User blog:LadyAlys/Android's Tasker app and HabitRPG's API

This is a brief introduction to using HabitRPG's API with the excellent Android Tasker app. If you need more information, you are welcome to leave a comment, but please be aware that I cannot spend too much time on this so I might not be able to answer all your questions. For general Tasker queries (i.e. anything not related directly to the HabitRPG API), Tasker's in-app manual and its FAQs, user guides, and forums are likely to be helpful.

First, carefully read all the information provided at the top of HabitRPG's API documentation, and also make sure you understand any of the API commands you intend to use. Using a command incorrectly can permanently delete information from your HabitRPG account.

You use the API by sending HTTP requests. Authentication must be done with x-api-user and x-api-key headers which Tasker's standard "HTTP Post" action does not support. Instead, you can issue the HTTP GET or POST requests using a "JavaScriptlet" action from Tasker's "Script" action category.

Your Tasker task will be easier to maintain and re-use if you set up some standard Tasker variables with the data that you'll be using in the API calls. I suggest global variables like these (set them once, use them unchanged in many tasks):

%HabitrpgUrl = https://habitrpg.com %HabitrpgUserid = 4b8d60d1-...-6e01664d4247 %HabitrpgApIToken = 4c5c3d71-...-94-1c25e7a5ee

and then local variables for specific purposes in specific tasks - for example, to add a new task:

%http_post_url = %HabitrpgUrl/api/v2/user/tasks %http_post_data = {"text":"text for new item goes here","type":"todo","notes":"Extra Notes for new item go here"}

Once you have your Tasker variables set up, you can access them in your JavaScriptlet code using the same variable names but without the % sign. For example, compare the %http_post_url Tasker variable above with the http_post_url JavaScript variable in the code below - both are the same variable with the same value.

Create a "JavaScriptlet" action from Tasker's "Script" action category. Enter something like this into the action's "Code" field:

function add_item { var http = new XMLHttpRequest; http.open("POST",http_post_url,false); http.setRequestHeader("Content-Type", "application/json"); http.setRequestHeader("x-api-user", HabitrpgUserid); http.setRequestHeader("x-api-key", HabitrpgApIToken); http.send(http_post_data); return(http.responseText); } try { var result = add_item; } catch(e) { var error = e.message; }

That should be all of the essential Tasker code that you need, although you'll want to do some standard handling of the response/errors from the JavaScriptlet. If the HTTP request fails, your new task will not appear in HabitRPG, so you definitely want to give highly-visible feedback to the user when an error occurs!

NOTE: I copied these variables and code out of my Tasker tasks but then modified them slightly to simplify them and to remove personal information. I might have introduced errors while doing that. If my code doesn't work, be sure to post a comment here! Just check your own code first to make sure it isn't the source of the mistakes. :)