Build your first API with Phoenix
7th June 2019Phoenix is an Elixir framework oriented to highly performant and scalable web applications, but before continuing, we need to know what is Elixir.
Elixir is a functional language designed for scalable and maintainable applications since it runs inside of threads of execution called processes to exchange information via messages. Another of its benefits is the fault-tolerance, that’s the reason for the expression “things will go wrong” but to confront the failures, Elixir provides supervisors that restart parts of your app when things go bad, going back to a known initial state that is guaranteed to work.
After this introduction, we are ready to code our first API!
The first thing before starting is to install Elixir:
brew install elixir
Once installed Elixir, we can install Phoenix running the next command:
mix archive.install hex phx_new 1.4.0
Now, we can create a Phoenix project:
mix phx.new books_store
Note: If we just want to build an API, we can add the next flags --no-html --no-webpack
to avoid all HTML views and webpack that is included by default but for now, it’s ok.
After command ends we already have the project created! so we can run next instructions:
- cd books_store
- mix ecto.create
- mix phx.server
Note: We need to be sure that we have Postgres installed because is the Database engine loaded by default in Phoenix.
Ecto is a library to interact with databases, we can say “is like an ORM”, but the difference is that Elixir is not an Object-Oriented language.
If everything goes well, we can visit http://localhost:4000
to see the welcome page.

The next step is to build our first schema and the JSON endpoint, running next command:
mix phx.gen.json Books Book books title:string price:integer publisher:string edition:string code:string
WhereBooks
is the context, Book
is our schema, books
is the name of the table in the database and finally the attributes, then we got something like this:

Phoenix added all, controller, schema, migrations, tests, and json views. So now, we are going to add the resource to our routes lib/books_store_web/router.ex
:
scope "/api", BooksStoreWeb do
pipe_through :api
resources "/books", BookController, except: [:new, :edit]
end
and run the migrations to create the books table with all attributes:
mix ecto.migrate
This command will insert into the database the schema we defined previously:
defmodule BooksStore.Repo.Migrations.CreateBooks do
use Ecto.Migration
def change do
create table(:books) do
add :title, :string
add :price, :integer
add :publisher, :string
add :edition, :string
add :code, :string
timestamps()
end
end
end
Before test our endpoint we need insert some data, we gonna do that through the console:
iex -S mix (access to the debug console)
alias BooksStore.Books
Books.create_book(%{title: "Elixir for beginners", price: 23_000, publisher: "McGraw", edition: "1st", code: "HH23DY"})
Books.create_book(%{title: "Phoenix for beginners", price: 15_000, publisher: "McGraw", edition: "3rd", code: "AAR67I"})
Run the server mix phx.server
and go to a REST Client like Postman or Insomnia, just to check our endpoints:


Also, we can test the CREATE, UPDATE, and DELETE, all those actions live in our books_controller.ex
file.
This is just an introduction to the Phoenix framework, how can we create an API in an easy way. This is the Phoenix documentation for more references.
See you soon!
View Comments