Skip to content

Generate UUID in ClojureScript

ClojureScript has a built-in function to generate pseudo-random version 4 UUIDs

clojure
(random-uuid)

Note that since ClojureScript is a JavaScript based language, we can also use the same methods and libraries JavaScript itself has. So, the simplest way to generate a UUID is using crypto.randomUUID() which produces a valid random version 4 UUID. This function is only available when your page is served from localhost or via https

clojure
(.randomUUID crypto)

If you need to generate a specific UUID version, you need to use a third-party library such as uuid

clojure
(require '[clj-uuid :as uuid])

;;generate UUID version 1 in ClojureScript
(uuid/v1)

;;generate UUID version 3 in ClojureScript using a
;; version 4 random UUID as the namespace and a custom name
(uuid/v3)

;;generate UUID version 4 in ClojureScript
(uuid/v4)

;;generate UUID version 5 in ClojureScript using a
;; version 4 random UUID as the namespace and a custom name
(uuid/v5 (uuid/v4) "name")

;;generate UUID version 6 in ClojureScript
(uuid/v6)

;;generate UUID version 7 in ClojureScript
(uuid/v7)

Since ClojureScript is based on JavaScript any JavaScript libraries such as uuid will also work. The Online UUID Generator on this site was implemented using this library, so another way to see it in action is to open it and view the page source.

Installation & Setup

shadow-cljs (deps.edn):

clojure
{:deps {danlentz/clj-uuid {:mvn/version "0.1.9"}}}

Leiningen (project.clj):

clojure
:dependencies [[danlentz/clj-uuid "0.1.9"]]

Advanced ClojureScript Usage

UUID Service with ClojureScript:

clojure
(ns uuid-service
  (:require [clj-uuid :as uuid]))

(defn generate-v4 []
  (str (uuid/v4)))

(defn generate-v7 []
  (str (uuid/v7)))

(defn generate-with-crypto []
  (if (exists? js/crypto.randomUUID)
    (js/crypto.randomUUID)
    (str (uuid/v4))))

(defn valid-uuid? [uuid-str]
  (try
    (uuid/as-uuid uuid-str)
    true
    (catch js/Error _
      false)))

(defn parse-uuid [uuid-str]
  (try
    (uuid/as-uuid uuid-str)
    (catch js/Error _
      nil)))

;; Usage
(def my-uuid (generate-v4))
(js/console.log "Generated UUID:" my-uuid)
(js/console.log "Valid:" (valid-uuid? my-uuid))

Re-frame Integration:

clojure
(ns app.events
  (:require [re-frame.core :as rf]
            [uuid-service :as uuid]))

;; Event to generate new UUID
(rf/reg-event-db
 :generate-uuid
 (fn [db _]
   (assoc db :current-uuid (uuid/generate-v4))))

;; Event to create new item with UUID
(rf/reg-event-db
 :create-item
 (fn [db [_ item-data]]
   (let [item (assoc item-data :id (uuid/generate-v4))]
     (update db :items conj item))))

;; Subscription to get current UUID
(rf/reg-sub
 :current-uuid
 (fn [db _]
   (:current-uuid db)))

Reagent Component:

clojure
(ns app.components
  (:require [reagent.core :as r]
            [re-frame.core :as rf]
            [uuid-service :as uuid]))

(defn uuid-generator []
  (let [current-uuid @(rf/subscribe [:current-uuid])]
    [:div.uuid-generator
     [:h2 "UUID Generator"]
     [:div.uuid-display
      [:code current-uuid]]
     [:button
      {:on-click #(rf/dispatch [:generate-uuid])}
      "Generate New UUID"]
     [:button
      {:on-click #(js/navigator.clipboard.writeText current-uuid)}
      "Copy to Clipboard"]]))

Node.js ClojureScript:

clojure
(ns server.uuid
  (:require [clj-uuid :as uuid]
            ["express" :as express]))

(def app (express))

(.get app "/api/uuid"
      (fn [req res]
        (.json res #js {:uuid (str (uuid/v4))
                        :timestamp (.toISOString (js/Date.))})))

(.get app "/api/uuid/:name"
      (fn [req res]
        (let [name (.-name (.-params req))
              named-uuid (uuid/v5 (uuid/v4) name)]
          (.json res #js {:uuid (str named-uuid)
                          :name name
                          :deterministic true}))))

(.listen app 3000
         (fn []
           (js/console.log "Server running on port 3000")))

UUID Version Comparison

Choose the right version for your ClojureScript application:

  • Version 1 - Time-based, includes MAC address
  • Version 3 - MD5 namespace-based, deterministic
  • Version 4 - Random, most popular choice
  • Version 5 - SHA-1 namespace-based, more secure than v3
  • Version 6 - Time-ordered, better than v1 for databases
  • Version 7 - Modern time-based with improved sorting

For ClojureScript applications:

  • React apps: Use Version 4 for component state and keys
  • Real-time systems: Consider Version 7 for event ordering
  • Functional UIs: Use Version 5 for deterministic rendering

How do I generate UUID in other languages?

Functional programming:

  • Clojure - JVM functional programming

Web development:

Backend systems:

  • Go - google/uuid package
  • Rust - uuid crate

← Back to Online UUID Generator