Accessing the Bear API via JavaScript and HTML

Hi everyone,

I’d like to do a request to the bear api, get certain files, and then render them to HTML, so that I can display my notes in column form and make changes to the display to help me visualize my projects, etc.

Is there a way to do this?

I tried doing it like this:

document.addEventListener("DOMContentLoaded", async () => {
  const response = await fetch(
    "bear://x-callback-url/create?title=My%20Note%20Title&text=First%20line&tags=home,home%2Fgroceries",
    {
      method: "GET",
      headers: {
        Authorization: "Bearer DA3FC-5-BE6BO6-C2E97", // Example header for authentication
        "Content-Type": "application/json",
      },
    }
  );

  console.log("response from bear", response);
});

but got the following errors in safari:

[Error] Cross origin requests are only supported for HTTP.
(anonymous function) (bear-app.js:2)
(anonymous function) (bear-app.js:1)

[Error] Fetch API cannot load bear://x-callback-url/create?title=My%20Note%20Title&text=First%20line&tags=home,home%2Fgroceries due to access control checks.
(anonymous function) (bear-app.js:2)
(anonymous function) (bear-app.js:1)

[Error] Unhandled Promise Rejection: TypeError: Load failed
(anonymous function) (bear-app.js:2)


If you cannot get the data from your notes via JavaScript and HTML, is there another route? I need to visualize the information in columns, etc. which is not currently available in Bear - but I like Bear’s interface, privacy, etc.

1 Like

You can’t use JS async calls on an HTML page with XURLs for security reasons.
Also, Bear’s API supports returning values but you need another app with XURL support to handle the reply.

I suggest trying to achieve this using Shortcuts’ Bear actions you can do multiple requests, get what you need from Bear, compose the HTML, and save it into a file.

Hi Trix180,

Thanks for that tip!

So, it seems like what I am actually looking for is a feature request for a shortcut that can dump all of the files of a certain tag into a document into a folder on my hard drive.
Do you happen to know if something like this is feasible?

(BTW - I tried to use shortcuts to save just one file to an .md file on my hard drive. It worked with the first state of the Bear note, but when I update the Bear note and then run the shortcut again, it did not continue to update the note on the hard drive).

The shortcut I’d be looking for is:

  1. search tag → get all files from that tag —> save those files to a folder on my hard drive → overwrite existing files with the same name (so that files are completely updated to the latest version).

Update: Seeing this can be done pretty easily from a keyboard shortcut to export a folder of the current tag, via Bear directly.

1 Like

I’m trying to do the same thing (export all notes with a specific tag to a directory of markdown files & attachments).

I’m curious – is there any way to programmatically trigger the tag-specific “Export” command you mentioned in your last sentence? It looks like it does exactly what I want, but I don’t know how to cause that action to happen without navigating to the tag in the Bear UI and pressing Cmd+Shift+S.

I’d like to be able to run a script that does the same thing instead, saving the files to a predetermined directory, and didn’t see a way to do this in Apple’s “Shortcuts” app and haven’t found find any other automation strategies yet. (My previous approach queried the SQLite database directly, but that’s less robust)

Edit: It looks like you can get a similar effect using shortcuts like this:

Which you can run from the command line with shortcuts run "Export Pub Notes" (replace the name with the name of your shortcut).

It would be nice if the exported text bundles put Bear’s note metadata (creation date, note title) into the info.json, but I managed to work around that by manually annotating notes with their dates and moving the title to the frontmatter in a postprocessing step.