Morpheus - Lightweight library to auto convert between snake_case and camelCase in Phoenix APIs

Hello everyone! I’m excited to share my first Hex package: Morpheus.

What is Morpheus?

Morpheus is a lightweight library designed to seamlessly convert between snake_case and camelCase in Phoenix projects. It’s particularly useful when your Phoenix API needs to communicate with frontend JavaScript code, where camelCase is the common convention.

This library was inspired by proper_case, which hasn’t been updated since May 2020. I decided to create a new library that:

  • Works with the latest Elixir (1.17)
  • Has minimal dependencies (only Jason)
  • Focuses on the most common use case: converting between snake_case and camelCase
  • Maintains a lightweight and simple API

Features

  • Convert map keys from snake_case to camelCase and vice versa
  • Handle nested maps and lists
  • Preserve atom and string key types
  • Seamless integration with Phoenix for automatic conversion of JSON responses

Quick Start

Add morpheus to your dependencies in mix.exs:

def deps do
  [
    {:morpheus, "~> 0.1.0"}
  ]
end

In your config/config.exs, add the following after Jason config:

config :phoenix, :format_encoders, json: Morpheus.Encoder

Now your Phoenix controllers will automatically convert snake_case keys to camelCase in JSON responses:

def show(conn, %{"id" => id}) do
 user = %{
   user_id: id,
   first_name: "John",
   last_name: "Doe",
   email_address: "john@example.com"
 }
 
 json(conn, user)  # Will be converted to camelCase automatically
end

For incoming requests, add the plug to your router:

pipeline :api do
 plug :accepts, ["json"]
 plug Morpheus.Plugs.SnakeCaseParams
end

Links

This is my first contribution to the Elixir ecosystem. Feedback, suggestions, and contributions are welcome! Let me know if you find it useful or have any ideas for improvement.

6 Likes

Morpheus v0.1.2 – Minor Update & Fixes

Hey everyone!

Just a quick update on Morpheus, my Elixir library for converting between camelCase and snake_case. I pushed out version 0.1.2 about 10 days ago but forgot to post an update here.

What’s New?

  • v0.1.2 (2025-03-24)

    • Updated documentation with correct version references
    • No functional changes
  • v0.1.1 (2025-03-23)

    • Fixed handling of structs that previously caused conversion failures
    • Improved handling for struct types

For those new to Morpheus, it’s designed to simplify key transformation in Elixir applications, particularly when working with Phoenix APIs that need automatic conversion of JSON request/response keys.

Check it out here: Morpheus on Hex

Feedback and contributions are always welcome! :rocket:

There is also GitHub - wemake-services/recase: ♻ Convert strings to any case. if you need more than snake and camel case

1 Like

Thanks for sharing Recase! It’s a great library for general string case conversion. Morpheus, on the other hand, is specifically designed for Phoenix APIs, providing a plug for automatic conversion of incoming and outgoing parameters.

1 Like