## NuID Clojure SDK

---

The Clojure SDK provides an interface for interacting with NuID APIs and utilities. We encourage you to go read the [package docs](https://cljdoc.org/d/nuid/sdk/) and [contact us](/content/contact.html) if you have any questions. We also encourage you to open an issue or pull request if you have any issues or suggestions.

### Reference

- `nuid/sdk`](https://clojars.org/nuid/sdk)
- Package Docs](https://cljdoc.org/d/nuid/sdk)
- Source Code](https://github.com/nuid/sdk-clojure)
- Report an issue](https://github.com/nuid/sdk-clojure/issues)
- Full Example](https://github.com/NuID/examples/blob/main/clojure-ring)
- Integration Guide](/content/docs/guides/integration.html)

### Installation

---

Install the package from [clojars](https://clojars.org/nuid/sdk) or [GitHub](https://github.com/nuid/zk/tree/platform/react-native). Be sure to add your API Key to your environment configuration.

```clojure
# my-clj-app/deps.edn
{:deps
 ;; ...
 nuid/sdk {:mvn/version "0.3.0"} ;; check clojars for latest version
 ;; or
 nuid/sdk {:git/url "https://github.com/NuID/sdk-clojure" :sha "..."}} 
```

### Usage

---

To use the package, require it into your app and configure it.

```clojure
(ns my.sever.api
 (:require [nuid.sdk.api.auth :as auth]))

(defn start-server!
 [& args]
 ;;...
 (auth/merge-opts! {::auth/api-key (System/getenv "NUID_AUTH_API_KEY")}
 ;;...
 )
```

Once you have a configured sdk client, you can use it to dispatch requests to the API.

```clojure
(defn register-handler
  [{:keys [body-params]}]
  (if-let [_ (db/find-by-email (:email body-params))]
    (fail-res 400 "Email address already taken")
    (let [register-res (auth/credential-create (:credential body-params))\
          nuid         (get-in register-res [:body "nu/id"])\
          user         (when (= 201 (:status register-res))\
                         (-> (select-keys body-params [:email :firstName :lastName])\
                             (assoc :nuid nuid)\
                             (db/user-insert!))\
                         (db/find-by-email (:email body-params)))]
      (if user
        {:status 201
         :body   {:user (user->from-db user)}}
        (fail-res 400 "Invalid request")))))
```

You can see a complete code example with ring using the Clojure SDK in our [examples repo](https://github.com/NuID/examples/tree/main/clojure-ring), or go read our [Integrating with NuID guide](/content/docs/guides/integration.html).
