Back to Templates

Unkey + Elixir Mix Supervision

A simple example of using the Unkey Elixir SDK.

Written by
glamboyosa
Language
Elixir
A simple example of using the Unkey Elixir SDK.

Unkey + Elixir Mix Supervision

A simple example of using the Unkey Elixir SDK.

Installation

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

Note: This project uses Elixir version 1.13.

1def deps do
2 [
3 {:unkey_elixir_sdk, "~> 0.1.1"}
4 ]
5end

Start the GenServer

This example uses a supervision tree to start the SDK

The GenServer takes a map with two properties.

  • token: Your Unkey Access token used to make requests. You can create one here required
  • base_url: The base URL endpoint you will be hitting i.e. https://api.unkey.dev/v1/keys (optional).
1 children = [
2 {UnkeyElixirSdk, %{token: "yourunkeyapitoken"}}
3 ]
4
5
6# Now we start the supervisor with the children and a strategy
7{:ok, pid} = Supervisor.start_link(children, strategy: :one_for_one)
8
9# After started, we can query the supervisor for information
10Supervisor.count_children(pid)
11#=> %{active: 1, specs: 1, supervisors: 0, workers: 1}

NOTE In order to run this project either create a config/dev.secret.exs file with the key unkey_elixir_sdk_example and values specified in .env.example (token & apiId) OR directly use the environment variables in code (not recommended).

Run the project

You can run the project via the IEX console.

1iex -S mix

and call the methods like so:

1ElixirMixSupervisionExample.create_key_via_sdk

The full list of callable methods can be found here.

Protect your API.
Start today.

2500 verifications and 100K successful rate‑limited requests per month. No CC required.