Writing Blazing Fast, Fault Tolerant, and Concurrent RESTFUL CRUD APIs in less than 4 minutes using Elixir Phoenix Framework.

Do you know that Facebook’s $19 Billion acquisition called WhatsApp make use of a rare functional programming language called Erlang/Elixir for it’s messaging? Pinterest, a company with popular app for pinning images moved its backend system from Java and adopted a young programming language called Elixir. Why? Pinterest’s notification system now uses Elixir to deliver 14,000 notifications per second. The notification system runs across 15 servers, whereas the old system, written in Java, ran on 30. The new code is about one-tenth of the size of the old code.

I am a language nut, I love exploring different languages and different frameworks. Recently, I came across a language called Elixir developed by José Valim which has a Ruby like syntax and it runs on Erlang Virtual Machine. Erlang VM and Erlang language was created by Ericson. In this tutorial I will walk you through on how to create fault tolerant, concurrent, scalable, and much performant RESTFUL CRUD APIs in matter of minutes.

Install Elixir

brew update && brew doctor
brew install postgresql
brew install elixir

Install Phoenix

mix archive.install https://github.com/phoenixframework/archives/raw/master/phoenix_new.ez

Install hex

mix local.hex

Create a new Phoenix Project

mix phoenix.new api

Navigate to the project directory

cd api

Open config/dev.exs and give credential to your Postgres database

Specify your Database Credentials

Create the Postgres database to use for the app:

mix ecto.create

Automatically generate your database models

mix phoenix.gen.json User users username:string email:string phonenumber:string age:integer

Open web/router.ex file and paste this code at the end of the file

scope “/api”, Api do
pipe_through :api
resources “/users”, UserController, except: [:new, :edit]

Update and migrate your database

mix ecto.migrate

Start Phoenix!

mix phoenix.server

Test your API (POST)

curl -H “Content-Type: application/json” -X POST -d ‘{“user”: {“username”: “saif”, “email”: “saif.rehman1@ibm.com”,“phonenumber”: “+971509786313”, “age”: 22}}’ http://localhost:4000/api/users

Test your API (GET)

curl -H "Content-Type: application/json" http://localhost:4000/api/users/1

Test your API (PUT)

curl -H “Content-Type: application/json” -X PUT -d ‘{“user”: {“username”: “saif”, “email”: “saif.rehman1@ibm.com”,“phonenumber”: “+971509786313”, “age”: 30}}’ http://localhost:4000/api/users

Test your API (DELETE)

curl -H “Content-Type: application/json” -X DELETE http://localhost:4000/api/users/1


Elixir and Phoenix are known for their speed, but that’s far from their only benefit. Elixir isn’t just a fast Ruby and Phoenix isn’t just Rails for Elixir. Through pattern matching, immutable data structures and new idioms your programs can not only become faster but more understandable and maintainable.

Elixir Phoenix is going to be the future of web apps and APIs in near future due to Erlang VM support and simplicity of the language. So, lets get functional!