Using Phoenix to create a REST API

2 min read

  1. Install Elixir and Erlang
# Arch Linux
sudo pacman -S elixir
# Debian
sudo apt install erlang-dev elixir
# Fedora
sudo dnf install elixir erlang
# macOS
brew install elixir
$ elixir -v
Erlang/OTP 27 [erts-15.1.2] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [jit:ns]
Elixir 1.17.3 (compiled with Erlang/OTP 27)
  1. Install Hex
mix local.hex
  1. Install Phoenix
mix archive.install hex phx_new
  1. Create a new Phoenix project (since we are creating a REST API, we will skip some of the options)
mix phx.new $FOLDERNAME \
--app $APPNAME \
--database $DATABASE \
--no-install \
--no-live \
--no-assets \
--no-html \
--no-dashboard \
--no-mailer \
--binary-id

Options:

Example:

mix phx.new my_app \
--app my_app \
--database postgres \
--no-install \
--no-live \
--no-assets \
--no-html \
--no-dashboard \
--no-mailer \
--binary-id
  1. Change directory to the project folder
cd my_app
  1. Change the database configuration in config/dev.exs
dev.exs
import Config
# Configure your database
config :my_app, My_app.Repo,
username: "postgres",
password: "postgres",
hostname: "localhost",
database: "my_app_dev",
stacktrace: true,
show_sensitive_data_on_connection_error: true,
pool_size: 10
  1. Install dependencies
mix deps.get
  1. Create the database
mix ecto.create
  1. Start the Phoenix server
mix phx.server
  1. Visit http://localhost:4000 in your browser to see the Phoenix welcome page.

Resources: