The Joys of Writing an Elixir SDK
Tags: elixir, opensource
Published: 2020-01-27 13:44:03

---
title: The Joys of Writing an Elixir SDK
published: true
description: The joyful journey in incrementally adding new features to an SDK written in #Elixir
tags: elixir, opensource
cover_image: https://thepracticaldev.s3.amazonaws.com/i/xpxjvakns1o6ivk3xwjg.png
---

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 <a href="https://developer.nexmo.com">Nexmo</a> 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 <a href="https://github.com/Nexmo/nexmo-node">Node.js</a>, <a href="https://github.com/Nexmo/nexmo-ruby">Ruby</a>, <a href="https://github.com/Nexmo/nexmo-php">PHP</a>, <a href="https://github.com/Nexmo/nexmo-python">Python</a> and <a href="https://github.com/Nexmo/nexmo-dotnet">.NET.</a>. We also offer experimental SDKs for the community, and albeit not officially supported, they are available to utilize as one needs. These include a <a href="https://github.com/nexmo-community/nexmo-go">Golang</a> SDK, and now an <a href="https://github.com/nexmo-community/nexmo-elixir">Elixir</a> SDK.


The Elixir SDK has been an act of love for me, and an attempt of not being afraid to <a href="https://www.swyx.io/writing/learn-in-public/">*learn in public*</a>. 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 <a href="https://hexdocs.pm/nexmo_elixir/api-reference.html">HexDocs</a>, 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, <a href="https://developer.nexmo.com/application/overview">Applications API</a>. 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:

```elixir
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
```

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 <a href="https://github.com/nexmo-community/nexmo-elixir">GitHub</a>. 

{% github nexmo-community/nexmo-elixir %}

If you'd like to give the package a spin, you can also find the package on <a href="https://hex.pm/packages/nexmo_elixir">Hex</a>.