Klaviyo

Installation

klaviyo is published on Hex. Add it to your list of dependencies in mix.exs:

def deps do
  [
    {:klaviyo, "~> 3.0"}
  ]
end

Usage

Requests can be made using the send/2 function. This function accepts a Klaviyo.RequestOperation struct as the first argument and a keyword list of configuration options as the second argument.

Resource modules (e.g.Klaviyo.Event) return Klaviyo.RequestOperation structs.

Example

iex> %Klaviyo.Event.all()
      ...> |> Klaviyo.send(
      ...>      api_key: "pk_xxx",
      ...>      revision: "2023-01-24"
      ...>    )
      {:ok, %Klaviyo.Response{}}

Configuration

The send/2 function takes a keyword list of configuration options as the second argument. These provide the client with additional details needed to make a request and various other options for how the client should process the request and how it should behave.

Options

Retries

klaviyo has a built-in mechanism for retrying requests that either return an HTTP status code of 500 or a client error. You can enable retries by providing a module that implements the Klaviyo.Retry behaviour to the :retry option when calling Klaviyo.send/2.

Currently, klaviyo provides a Klaviyo.Retry.Linear strategy for retrying requests. This strategy will automatically retry a request on a set interval. You can configure the interval by adding :retry_in with the number of milliseconds to wait before sending another request to the :retry_opts option.

Example

iex> %Klaviyo.Event.all()
      ...> |> Klaviyo.send(
      ...>      api_key: "pk_xxx",
      ...>      retry: Klaviyo.Retry.Linear,
      ...>      retry_opts: [retry_in: 250],
      ...>      revision: "2023-01-24"
      ...>    )
      {:ok, %Klaviyo.Response{}}

The example above would retry a failed request after 250 milliseconds. By default Klaviyo.Retry.Linear will retry a request immediately if :retry_in has no value