zenw0lf

zenw0lf

Tutorial: Building a JSON API with Phoenix 1.3 and Elixir

Hello all!

I just finished a full step-by-step tutorial on how to build a JSON API with Phoenix:
https://lobotuerto.com/blog/building-a-json-api-with-phoenix-and-elixir/

It covers:

  • Create a new API-only Phoenix application —skip HTML and JS stuff.
  • Create a User schema module (model) and hash its password —because storing plain text passwords in the database is just wrong.
  • Create a Users endpoint —so you can get a list of, create or delete users!
  • CORS configuration —so you can use that frontend of yours that runs on another port / domain.
  • Create a Sign in endpoint —using session based authentication.

Any feedback is appreciated!

P.S. Please bear in mind I’m just starting with Elixir and Phoenix. But did my best to try to make it as clear as possible for advanced users from other frameworks (like Rails).

Most Liked

josevalim

josevalim

Creator of Elixir

You can achieve that by simply passing a flag to phx.new:

mix phx.new --umbrella my_app

And then if you need an app that doesn’t talk to the database:

cd my_app/apps
mix new some_api_client

Those capabilities have been there since v1.3. It is not a default but it is definitely promoted as part of Phoenix as a first class concept.

There have been plenty of discussions on why it isn’t the default so maybe someone can post a link to those (in a nutshell, dropping users into an umbrella project without any context may confuse more than help). We also plan to explore and discuss it in the upcoming Phoenix v1.4 book update too.

oldpond

oldpond

There is native support in erlang. Here’s a good tutorial. http://rny.io/elixir/phoenix/ldap/2016/09/20/ldap-authenication-with-phoenix.html

GeetHoobs

GeetHoobs

Thank you, this is just what I need to start, as most tutorials are focusing on full stack.

You’re planning to use JWT, but do you have any recommendation for LDAP bindings for elixir?

zenw0lf

zenw0lf

Maybe because being web means it speaks HTTP?
Just think about it for a minute, I think you’ll find it makes sense.

Exadra37

Exadra37

No I am not being pedantic with the name choosen for the Module if that is what you are referring to.

because they are two separate concerns. An API can be used by a mobile app, a web app, an IOT device, another micro-service, etc.

As I said in the because bit an api can be consumed by many different clients, thus my concern is about separation of concerns and software architecture.

The tutorial seems to be based on the Phoenix structure that mixes endpoints for a web app with REST API api can be consumed by many different clients, thus my concern is about separation of concerns and software architecture.

The tutorial seems to be based on the Phoenix structure that mixes endpoints for a web app with REST API endpoints… by their docs it seems that way, both api and web endpoints under same folder lib/myapp_web. Am I misunderstanding something?

Keeping all this endpoints mixed is contra-productive in the long run for a software you release in production… I learned this lesson in the hard-way and now that I separate them is much more easy to maintain, add new features, less prone to bugs and more easy to onboard junior developers. Can take you more hours to develop but saves lots of hours more in the long run.

Now for a pet project is fine to have all mixed once they are normally to throw away once you are done with learning some features you are interest in. For a prototype I would not recommend to mix the endpoints, once they tend to end-up in production.

This is my point of view based on my experience… You may have another view and I love to discuss them, because we always learn something new :slight_smile:

Where Next?

Popular in Other Resources Top

alvises
Connecting Elixir Nodes with libcluster, locally and on Kubernetes I’m not used to do screencasts :smiley: Please let me know any feedba...
New
aifrak
tl;td: My solutions are here and here. Elixir is awesome. Exercism is awesome. Thank you Elixir community. Recently, I have solved all t...
New
ErlangSolutions
Webinar announcement :loudspeaker: We’re kicking 2020 off with a live coding demonstration of how you can build Tetris in Elixir. It’s a ...
New
luckywatcher
I’ve been working in Elixir for two years this month. I’ve enjoyed the community and the help I’ve received and want to give back in a sm...
New
ambareesha7
there are lot of learning resources and blogs are available but they scattered across, so i’m trying to pooling them in this repo. pleas...
New
wolfiton
Cool way to learn service workers by playing this game Also this game may not work well in the latest Firefox but works in Opera
New
landonwilkins
Hi! My apartment’s walls are blank. I want to fill at least one with Elixir diagrams. Links to Diagrams https://github.com/benjamintan...
New
Timtonix
A tous les français de la communauté Elixir, J’ai découvert hier ce livre traitant d’élixir Elixir - Un langage de programmation 100 %...
New
drobban
Hey everyone, Some of you might remember 4Clojure from back in the day—it was a fantastic site with a series of coding challenges for th...
New
wolfiton
Cool way to learn flexbox and have fun at the same time.
New

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New

We're in Beta

About us Mission Statement