View Source UnkeyElixirSdk (UnkeyElixirSdk v0.3.0)

Documentation for UnkeyElixirSdk.

Link to this section Summary

Functions

Returns a specification to start this module under a supervisor.

Creates an API key for your users

Delete an api key for your users

Start the GenServer Returns {:ok, pid}

Updates the configuration of a key

Updates the remaining value for a specified key. Takes in a map of the shape: %{ "keyId": "key_123", "op": "increment", "value": 1 } Where "op" is "increment" | "decrement" | "set" and value is the value you want to increase by or nil (unlimited)

Verify a key from your users. You only need to send the api key from your user. Optionally, second param is a map with the key apiId which sends the apiId

Link to this section Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

@spec create_key(map()) :: map()

Creates an API key for your users

Returns a map with the key %{"keyId" => "key_cm9vdCBvZiBnb29kXa", "key" => "xyz_AS5HDkXXPot2MMoPHD8jnL"}

examples

Examples

iex> UnkeyElixirSdk.create_key(%{"apiId" => "myapiid"})
%{"keyId" => "key_cm9vdCBvZiBnb29kXa", "key" => "xyz_AS5HDkXXPot2MMoPHD8jnL"}

iex> UnkeyElixirSdk.create_key(%{ "apiId" => "myapiid", "prefix" => "xyz", "byteLength" => 16, "ownerId" => "glamboyosa", "meta" => %{ "hello" => "world" }, "expires" => 1_686_941_966_471, "ratelimit" => %{ "type" => "fast", "limit" => 10, "refillRate" => 1, "refillInterval" => 1000 }, "remaining" => 5 })

%{"keyId" => "key_cm9vdCBvZiBnb29kXa", "key" => "xyz_AS5HDkXXPot2MMoPHD8jnL"}

@spec delete_key(binary()) :: :ok

Delete an api key for your users

Returns :ok

examples

Examples

iex> UnkeyElixirSdk.delete_key("key_cm9vdCBvZiBnb29kXa")

:ok
@spec start_link(map()) :: {:ok, pid()}

Start the GenServer Returns {:ok, pid}

examples

Examples

iex> UnkeyElixirSdk.start_link(%{token: "yourtoken"})
`{:ok, pid}`

iex> UnkeyElixirSdk.start_link(%{token: "yourtoken", base_url: "theunkeybaseurl"})

{:ok, pid}

Link to this function

update_key(key_id, opts)

View Source
@spec update_key(binary(), map()) :: :ok

Updates the configuration of a key

Takes in a key_id argument and a map whose members are optional but must have at most 1 member present.

%{
  "name" => "my_new_key",
  "ownerId" => "still_glamboyosa",
   "meta" => %{
    "hello" => "world"
   },
   "expires" => 1_686_941_966_471,
   "ratelimit" => %{
   "type" => "fast",
   "limit" => 15,
   "refillRate" => 2,
   "refillInterval" => 500
   },
   "remaining" => 3
}

Returns :ok

examples

Examples

    iex> UnkeyElixirSdk.update_key("key_cm9vdCBvZiBnb29kXa", %{
  "name" => "my_new_key",
  "ownerId" => "still_glamboyosa",
   "meta" => %{
    "hello" => "world"
   },
   "expires" => 1_686_941_966_471,
   "ratelimit" => %{
   "type" => "fast",
   "limit" => 15,
   "refillRate" => 2,
   "refillInterval" => 500
   },
   "remaining" => 3
})
:ok
@spec update_remaining(map()) :: :ok

Updates the remaining value for a specified key. Takes in a map of the shape: %{ "keyId": "key_123", "op": "increment", "value": 1 } Where "op" is "increment" | "decrement" | "set" and value is the value you want to increase by or nil (unlimited)

Returns a map with the updated "remaining" value.

examples

Examples

iex> UnkeyElixirSdk.update_remaining(%{

"keyId": "key_123", "op": "increment", "value": 1 })

%{remaining: 100}
Link to this function

verify_key(key, opts \\ %{})

View Source
@spec verify_key(binary(), map()) :: map()

Verify a key from your users. You only need to send the api key from your user. Optionally, second param is a map with the key apiId which sends the apiId

Returns a map with whether the key is valid or not. Optionally sends ownerId and meta

examples

Examples

iex> UnkeyElixirSdk.verify_key("xyz_AS5HDkXXPot2MMoPHD8jnL")

`%{"valid" => true,
 "ownerId" => "chronark",
"meta" => %{
"hello" => "world"
}}`

iex> UnkeyElixirSdk.verify_key("xyz_AS5HDkXXPot2MMoPHD8jnL", %{"apiId"} => "api_sASDSsgeegd")

`%{"valid" => true,
 "ownerId" => "chronark",
"meta" => %{
"hello" => "world"
}}`