Adding tasks to Todoist from the command line
I’ve been using Todoist for close to ten years. I find it a great tool to track my task and get ideas out of my head. There is a browser implementation and apps which works on pretty much works on every OS. Since I spend a lot of time in my code editors such as Vim and VSCode. Both which have community plugins for Todoist. However I wanted something which will work the same in all my editors which I use. The following code goes through how I wrote a simple bash script to add items to my Todoist account.
The following code was written on a Linux machine but if you have a Mac it should work as its only a shell script. I’m not to sure about Windows machine. You also need to have a Todoist account.
Lets start by create our shell script create a file called addTaskToTodoist.sh. Inside the file place the following code below. The first line is saying it’s a shell script. The next line asks the users what task they wish to add to Todoist. Finally we store what the user entered as a variable.
#!/bin/sh echo "Please enter the name of the todo" read TODO
Now we going to visit todoist developer documentation. In this example we going to be using a Sync API. What we will be using is the quick add an item in the API – https://developer.todoist.com/sync/v8/#quick-add-an-item. As I find this similar to what the current Todoist website does its uses smart dates # projects and @for tags and labels. Copy the code curl command and paste the code into your script below the previous code.
curl https://api.todoist.com/sync/v8/quick/add \ -H "Authorization: Bearer 0123456789abcdef0123456789abcdef01234567" \ -d text='Task1 @Label1 #Project1 +ExampleUser'
We going to replace the text parameter with the value of our TODO we created earlier.
#!/bin/sh echo "Please enter the name of the todo" read TODO curl https://api.todoist.com/sync/v8/quick/add \ -H "Authorization: Bearer 0123456789abcdef0123456789abcdef01234567" \ -d text="$TODO" \
Now we need to get the Bearer token. Todoist makes this simple to do, so go to the following URL – https://todoist.com/app/settings/integrations and then go down to the part where it says API token copy to clipboard and that’s a bearer token.
Replace the current Bearer code with the one in your clipboard.
The script is now complete in the command line where the files is saved. Enter bash addTaskToTodoist.sh. Or whatever named the file and the script will run.
I want to be able to run the script by simply entering the word todo from the command line. To set this up we need to update our Bashrc file. Open up your .bashrc file this usually lives in your home directory eg /home/mymachine/.bashrc.
Add the code below. What it does is sets an alias of todo to the path of where we saved our file.
#Run my TODO bash script alias todo='/PATH-TO-SCRIPT/addTaskToTodoist.sh'
Finally back to the command type in the word todo and our script will run. We can now do that from whatever directory we are working from.
Link to the file – https://bitbucket.org/duaneblake/workspace/snippets/oB57xa