217 words, 2 min read
    
  
  If you are using Komoot for planning and managing routes, here's a very simple Elixir module to interface with it.
defmodule Komoot do
  @email Application.compile_env!(:myapp, :komoot_email)
  @password Application.compile_env!(:myapp, :komoot_password)
  @user_id Application.compile_env!(:myapp, :komoot_user_id)
  @base_url Application.compile_env!(:myapp, :komoot_base_url)
  defp new(options) when is_list(options) do
    Req.new(
      base_url: "#{@base_url}/api/v007/",
      retry: :transient,
      auth: {
        :basic,
        "#{@email}:#{@password}"
      },
      headers: %{
        "content-type" => ["application/json"]
      }
    )
    |> Req.merge(options)
  end
  defp request(options) do
    case Req.request(new(options)) do
      {:ok, %Req.Response{status: 200, body: body}} ->
        {:ok, body}
      {:ok, %Req.Response{body: body}} ->
        {:error, String.trim(body)}
      {:error, response} ->
        {:error, "Unexpected response: #{inspect(response.body)}"}
    end
  end
  def tours(options \\ []) do
    request(
      url: "users/#{@user_id}/tours/",
      params:
        Keyword.merge(
          [
            limit: "24",
            status: "private",
            name: "",
            page: 0,
            sort_field: "date",
            sort_direction: "desc",
            hl: "nl"
          ],
          options
        )
    )
  end
  def download(route_id), do: request(url: "tours/#{route_id}.gpx")
  def delete(route_id), do: request(method: :delete, url: "tours/#{route_id}.gpx")
end
Don't forget to add this to your config:
config :myapp,
  komoot_email: "me@hostname.com",
  komoot_password: "password",
  komoot_user_id: "123456789123",
  komoot_base_url: "https://www.komoot.com"
Usage is very simple. Getting the list of tours can be done like this:
Komoot.tours(
  type: "tour_planned",
  page: 0, # Starts counting a 0
  sort: "name",
  name: search_query, # Filter on the name of the routes
  limit: 25
)
Download the GPX for a route is like this:
Komoot.download(route_id)
Deleting a route (at your own risk):
Komoot.delete(route_id)
Note that this is using a non-documented API from Komoot.
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.
