# Creating a node entirely programmatically

**URL:** <https://org-roam.discourse.group/t/creating-a-node-entirely-programmatically/3841>\
**Category:** How To\
**Created:** [August 7, 2025, 6:06pm UTC](https://org-roam.discourse.group/t/creating-a-node-entirely-programmatically/3841 "2025-08-07T18:06:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![eamonnsullivan](https://avatars.discourse-cdn.com/v4/letter/e/c68b51/32.png) [@eamonnsullivan](https://org-roam.discourse.group/u/eamonnsullivan)\
**Post date:** [August 7, 2025, 6:06pm UTC](https://org-roam.discourse.group/t/creating-a-node-entirely-programmatically/3841/1 "2025-08-07T18:06:41Z")

</div>

I’m hoping this is an easy question: I want to create a small function that I can run with a key binding that creates a node and inserts a template automatically (so, bypassing any question about which template to use). I just want to go straight to start typing in a new node. I’m hoping to prompt for a title and then start typing. It’s for quickly filing a link, primarily. The template will take the url from the clipboard and insert it into the template. Which function would I start with?

---

<div class="post-metadata">

**Author:** ![eamonnsullivan](https://avatars.discourse-cdn.com/v4/letter/e/c68b51/32.png) [@eamonnsullivan](https://org-roam.discourse.group/u/eamonnsullivan)\
**Post date:** [August 7, 2025, 6:41pm UTC](https://org-roam.discourse.group/t/creating-a-node-entirely-programmatically/3841/2 "2025-08-07T18:41:14Z")

</div>

I’m guessing the entry point would be org-roam-capture, but with all of the parameters filled out and only one template provided. Has anyone does this and could show me an example?

---

<div class="post-metadata">

**Author:** ![eamonnsullivan](https://avatars.discourse-cdn.com/v4/letter/e/c68b51/32.png) [@eamonnsullivan](https://org-roam.discourse.group/u/eamonnsullivan)\
**Post date:** [August 7, 2025, 8:11pm UTC](https://org-roam.discourse.group/t/creating-a-node-entirely-programmatically/3841/3 "2025-08-07T20:11:29Z")

</div>

Actually, I think I found it (mostly copied from another thread):

```lisp
(defun eds/strip-invalid-chars (title)
  "Strip characters that don't work in filenames or github branches"
  (replace-regexp-in-string "[\\?\\>\\<\\|\\:\\&]" "" title))

(defun eds/process-title (title)
  "Downcase and hyphenate a title case string. Remove characters
that don't work in a filename."
  (eds/strip-invalid-chars (mapconcat 'identity (split-string (downcase title)) "-")))

(defun eds/create-new-note-from-clipboard-link (title)
  "Create a new note file in `org-roam-directory' and insert a link."
  (interactive "sTitle: ")
  (let* ((time-string (format-time-string "%Y%m%dT%H%M%S"))
         (clipboard-content (or (gui-get-selection 'CLIPBOARD) "Clipboard is empty."))
         (extension "org")
         (slug (eds/process-title title))
         (file-base-name (concat time-string "-" slug "." extension))
         (org-roam-directory (eds/get-org-directory))
         (file-name (expand-file-name file-base-name org-roam-directory))
         (title-line (format "#+title: %s\n" title))
         (startup-line "#+startup: content\n")
         (link-line (format "* [[%s][%s]]\n" clipboard-content title)))
    (find-file file-name)
    ;; In the new buffer
    (insert (concat title-line startup-line link-line))
    (beginning-of-buffer)
    (org-id-get-create)
    (end-of-buffer)
    (save-buffer)
    (org-roam-db-sync)))

```

---

<div class="post-metadata">

**Author:** ![akashp](https://yyz2.discourse-cdn.com/free1/user_avatar/org-roam.discourse.group/akashp/32/1383_2.png) [@akashp](https://org-roam.discourse.group/u/akashp)\
**Post date:** [August 8, 2025, 5:11pm UTC](https://org-roam.discourse.group/t/creating-a-node-entirely-programmatically/3841/4 "2025-08-08T17:11:46Z")

</div>

you can in the future look into org-roam-protocol which can be used to interact with the outside-emacs world - for example creating a node from the link in a webpage and so on. Might be useful to your usecase.

---

<div class="post-metadata">

**Author:** ![dmg](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@dmg](https://org-roam.discourse.group/u/dmg)\
**Post date:** [August 13, 2025, 5:04am UTC](https://org-roam.discourse.group/t/creating-a-node-entirely-programmatically/3841/5 "2025-08-13T05:04:44Z")

</div>

All you need to do is create a file in the org-roam directory (or children) with at least one node (either the file ID or a header with an ID) and it will be automatically added to the org-roam database.

That, however, does no “run” the template.

if you want to actually run the template as if you provided the key, see the code of the function:

org-roam-dailies–capture

it calls org-roam-capture as it was called interactively

---

<div class="post-metadata">

**Author:** ![eamonnsullivan](https://avatars.discourse-cdn.com/v4/letter/e/c68b51/32.png) [@eamonnsullivan](https://org-roam.discourse.group/u/eamonnsullivan)\
**Post date:** [August 21, 2025, 5:07pm UTC](https://org-roam.discourse.group/t/creating-a-node-entirely-programmatically/3841/6 "2025-08-21T17:07:37Z")

</div>

Thank you! That actually put me on the path of org-roam-protocol and roam-ref, which was exactly what I needed. I’m mostly using a Mac these days, so had to jump through an extra hoop or two, but I’ve added a bookmarklet to my browser and set up the ref template to do what I want:

```auto
(setq org-roam-capture-ref-templates
        '(("r" "ref" entry "* [[${ref}][${title}]]\n${body}\n%?"
           :target
           (file+head
            "%<%Y%m%d%H%M%S>-${slug}.org"
            "#+title: ${title}")
           :unnarrowed t)))

```

That mostly replaces that more complicated function I did above.

---

<div class="post-metadata">

**Author:** ![dmg](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@dmg](https://org-roam.discourse.group/u/dmg)\
**Post date:** [August 21, 2025, 5:53pm UTC](https://org-roam.discourse.group/t/creating-a-node-entirely-programmatically/3841/7 "2025-08-21T17:53:58Z")

</div>

if you use a mac, try hammerspoon. It is very powerful. I no longer user bookmarklets.

Another thing i do is run applescript to talk to the browser. that way I can get the URL/title/selected text from emacs (instead of from the browser).
