## NuID Ruby SDK Gem

The Ruby SDK gem provides an interface for interacting with NuID APIs and utilities. We encourage you to go read the [gem docs](https://rubydoc.info/gems/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://rubygems.org/gems/nuid-sdk)
- Gem Docs](https://rubydoc.info/gems/nuid-sdk)
- Source Code](https://github.com/nuid/sdk-ruby)
- Report an issue](https://github.com/nuid/sdk-ruby/issues)
- Full Example](https://github.com/NuID/examples/blob/main/ruby-rails)
- Integration Guide](/content/docs/guides/integration.html)

### TOC

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

## Installation

Install the gem from rubygems. Be sure to add your API Key to your environment configuration.

```ruby
# my-rails-app/Gemfile
gem "nuid-sdk"
```

```shell
$ bundle install
```

```ruby
# my-rails-app/config/application.rb
config.x.nuid.auth_api_key = ENV['NUID_API_KEY'] // '<API KEY>'
```

## Usage

To use the gem, require it into your app and configure it. This is simplest to do in your ApplicationController:

```ruby
require 'nuid/sdk'

class ApplicationController < ActionController::Base
  def nuid_api
    @nuid_api ||= ::NuID::SDK::API::Auth.new(Rails.configuration.x.nuid.auth_api_key)
  end

def render_error(error, status)
    render(json: {errors: [error]}, status: status)
  end

#...
end
```

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

```ruby
class UsersController < ApplicationController
  def create
    credential_res = nuid_api.credential_create(params[:credential])
    unless credential_res.code == 201
      return render_error("Unable to create the credential", :bad_request)
    end

user = User.create!({
      email: params[:email].strip.downcase,
      first_name: params[:firstName],
      last_name: params[:lastName],
      nuid: credential_res.parsed_response["nu/id"]
    })

render(json: { user: user }, status: :created)
  rescue => exception
    render_error(exception.message, 500)
  end
end
```

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