## 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](https://pkg.go.dev/github.com/NuID/sdk-go/api/auth) 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

- [Source Code](https://github.com/nuid/sdk-go)
- [Package Docs](https://pkg.go.dev/github.com/NuID/sdk-go/api/auth)
- [Report an issue](https://github.com/nuid/sdk-go/issues)
- [Full Example](https://github.com/NuID/examples/blob/main/go)
- [Integration Guide](/content/docs/guides/integration.html)

### TOC

- [Installation](/content/docs/server/go.html#install/index.html)
- [Usage](/content/docs/server/go.html#usage/index.html)

## Installation

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

```shell
$ 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:

```go
// 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.

```go
// 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](https://github.com/NuID/examples/tree/main/go), or go read our [Integrating with NuID guide](/content/docs/guides/integration.html).
