DEV Community

Cover image for The Joys of Writing an Elixir SDK
Ben Greenberg
Ben Greenberg

Posted on

The Joys of Writing an Elixir SDK

My fascination with Elixir began in the late summer of 2019. I first became aware of this emerging language community a few months before, but could not spare time to devote to learning it until August. The concurrency, clean syntax and scalability of Elixir really interested me. As someone who spends a lot of time in the Ruby community, I could also not ignore the strong communal overlap between the two.

As such, in August I began slowly writing an experimental SDK for my company Nexmo in Elixir. Nexmo provides a suite of APIs for communications, such as SMS, Voice, Video, 2FA and a lot more, and has SDKs in a variety of languages, including Node.js, Ruby, PHP, Python and .NET.. We also offer experimental SDKs for the community, and albeit not officially supported, they are available to utilize as one needs. These include a Golang SDK, and now an Elixir SDK.

The Elixir SDK has been an act of love for me, and an attempt of not being afraid to learn in public. It continues to delight me in figuring out better ways to architect the SDK and better ways to get the job done.

A couple of months ago I moved the documentation officially to HexDocs, which is the Elixir community source of Hex packages docs. In so doing, I studied and learned ways to most effectively store the documentation inside the codebase for HexDocs to parse.

Just this week we have added support for another Nexmo API to the Elixir SDK, Applications API. This API lets you manage your Nexmo Applications through the SDK, and sit alongside support for the Number Insights, Account and SMS APIs.

Each API call is wrapped into a function that abstracts a lot of the work for you as the user of the API. For example, this is the function that wraps the create a Nexmo Application API POST request to the Applications API:

def create(params) do
  credentials = "#{Nexmo.Config.api_key}:#{Nexmo.Config.api_secret}" |> Base.encode64()
  headers = [
    {"Content-Type", "Application/json"},
    {"Authorization", "Basic #{credentials}"}
  ]
  body = Enum.into(params, %{})
  Nexmo.Applications.post("#{System.get_env("APPLICATIONS_API_ENDPOINT")}", Poison.encode!(body), headers)
end
Enter fullscreen mode Exit fullscreen mode

The function accepts an argument of params, which it turns into a map using the Enum.into/2 function. The new parameter map is then encoded into JSON and utilized in the POST request. The credentials are generated by Base 64 encoding the combination of the user's API key and API secret and passed into the headers in the Authorization format.

All the functions whether they wrap a POST, PUT, DELETE or GET request are similarly written to be simple and concise. This is something that comes easily in Elixir, and one of the reasons I enjoy the language so much.

We welcome contributions and you can find the code on GitHub.

GitHub logo nexmo-community / nexmo-elixir

An experimental Elixir client library for Nexmo

Nexmo Elixir Client Library

Build Status codecov

This is a work in progress Elixir client library for Nexmo. Functionality will be added for each Nexmo API service. Currently, this library supports the Account, Applications, Number Insight and SMS Nexmo APIs.

Installation

Hex

The Hex package can be installed by adding nexmo to your list of dependencies in mix.exs:

def deps do
  [
    {:nexmo, "~> 0.4.0", hex: :nexmo_elixir}
  ]
end
Enter fullscreen mode Exit fullscreen mode

Environment Variables

The client library requires environment variables to be supplied in order to enable its functionality. You can find a sample .env file in the root directory of the project. You need to supply your API credentials and the host names for the API endpoints in the .env file.

Your Nexmo API credentials:

  • NEXMO_API_KEY
  • NEXMO_API_SECRET

API host names:

  • ACCOUNT_API_ENDPOINT="https://rest.nexmo.com/account"
  • NUMBER_INSIGHT_API_ENDPOINT="https://api.nexmo.com/ni"
  • SECRETS_API_ENDPOINT="https://api.nexmo.com/accounts"
  • SMS_API_ENDPOINT="https://rest.nexmo.com/sms/json"

Documentation

If you'd like to give the package a spin, you can also find the package on Hex.

Top comments (0)