DEV Community

Artur Piszek
Artur Piszek

Posted on • Originally published at piszek.com on

Your own Roam Research Quicknote REST API with Firebase

I love Roam Research (have written a lot about it), but the biggest struggle for me is the lack of the proper REST API. I have tried faking it with Puppeteer, but I needed something quick for capturing the notes on the go.

Now I can capture the notes from a variety of apps through IFTTT, or iOS shortcuts (including Siri on my watch), and I have been doing so for more than a year. As automation is important for me, this script has helped include roam into my workflows.

Hey Siri, make a note in Roam

Cool, huh?

How does it work?

  1. iOS shortcut or IFTTT makes a POST request to Firebase DB
  2. Every 10 minutes Roam checks that DB, fetches new notes and deletes them in DB.
  3. Any new note shows up in my current daily page, with “#Inbox” tag

From what I have been able to gather, this is similar to what phonetonote is doing, without the need for you to manage your own DB.

What do you need to make this work for you?

You need 3 things: A Firebase project, a custom script in your Roam graph, and iOS shortcuts to start getting your data in the graph.

Firebase

Firebase is a Google Cloud offering that lets you store JSON in the cloud. With the usage you will be generating, you will be able to operate the project for free.

  1. You need to set up a Firebase Realtime Database project
  2. Once you set it up, you need to generate a database secret ( Project Settings -> Service Accounts -> Database Secrets)

Roam custom plugin

This custom Roam plugin will pull data from this database – you can customize path/to/quicknotes to whatever you like.

Read here on how to install custom Roam plugins

const fireBaseUrl = 'https://YOURPROJECT.firebaseio.com/path/to/quicknotes';
const fireBaseToken = '';

function zeroPad( data ) {
    if ( data < 10 ) {
        return '0' + data;
    }
    return '' + data;
}

function getTodayUid() {
    const d = new Date();
    return zeroPad( d.getMonth() + 1 ) + "-" + zeroPad( d.getDate() ) + "-" + d.getFullYear();
}
function importFromFirebase() {
    window.fetch( fireBaseUrl + '.json?auth=' + fireBaseToken )
        .then( response => response.json() )
        .then( data => {
            console.log( 'Firebase Import:', data );
            if ( ! data ) {
                return;
            }
            Object.keys( data ).forEach( key => {
                const entry = data[key];
                if ( ! entry.string ) {
                    console.warn( 'The payload needs at least a string', entry );
                    return;
                }
                entry.string += ' #Inbox';
                window.roamAlphaAPI.createBlock( {
                    "location": {"parent-uid": getTodayUid(), "order": 0 }, 
                    "block": entry
                } );
                window.fetch( fireBaseUrl + '/' + key + '.json?auth=' + fireBaseToken, { method: 'DELETE' } );
            } );
        } );

    window.setTimeout( importFromFirebase, 10 * 60 * 1000 ); // Check for more notes every 10 minutes.
}

window.setTimeout( importFromFirebase, 60 * 1000 ); // We run this a minute after Roam starts.
Enter fullscreen mode Exit fullscreen mode

Start POSTing your data

Now you need to start throwing your data into the database and see it in your Roam graph!

iOS shortcuts

Here is an iOS shortcut action that will save my data in this database. You can include it in your workflows (just remember to change Text to your token and YOURPROJECT to your project).

IFTTT

You can always include it in IFTTT to provide similar functionality, or make it save all your liked Youtube videos for example:

Enjoy!

Top comments (0)