NuID Go SDK


The Go SDK package provides an interface for interacting with NuID APIs and utilities. We encourage you to go read the package docs and contact us if you have any questions. We also encourage you to open an issue or pull request if you have any issues or suggestions.

Installation


Install the package from our GitHub source. Be sure to add your API Key to your environment configuration.

$ cd my-go-app
$ GO111MODULE=on go get github.com/NuID/sdk-go

# Add the API key and host to your environment configuration
$ export NUID_API_KEY="<API KEY>"

Usage


To use the package, import it into your server and configure it:

// server.go
package main

import (
	"fmt"
	"net/http"
	"os"

	"gorm.io/gorm"

	"github.com/rs/cors"
	"github.com/NuID/sdk-go/api/auth" // import the auth package
)

type Server struct {
	db *gorm.DB
	api *auth.APIClient
}

func main() {
	fmt.Println("Initializing...")
	srv := &Server{
		// Configure the package with your API Key
		api: auth.NewAPIClient(os.Getenv("NUID_API_KEY")),
		db: initDB(),
	}
	mux := http.NewServeMux()
	mux.HandleFunc("/register", srv.registerHandler)
	mux.HandleFunc("/challenge", srv.challengeHandler)
	mux.HandleFunc("/login", srv.loginHandler)
	port := os.Getenv("PORT")
	addr := "127.0.0.1:" + port
	server := &http.Server{
		Addr: addr,
		Handler: cors.Default().Handler(mux),
	}
	fmt.Printf("Listening at %s\n", addr)
	server.ListenAndServe()
}

Once you have a configured sdk client, you can use it to dispatch requests to the API with all the go error handling semantics you know and love.

// Create the NuID Credential on the API
_, credentialBody, err := srv.api.CredentialCreate(body.Credential)
if err != nil || credentialBody == nil{
	requestFailed(res, 500, "Unable to create credential")
	return
}
fmt.Println(credentialBody.NuID)

You can see a complete code example using the Go SDK in our examples repo, or go read our Integrating with NuID guide.